Why isn't my code displaying the links???

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

Post Reply
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Why isn't my code displaying the links???

Post by cturner »

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();
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

what exactly is the output?
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I could be wrong, but I believe

Code: Select all

$numrows = $row['article_id'];
Should be..

Code: Select all

$numrows = $row['numrows'];
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You are right astions. The query aliases the selected element.
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

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();
Post Reply