seneca wrote:I'm extremely new to PHP. I have the following code which I would like to add pagination to. I've tried nearly every tutorial I could find and am unable to get it to work. I can get pagination to work, but it's listing the result vertically down the page. What I need is for the results to be listed in a table that has 4 cells across, with 1 result being listed in each cell. I've gotten the code for displaying the data in the cells, I just can't get pagination to work with it.
Here is working code for pagination with columns.
Pagination only sorts out the number of pages to break your data records into.
It is the actual script/code that returns the number of data entries to be used in creating the actual columns and then if any left over create a new page and show balance of entries.
It's always better in my humble opinion to let code speak for itself.
If someone can write the code better for all of us to learn please do so.
I'm all for learning.
<?php
include ("../includes/dbconnect.php");
// Define your colors for the alternating rows
$color1 = "#ffffff";
$color2 = "#ccffff";
$row_count = 0;
$limit = 100;
// Perform a standard SQL query:
if (isset($_GET['page']))
{
$page = $_GET['page'];
}
else{
$page = 1;
}
// Figure out the limit for the query based
// on the current page number.
$from = (($page * $limit) - $limit);
// Figure out the current page result numbers
$fr = $from + 1;
$row_count = $fr;
$query_count = "select * from buyaname order by buyanameID asc";
$result_count = mysql_query($query_count);
$totalrows = mysql_num_rows($result_count);
if(empty($page)){
$page = 1;
}
$limitvalue = $page * $limit - ($limit);
//set the number of columns
$columns = 5;
// $pagelimit, $adminlimit
//change the query to get another field from the database
$query = "SELECT * FROM buyaname order by rand() asc LIMIT $limitvalue, $limit";
$result = mysql_query($query) or die ("can not select from buyaname");
//we add this line because we need to know the number of rows
$num_rows = mysql_num_rows($result);
echo "<center><table border='0' cellpadding='3' cellspacing='0' width='100%' valign=top class='tp'>";
//changed this to a for loop so we can use the number of rows
for($i = 0; $i < $num_rows; $i++) {
$row = mysql_fetch_array($result);
if($i % $columns == 0) {
//if there is no remainder, we want to start a new row
echo "<TR>";
}
// My preference only is to list the rows I want to display.
$ProductName = stripslashes($row['ProductName']);
$webit = stripslashes($row['webit']);
// change the row colors
$row_color = ($row_count % 2) ? $color1 : $color2;
echo "<TD valign=top width=115><center><table border='0' cellpadding='2' cellspacing='1' width='100' height='125' >
<tr><td bgcolor='$row_color'><center><A HREF='$webit'>$ProductName</A><br><br></center><br>
<center><A HREF='$webit'>More Info >>></A><hr color='#0000A0'></center></td></tr>
</table></TD>";
if(($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows) {
//if there is a remainder of 1, end the row
//or if there is nothing left in our result set, end the row
echo "</TR>";
// Add 1 to the row count
$row_count++;
}
}
//echo "</TABLE></center>";
echo "</TD></TR><TR><TD COLSPAN='$columns'>"; // Colspan needs to be the same as column number.
//echo "</TD></TR></TABLE></center>\n";
echo "<center><hr color=0000ff>";
echo("<center><font face='Arial' size=-1>");
/***** start of pagination *******/
// Figure out the total number of pages we will have
$total_pages = ceil($totalrows / $limit);
// Build First and Prev links. If on page 1, we won't make links
if($page > 1){
$prev = ($page - 1);
echo "<a href='{$_SERVER['PHP_SELF']}?page=1'>First</a> ";
echo "<a href='{$_SERVER['PHP_SELF']}?page=$prev'>Prev</a> ";
} // end if
// build the links to the 2 previous and 2 next pages
for($i = ($page - 2); $i <= ($page + 2); $i++){
// only make a link if the prev/next is a valid page number
if (($i >= 1) && ($i <= $total_pages)) {
echo ($page == $i) ? "[$i] " : "<a href='{$_SERVER['PHP_SELF']}?page=$i'>$i</a> ";
} // end if
} // end for
// Build Next and Last links. If on last page, we won't make a links
if($page < $total_pages){
$next = ($page + 1);
echo "<a href='{$_SERVER['PHP_SELF']}?page=$next'>Next</a> ";
echo "<a href='{$_SERVER['PHP_SELF']}?page=$total_pages'>Last</a>";
} // end if
/***** end of pagination ******/
//$to = $from + mysql_num_rows($result_count);
$showing = $row_count -1;
$showing = ceil($limit * $page);
echo("<p><center>showing $fr to $showing of $totalrows results<br />");
echo("<font face='Verdana' size='1'><a href='#top'>Top</a></font></center></p>");
echo "</TD></TR></TABLE></center>\n";
?></TD>
</TR>
</TABLE>