|
Posted by mpar612 on 12/16/82 11:58
Hi everyone,
The code posted below retrieves all of the rows from a database table.
It paginates them by limiting to 20 results per page. It is supposed
to print 5 rows of results with 4 results per row. Everything works
fine, except the first row returned from the database is never printed.
It doesn't matter how the results are ordered or how many rows are in
the db, the first row that is supposed to be printed is not printed.
Does anyone have any thoughts? I have been going nuts over this for
the past day.
Any feedback would be great. Thanks!
<?php
$limit = 20;
$query_count = "SELECT * FROM lounge_products";
$result_count = mysql_query($query_count);
$totalrows = mysql_num_rows($result_count);
$PHP_SELF = $_SERVER['PHP_SELF'];
if(!isset($_GET['page'])){ // Checks if the $page variable is empty
(not set)
$page = 1; // If it is empty, we're on page 1
} else{
$page = $_GET['page'];
}
$limitvalue = $page * $limit - ($limit );
// Ex: (page2 * 5(items per page) = 10) - 5 = 5 <- data starts at 5
$query = "SELECT * FROM lounge_products ORDER BY artist_name DESC
LIMIT $limitvalue, $limit";
$result = mysql_query($query) or die("Error: " . mysql_error());
if(mysql_num_rows($result) == 0){
echo("Nothing to Display!");
}
echo("<table>");
while($row = mysql_fetch_array($result)){
$counter = 0;
$cells_per_row = 4;
while($row=mysql_fetch_array($result))
{
$counter++;
print $counter;
if(($counter % $cells_per_row) == 1) { echo '<tr>'; }
echo '<td width=60 valign=top>' . ('<img src="'.$row[image] .'"
height="60" width="60"><br>'.$row[artist_name]) . '</td>';
if(($counter % $cells_per_row) == 0) { echo '</tr>'; }
}
// just in case we haven't closed the last row
// this would happen if our result set isn't divisible by
$cells_per_row
if(($counter % $cells_per_row) != 0) { echo '</tr>'; }
} //closing while loop
echo("</table>");
if($page != 1){
$pageprev = $page - 1; //decrementing $page-- does not work on
all php configurations.
echo("<a href=\"$PHP_SELF?page=$pageprev\">PREV</a> ");
}else{
echo("PREV");
}
$numofpages = $totalrows / $limit;
#echo "<br>", $totalrows;
#exit;
for($i = 1; $i <= $numofpages; $i++){
/* This for loop will add 1 to $i at the end of each pass until
$i is greater
than $numofpages. */
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
}
} //code above has been decoded lozza
if(($totalrows % $limit) != 0){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
}
}
if(($totalrows - ($limit * $page)) > 0){
$pagenext = $page + 1; //incrementin $page like $page++ does
not work in all enviroments
echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT</a>");
}else{
echo("NEXT");
}
mysql_free_result($result);
?>
Navigation:
[Reply to this message]
|