Re: Logic Problem with Next/Previous multiple listing from DB.
| From: | Joe Stump | Date: | Thu, 05 Oct 2000 21:03:20 +0000 |
| Subject: | Re: Logic Problem with Next/Previous multiple listing from DB. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-18768@lists.php.net to get a copy of this message | ||
You can check out my PHP Simple News - check out the logic in news.inc
http://www.miester.org/software
I usually do this:
<?
$NEWS_LIMIT = 10;
// use strlen because empty, isset, etc work funn sometimes...
if(!strlen($start)){
$start = 0;
}
$sql = "SELECT * FROM news LIMIT $start,$NEWS_LIMIT ORDER BY post_date DESC";
$r = mysql_query($sql);
while($row = mysql_fetch_array($r)){
// spit out news here
}
if($start >= $NEWS_LIMIT){
echo '<a href="'.$PHP_SELF.'?start='.($start -
$NEWS_LIMIT).'">'."\n";
echo '<< Previous '.$NEWS_LIMIT.' Entries</a>'."\n";
}
// get total number of news
$sql = "SELECT * FROM news";
$r = mysql_query($sql);
if(@mysql_num_rows($r) > ($start + $NEWS_LIMIT)){
echo '<a href="'.$PHP_SELF.'?start='.($start +
$NEWS_LIMIT).'">'."\n";
echo 'Next '.$NEWS_LIMIT.' Entries</a> >>'."\n";
}
?>
--Joe
On Thu, Oct 05, 2000 at 03:54:08PM -0500, James Montz wrote:
> 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> | </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>";
> }
> %>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
/*****************************\
* Joe Stump *
* www.Care2.com *
* Office: 650.328.0198 *
* Extension: 122 *
\*****************************/
http://www.miester.org/Joe/Stump/Joe_Stump_resume.html
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GB/E/IT d- s++:++ a? C++++ UL++$ P+ L+++$ E----! W+++$
N+@ o? K? w---! O-@ M+@ V-! P(++) PE(+) Y+@ PGP+++@ t+@
5? R-! tv@ b+ DI++@ D(++++) G++@ e+@ h@ r+! z(+++++**)!
------END GEEK CODE BLOCK------