View Full Version : PHP dynamic navigation problems


onigiri
01-05-2006, 04:41 AM
I have a dynamic navigation script in the page of my blog that displays the entries. This script is supposed to make it so there are only five entries displayed per page, and it will make pages based on the number of entries divided by five. The script has no problem creating pages, but it displays all of the entries on every page. (Did I explain the problem clearly enough?) :o

index.php:

<?php
//connect to the database
require_once("config.php");

//Gather the posts from the database
$query = "SELECT id, post, title, create_time FROM blog ORDER BY id DESC";
$query_result = mysql_query($query,$db) or die(mysql_error());

// dynamic navigation variables
$rows_per_page = 5;
$total_records=mysql_num_rows($query_result);
$pages = ceil($total_records / $rows_per_page);

$screen = $_GET["screen"];
if (!isset($screen)) {
$screen=0;
}
$start = $screen * $rows_per_page;
$query .= " LIMIT $start, $rows_per_page";
$result= mysql_query($query) or die
("Could not execute query : $query." . mysql_error());

if(mysql_num_rows($query_result) < 1) {//no posts
echo "Antibaseball has not posted yet.";
} else {
while($row=mysql_fetch_array($query_result)) {
$id = $row['id'];
$entry = $row['post'];
$title = $row['title'];
$create_time = $row['create_time'];
?>

<table width="80%" border="0" cellspacing="1" cellpadding="0" class="entry">
<tr>
<td><p class="header"><?php echo "$entry"; ?></p></td>
</tr>
<tr valign="top">
<td><?php echo "$title"; ?></p></td>
</tr>
<tr>
<td><p class="timestamp"><?php echo "$create_time"; ?></td>
</tr>
<tr>
<td align="right"><?php
if(isset($_SESSION['username'])) {
echo "<a href='admin/admin_editpost.php?id=$id'>Edit Post</a> || <a href='admin/admin_deletepost.php?id=$id'>Delete Post</a>";
}
?>
</td>
</tr>
</table>
<p></p><p></p>
<?
}
}

// Display dynamic navigation here

// create the dynamic links
if ($screen > 0) {
$j = $screen - 1;
$url = "index.php?screen=$j";
echo "<a href=\"$url\">Prev</a>";
}

// page numbering links now

for ($i = 0; $i < $pages; $i++) {
$url = "index.php?screen=" . $i;
$j = $i + 1;
echo " | <a href=\"$url\">$j</a> | ";
}

if ($screen < $pages-1) {
$j = $screen + 1;
$url = "index.php?screen=$j";
echo "<a href=\"$url\">Next</a>";
}

?>

Douglas
01-05-2006, 05:40 PM
Ah good old pagination, dun dun dun, rewrite to the rescue!:


<?php
//connect to the database
require_once("config.php");

//Gather the posts from the database
$query="SELECT * FROM blog ORDER BY id DESC";
$query_result=mysql_query($query,$db) or die(mysql_error());

// dynamic navigation variables
$rows_per_page=5;
$total_records=mysql_num_rows($query_result);
$pages = ceil($total_records/$rows_per_page);

$screen = $_GET["screen"];
if (!isset($screen)) {
$screen=0;
}
$start=$screen*$rows_per_page;
$query .= " LIMIT $start, $rows_per_page";
$result=mysql_query($query) or die
("Could not execute query : $query." . mysql_error());

if ($total_records < 1) {//no posts
echo "Antibaseball has not posted yet.";
}
else {
while($row=mysql_fetch_array($query_result)) {
$id=$row['id'];
$entry=$row['post'];
$title=$row['title'];
$create_time=$row['create_time'];
?>

<table width="80%" border="0" cellspacing="1" cellpadding="0" class="entry">
<tr>
<td><p class="header"><?php echo $entry; ?></p></td>
</tr>
<tr valign="top">
<td><?php echo "$title"; ?></p></td>
</tr>
<tr>
<td><p class="timestamp"><?php echo $create_time; ?></td>
</tr>
<tr>
<td align="right"><?php
if (isset($_SESSION['username'])) {
echo "<a href='admin/admin_editpost.php?id=$id'>Edit Post</a> || <a href='admin/admin_deletepost.php?id=$id'>Delete Post</a>";
}
?>
</td>
</tr>
</table>
<p></p><p></p>
<?
}
}

