Answer the question
In order to leave comments, you need to log in
How to make a selection to the main page from the database by the word?
There is a main page, it displays data from the database by id, but there is not enough experience to add one more field to the selection so that they are sorted on each page depending on the word that is indicated in the table field.
Here is index.php
<!DOCTYPE html>
<html lang="ru">
<head>
<?php
require_once "func/func.php";
$tovar = getNews(9, $id);
require_once "blocks/head.php";
?>
</head>
<body>
<!-- Подключаем блок с шапкой -->
<?php require_once "blocks/header.php";?>
<section class="main">
<div class="container">
<div class="row">
<div class="col-lg-8">
<div class="main-content">
<div class="row">
<!-- Выводим карточки из таблицы -->
<?php
for ($i = 0; $i < count($tovar); $i++){
echo '
<div class="col-lg-4">
<div class="main-content-box">
<img src="img/'.$tovar[$i]["id"].'.jpg" alt="Журнальный столик">
<h2>'.$tovar[$i]["title"].'</h2>
<p>'.$tovar[$i]["chena"].'р</p>
<a href="cart.php?id='.$tovar[$i]["id"].'"><button class="btn">Подробнее</button></a>
</div>
</div>
';
}
?>
</div>
</div>
</div>
<div class="col-lg-4">
<div class="main-banner text-center">
<a href="#"><img src="img/banner.jpg" alt="Ваша реклама"></a>
</div>
</div>
</div>
</div>
</section>
<!-- Подключаем блок с футером -->
<?php require_once "blocks/footer.php";?>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
Include all compiled plugins (below), or include individual files as needed
<script src="js/bootstrap.min.js"></script> -->
</body>
</html>
<?php
$mysqli = false;
function connectDB (){
global $mysqli;
$mysqli = new mysqli("localhost", "root", "", "tovarbase");
$mysqli->query("SET NAMES 'utf-8'");
}
function closeDB (){
global $mysqli;
$mysqli->close ();
}
function getNews($limit, $id){
global $mysqli;
connectDB ();
if($id)
$where = "WHERE `id` = ".$id;
$result = $mysqli->query("SELECT * FROM `tovar` $where ORDER BY `id` DESC LIMIT $limit");
closeDB ();
if(!$id)
return resultToArray($result);
else
return $result->fetch_assoc();
}
function resultToArray ($result){
while (($row = $result->fetch_assoc()) != false)
$array[] = $row;
return $array;
}
?>
Answer the question
In order to leave comments, you need to log in
So after all AND, for example:
And everything is googled in this topic: 1 , 2
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question