0

hello is there a way to create pages of loaded divs from sql? Id like to limit it to 6 per page

I used:

$result = mysqli_query($conn,"SELECT * FROM products ORDER BY ID DESC LIMIT 6"); to limit and I want to display the other ones in next pages.

if(isset($_POST['filter']))
{
$filter = $_POST['filter'];
$result = mysqli_query($conn,"SELECT * FROM products where Product like '%$filter%' or Description like '%$filter%' or Category like '%$filter%'");
                    
}
else
{
$result = mysqli_query($conn,"SELECT * FROM products ORDER BY ID DESC LIMIT 6");
}           
if($result){                
while($row=mysqli_fetch_array($result)){        
$prodID = $row["ID"];   
echo '<ul class="col-sm-4">';
echo '<div class="product-image-wrapper">
<div class="single-products">
<div class="productinfo text-center">
<a href="product-details.php?prodid='.$prodID.'" rel="bookmark" title="'.$row['Product'].'"><img src="reservation/img/products/'.$row['imgUrl'].'" alt="'.$row['Product'].'" title="'.$row['Product'].'" width="150" height="150" /></a>
</a>
                    
<h2><a href="product-details.php?prodid='.$prodID.'" rel="bookmark" title="'.$row['Product'].'">'.$row['Product'].'</a></h2>
<h2>₽ '.$row['Price'].'</h2>
<p>Stock: '.$row['Stock'].'</p>
<p>Category: '.$row['Category'].'</p>
                    
<a href="product-details.php?prodid='.$prodID.'" class="btn btn-default add-to-cart"><i class="fa fa-search"></i>View Details</a>
</div>';        
echo '</ul>';           
}
}
?>

Thank you.

  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Nov 04 '21 at 07:55
  • hi sir @Dharman, can you help out on the making the results from query to a pagination. I dont know where to start and even tutorials arent working. Badly need help Thank you sir. – CleverNever Nov 04 '21 at 11:52
  • Why do you ping me? Please don't beg for help, it's rude. Stack Overflow is not a help desk. You can't expect people to offer personal help here. If you need personal guidance I recommend you get a programming course or find a tutor. – Dharman Nov 04 '21 at 11:53
  • sorry sir. i just thought you know how. sorry for the trouble. – CleverNever Nov 04 '21 at 11:57

1 Answers1

0

Use the LIMIT clause with an offset. For example:

 SELECT ... LIMIT 0, 6 -- select the six rows starting at row 0

then

 SELECT ... LIMIT 6, 6 -- select the six rows starting at row 6
 SELECT ... LIMIT 12, 6 -- select the six rows starting at row 12
 ...

See the MySQL Manual for more details

  • hi, i kinda want it to have page numbers at the bottom if the display is over 6 items (i have plenty of items so itll be alot of pages). i tried your answer but it only limits them. thanks for the answer tho appreciate it. – CleverNever Nov 04 '21 at 01:40