// Display dynamic navigation here

// create the dynamic links
if ($screen > 0) {
$j = $screen - 1;
$url = "index.php?screen=$j";
echo "<a href=\"$url\">Prev</a>";
}

// page numbering links now

for ($i = 0; $i < $pages; $i++) {
$url = "index.php?screen=" . $i;
$j = $i + 1;
echo " | <a href=\"$url\">$j</a> | ";
}

if ($screen < $pages-1) {
$j = $screen + 1;
$url = "index.php?screen=$j";
echo "<a href=\"$url\">Next</a>";
}

?>


That should would good, or maybe not xD

onigiri
01-06-2006, 01:10 AM
Darn, that doesn't work. Stupid PHP. X(

harmor
01-06-2006, 01:16 AM
I'm sorry but I never studied pagination yet but hopefully this tutorial will help you
http://www.tutorialized.com/tutorial/MySQL-Record-Pagination/9914

Douglas
01-06-2006, 01:28 AM
I didn't see anything wrong with the pagination, but anyway, I'll do it:


<?php
//connect to the database
require_once("config.php");

//Gather the posts from the database
$query="SELECT * FROM blog ORDER BY id DESC";
$query_result=mysql_query($query,$db) or die(mysql_error());

// dynamic navigation variables
$rows_per_page=5;
$total_records=mysql_num_rows($query_result);
$pages = ceil($total_records/$rows_per_page);

$screen = $_GET["screen"];
if (!isset($screen)) {
$screen=0;
}
$start=$screen;
$query .= " LIMIT $start, $rows_per_page";
$result=mysql_query($query) or die
("Could not execute query : $query." . mysql_error());

if ($total_records < 1) {//no posts
echo "Antibaseball has not posted yet.";
}
else {
while($row=mysql_fetch_array($query_result)) {
$id=$row['id'];
$entry=$row['post'];
$title=$row['title'];
$create_time=$row['create_time'];
?>

<table width="80%" border="0" cellspacing="1" cellpadding="0" class="entry">
<tr>
<td><p class="header"><?php echo $entry; ?></p></td>
</tr>
<tr valign="top">
<td><?php echo "$title"; ?></p></td>
</tr>
<tr>
<td><p class="timestamp"><?php echo $create_time; ?></td>
</tr>
<tr>
<td align="right"><?php
if (isset($_SESSION['username'])) {
echo "<a href='admin/admin_editpost.php?id=$id'>Edit Post</a> || <a href='admin/admin_deletepost.php?id=$id'>Delete Post</a>";
}
?>
</td>
</tr>
</table>
<p></p><p></p>
<?
}
}

// Display dynamic navigation here

// create the dynamic links
if ($total_records == 0) {

}
elseif ($total_records == 1) {
?>
<a href="index.php?screen=0">1</a>
<?
}
else {
if ($pages > 19) {
for ($i=0;$i<20;$i++) {
$new_i=$i+1;
$new_new_i=($rows_per_page*$new_i)-$rows_per_page;
?>
<a href="index.php?screen=<? echo $new_new_i; ?>"><? echo $new_i; ?></a> |
<?
}
}
else {
for ($i=0;$i<$pages;$i++) {
$new_i=$i+1;
$new_new_i=($rows_per_page*$new_i)-$rows_per_page;
?>
<a href="index.php?screen=<? echo $new_new_i; ?>"><? echo $new_i; ?></a> |
<?
}
}
}
?>


This should work ;) It will only show up to 20 pages, you can change this by editing the:

if ($pages > 19) {

part to anything, 1 less than the number of pages you want to show up :)

onigiri
01-06-2006, 02:50 AM
I don't get it. It still shows all the entries on every page.