Logic Problem with Next/Previous multiple listing from DB.

From: Date: Thu, 05 Oct 2000 20:54:08 +0000
Subject: Logic Problem with Next/Previous multiple listing from DB.
Groups: php.db php.general 
Request: Send a blank email to php-db+get-3365@lists.php.net to get a copy of this message
Im having a bit of a problem implementing a "Next X Articles" and "Previous X Articles" function to my news posting code Im working on. The "Get Next X Articles" is working just fine, but I cant get the "Get Previous X Articles" to work properly. I know it is just a logic problem, but I can't seem to come up with a solution. Entire code is posted below: Problem area is at the very end starting with if ( $offset < $numrows ) I can see that my $offset is being globally altered before I get to my "View Previous X Articles". Also, is there a way to get the total total number of rows, not based of a query. In my code, I make 2 seperate DB connections. One to get the total number of rows just for my Next X, Previous X, and another to actually pull the data. Any ideas on how to solve these problems? Thanks to all who can help in advance! ____________________ James C. Montz <!-- Begin PHP Code --> <% $limit=5; if (!isset($offset)) $offset=0; mysql_connect("localhost", "linux", "linuxsql") or DIE("Unable to connect to database!"); $rows = "SELECT id FROM news"; $query = "SELECT title, author, date, story, id FROM news ORDER by id DESC LIMIT $offset,$limit"; $result = mysql_db_query("linux", $query); $getrows = mysql_db_query("linux", $rows); $numrows=mysql_num_rows($getrows); if ($result) { while ($r = mysql_fetch_array($result)) { $title = $r["title"]; $author = $r["author"]; $date = $r["date"]; $story = $r["story"]; $id = $r["id"]; echo "<table border=0 width=100% bordercolor=#000000 cellspacing=1 cellpadding=2 bgcolor=#000000>"; echo "<tr>"; echo "<td width=10% align=center bgcolor=#000000><b><font size=2><a href=\"edit.php3?id=$id\">EDIT</a><font color=#FFFFFF>&nbsp;&nbsp;|&nbsp;&nbsp;</font><a href=\"delete.php3?id=$id\">DELTE</a></font></b></td>"; echo "<td width=30% bgcolor=#CCCCCC><font size=3>Title: </font><font size=4>$title</font></td>"; echo "<td width=30% bgcolor=#C0C0C0><font size=3>Author: </font><font size=4>$author</font></td>"; echo "<td width=30% bgcolor=#999999><font size=3>Date:</font><font size=4>$date</font></td>"; echo "</tr>"; echo "<tr>"; echo "<td width=10% bgcolor=#FFFF99 align=center><font size=2><u>Article ID</font></u><br><font size=4>$id</font></td>"; echo "<td width=90% bgcolor=#FFFFFF colspan=3>$story</td>"; echo "</tr>"; echo "</table><img border=0 src=/images/spacer.gif width=10 height=10>"; } } else { echo "No Articles to View."; } echo "<br>Numrow set to: $numrows\n"; echo "<br>Offset set to: $offset\n"; mysql_free_result($result); if ( $offset < $numrows ) { $offset += $limit; echo "<p align=right><a href=\"$PHP_SELF?offset=$offset\">Next $limit Articles</a></p>"; } if ( $offset > $numrows ) { $offset -= $limit; echo "<p align=right><a href=\"$PHP_SELF?offset=$offset\">Previous $limit Articles</a></p>"; } %>

« previous php.db (#3365) next »