Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP displaying data in a 5 by 3 table with pagination that contains thumbs and other info

Tags:

php

mysql

I am making a website that will show night club revelers events and night establishments in a big city. Events could number in the hundreds. I would like to have data displayed in a 5 by 3 table that contains thumbs and other information with paging. For example:

Thumb_event1  Thumb_event2  Thumb_event3  Thumb_event4  Thumb_event5
event_name1   event_name2   event_name3   event_name3   event_name5
Date          Date          Date          Date          Date 

Thumb_event6  Thumb_event7  Thumb_event8  Thumb_event9  Thumb_event10
event_name6   event_name7   event_name8   event_name9   event_name10
Date          Date          Date          Date          Date 

Thumb_event11 Thumb_event12 Thumb_event13 Thumb_event14 Thumb_event15
event_name11  event_name12  event_name13  event_name14  event_name1
Date          Date          Date          Date          Date5

Records 1 to 15 of 36                First    Previous   Next   Last

How does one achieve this? Any help will be greatly appreciated. I have made PHP code before to display data in a similar fashion but without pagination.

This was to display some picture thumbs only some time back:

<?php
echo "\n<table>";
$i = 5;
while ($pictures = mysql_fetch_assoc($pictures_result))
{
if($i==5) echo "\n\t<tr>";
echo "<td width='100' height='100' align='center' valign='middle'><a href = 'picture_view.php?picture_id=$pictures[picture_id]'>
  <img src='User_Images/$pictures[picture_thumb_url]' border='0'/></a></td>\n";
$i--;
if($i==0) {
echo "\n\t</tr>\n\t<tr>";
$i = 5;
} 
}
if($i!=5) echo "\n\t\t<td colspan=\"$i\"></td>\n\t</tr>";
echo "\n</table>";
?> 

But I have no idea how to go about making pages if I set a limit of 15 per page. Thanks in advance.

like image 947
Kinyanjui Kamau Avatar asked Dec 28 '25 01:12

Kinyanjui Kamau


1 Answers

Well, you definitely need to know the number of pages that you will have in total, so that you can do something like this:

define('ITEMS_PER_PAGE', 15);

// do a count query here
$num_items = mysql_result($result);
$num_pages = ceil($num_items / ITEMS_PER_PAGE);

$cur_page = (isset($_GET['p']) && (intval($_GET['p']) > 0)) ? intval($_GET['p']) : 1;

if ($cur_page > $num_pages)
    die('This page does not exist.');

$limit_clause = 'LIMIT '.(($cur_page - 1)*ITEMS_PER_PAGE).','.ITEMS_PER_PAGE;
// Add this to the end of your query
// YOUR QUERY GOES HERE

// YOUR THUMBNAIL CODE GOES HERE

// Now create the links
$links = array();
if ($cur_page > 1)
{
   $links[] = '<a href="page.php?p=1">First page</a>';
   $links[] = '<a href="page.php?p='.($cur_page-1).'">Previous page</a>';
}
if ($cur_page < $num_pages)
{
   $links[] = '<a href="page.php?p='.($cur_page+1).'">Next page</a>';
   $links[] = '<a href="page.php?p='.$num_pages.'">Last page</a>';
}

So what you do is you calculate the number of overall pages (based on how many items you have) and the page you're currently on and use that to show links to previous or later pages (if necessary).

I hope that helps. Ask away if there are more questions.

EDIT: I didn't realize you did not yet have the query with LIMIT clause. So, to add the code mentioned in the other answer, I added the code how you have to adapt your query.

like image 52
Franz Avatar answered Dec 30 '25 14:12

Franz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!