I have my articles on one of my websites organized newest to oldest using simple php. Now that the number of articles is increasing, I need pagination. The tutorials I've been trying to do are not showing me how to keep my articles from newest to oldest (ascending id) with the pagination. Can any give me a hand? This is how I have it now. <div id="content"> <?php $sql = mysql_query("SELECT * FROM enteries ORDER BY id DESC"); while($row = mysql_fetch_array($sql)){ $title = $row['title']; $description = $row['description']; $image = $row['image']; ?> <a class="box" href="#"> <div class="boxleft"><img src="postpics/<?php echo $image ?>" width="148" height="120"></div> <div class="boxleft"> <p class="title"><?php echo $title ?></p> <p class="des"><?php echo $description ?></p> </div> </a> <?php } ?> </div> Code (markup): Any help would be greatly appreciated.
Hi Clockwork Joe, To make a pagination working you need the add a variable LIMIT in your query. This variable will be set usually with an $_GET response. For example: <div id="content"> <?php // Use a specified url to get an $_GET response, like: http://yourwebsite.com/?page=2 $GETPAGE = intval($_GET['page']); $sql = mysql_query("SELECT * FROM enteries ORDER BY id DESC LIMIT ".$GETPAGE.",18 "); while($row = mysql_fetch_array($sql)){ $title = $row['title']; $description = $row['description']; $image = $row['image']; ?> <a class="box" href="#"> <div class="boxleft"><img src="postpics/<?php echo $image ?>" width="148" height="120"></div> <div class="boxleft"> <p class="title"><?php echo $title ?></p> <p class="des"><?php echo $description ?></p> </div> </a> <?php } ?> </div> PHP: Hope this help you out, Kind regards, Maarten
Hello Clockwork Joe, I would just google this topic because there are loads of simple examples/tutorials of pagination in PHP out there. I would not recomnend using marht's code as you should not be using PHP's old MySQL API at all. Check out PDO, which allows you to use prepared statements among other things - http://php.net/manual/en/book.pdo.php
Hi MakZF, I agree with you that PDO is better. Nevertheless, the code is not bad and it gives the OP a pagination system ;-). Regards
I'm not sure if the code above does what it's supposed to do. For instance, ?page=2 will start at the second record, and pull the next 18. Page 3 will start at the third record, and go from there. It's not how pagination is supposed to work, I'm afraid. To calculate the offset, you have to multiply the page number by the amount of rows you want to display on each page.
Thank you for correcting me. This should do the trick: <div id="content"> <?php // Use a specified url to get an $_GET response, like: http://yourwebsite.com/?page=2 $GETPAGE = intval($_GET['page']); $items_per_page = 18;//INPUT HERE THE NUMBER OF ITEMS PER PAGE MAX if ($GETPAGE!=0) $GETPAGE=(($GETPAGE-1)*$items_per_page); $sql = mysql_query("SELECT * FROM enteries ORDER BY id DESC LIMIT ".$GETPAGE.",".$items_per_page." "); while($row = mysql_fetch_array($sql)){ $title = $row['title']; $description = $row['description']; $image = $row['image']; ?> <a class="box" href="#"> <div class="boxleft"><img src="postpics/<?php echo $image ?>" width="148" height="120"></div> <div class="boxleft"> <p class="title"><?php echo $title ?></p> <p class="des"><?php echo $description ?></p> </div> </a> <?php } ?> </div> PHP:
Actually you don;t have to LIMIT the queries do something like array_chunk(); that will be an easy start for n00bs.. hehe.
what if you use the current date ...and get a greater/lesser than <> script to grab only the recent articles rather than processing and pulling * wildcard ALL... that'll kill your pagination the more articles you file.
The wildcard is for the fields, and not the rows. Having it compare ALL dates in the database is actually much slower than using the offset/limit.