PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
cturner
Forum Contributor
Posts: 153 Joined: Sun Jul 16, 2006 3:03 am
Location: My computer
Post
by cturner » Mon Jul 24, 2006 8:11 pm
My code doesn't display the links to each page. Can someone please tell me why this is happening and how I can fix it? Thanks in advance.
Here is my code:
Code: Select all
require "config2.php";
// how many rows to show per page
$rowsPerPage = 2;
// by default we show first page
$pageNum = 1;
// if $_GET['article_id'] defined, use it as page number
if(isset($_GET['article_id'])) {
$pageNum = $_GET['article_id'];
}
// counting the offset
$offset = ($pageNum > 1) ? ($pageNum - 1) * $rowsPerPage : $rowsPerPage;
$query = "SELECT * FROM items LIMIT $offset, $rowsPerPage";
if ($r = mysql_query ($query)) {
$str = "";
while ($row = mysql_fetch_array ($r)) {
$str .= '<p>'. $row['date_entered'] . '<br />' . $row['article_item'] . '<br />
<a href="modify_article.php?id="'. $_GET['article_id'] .'">Modify</a>
<a href="delete_article.php?id="'. $_GET['article_id'] .'">Delete</a>
</p>';
}
if($str==="") {
echo 'No entries to be displayed. <a href="add_an_article.php">Click here</a> to return to the main page.<br />';
} else {
echo $str;
}
}
// how many rows we have in database
$query = "SELECT COUNT(article_id) AS numrows FROM items";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['article_id'];
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';
if ($pageNum > 1) {
$page = $pageNum - 1;
echo "<a href=\"$self?page=1&rowsPerPage=$rowsPerPage\">First Page</a> - ";
echo "<a href=\"$self?page=$page&rowsPerPage=$rowsPerPage\">Previous</a> - ";
} else {
echo "First Page - ";
echo "Previous - ";
}
if ($pageNum < $maxPage) {
$page = $pageNum + 1;
echo $pageNum." of ".$maxPage." - ";
echo " <a href=\"$self?page=$page&rowsPerPage=$rowsPerPage\">Next</a> - ";
echo "<a href=\"$self?page=$maxPage&rowsPerPage=$rowsPerPage\">Final Page</a>";
} else {
$page = $pageNum + 1;
echo $pageNum." of ".$maxPage." - ";
echo " Next - ";
echo "Final Page";
}
// and close the database connection
mysql_close();
cturner
Forum Contributor
Posts: 153 Joined: Sun Jul 16, 2006 3:03 am
Location: My computer
Post
by cturner » Mon Jul 24, 2006 9:07 pm
This is the output:
25 July, 2006
sdfdsf
Modify Delete
25 July, 2006
wewewe
Modify Delete
First Page - Previous - 1 of 0 - Next - Final Page
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Tue Jul 25, 2006 1:36 am
I could be wrong, but I believe
Should be..
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Tue Jul 25, 2006 2:58 am
You are right astions. The query aliases the selected element.
cturner
Forum Contributor
Posts: 153 Joined: Sun Jul 16, 2006 3:03 am
Location: My computer
Post
by cturner » Tue Jul 25, 2006 3:43 pm
Well that solved that problem.
Now how come it only prints the second row on the first page? How can I make the second page and so on appear after clicking on the next and final page links?
Here is the updated code:
Code: Select all
require "config2.php";
// how many rows to show per page
$rowsPerPage = 1;
// by default we show first page
$pageNum = 1;
// if $_GET['article_id'] defined, use it as page number
if(isset($_GET['article_id'])) {
$pageNum = $_GET['article_id'];
}
// counting the offset
$offset = ($pageNum > 1) ? ($pageNum - 1) * $rowsPerPage : $rowsPerPage;
$query = "SELECT * FROM items LIMIT $offset, $rowsPerPage";
if ($r = mysql_query ($query)) {
$str = "";
while ($row = mysql_fetch_array ($r)) {
$str .= '<p>'. $row['date_entered'] . '<br />' . $row['article_item'] . '<br /></p>';
}
if($str==="") {
echo 'No entries to be displayed. <a href="add_an_article.php">Click here</a> to return to the main page.<br />';
} else {
echo $str;
}
}
// how many rows we have in the database
$query = "SELECT COUNT(article_id) AS numrows FROM items";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';
if ($pageNum > 1) {
$page = $pageNum - 1;
echo "<a href=\"$self?page=1&rowsPerPage=$rowsPerPage\">First Page</a> - ";
echo "<a href=\"$self?page=$page&rowsPerPage=$rowsPerPage\">Previous</a> - ";
} else {
echo "First Page - ";
echo "Previous - ";
}
if ($pageNum < $maxPage) {
$page = $pageNum + 1;
echo $pageNum." of ".$maxPage." - ";
echo " <a href=\"$self?page=$page&rowsPerPage=$rowsPerPage\">Next</a> - ";
echo "<a href=\"$self?page=$maxPage&rowsPerPage=$rowsPerPage\">Final Page</a>";
} else {
$page = $pageNum + 1;
echo $pageNum." of ".$maxPage." - ";
echo " Next - ";
echo "Final Page";
}
// and close the database connection
mysql_close();