[SELF SOLVED!]With class=+2 without=+1

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
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

[SELF SOLVED!]With class=+2 without=+1

Post by blacksnday »

In my attempt to transfer my code to classes
I ran across this annoying error.

To reference the DB class I am using you can view it at
http://www.phpbuilder.com/snippet/downl ... et&id=1966

The problem is without using the DB class, my pagination correctly
counts +1 and grabs the next article.
With the db class, pagination link correctly counts +1 but
the article displayed is actually +2 so it skips an article on every
next display.

The control to determine how many to show on the page is
$rows_per_page but if I set it to 1 using the DB class
then it won't show anything, and if set to 2 it only displays one
but counting two, hence the next article being skipped when
clicking the next link.

I can't figure out how to solve this and ask for any skilled
eyes to point out where the problem lies...

Below is two codes.
First one is with using the DB Class
Second one is without the DB Class and the one that
correctly counts and displays +1
The DB Class version is soooooo much cleaner
and I really want to use it if this problem can be solved.

Code: Select all

$query        = "select * from news where published='1' order by id desc "; 
$result        = $sql->query($query); 
$screen 	   = $_GET['view']; 
$PHP_SELF = $_SERVER['PHP_SELF']; 

$rows_per_page	= 2; 
$total_records	= $sql->totalrows;
$pages 		= ceil($total_records / $rows_per_page); 

if (!isset($screen)) 
$screen	= 0; 
$start 	= $screen * $rows_per_page; 
$query    .= "LIMIT $start, $rows_per_page"; 
$result	= $sql->query($query); 

while($sql->next_row())
{ 
if ($screen > 0) { 
$j = $screen - 1; 
echo "<a href=\"$PHP_SELF?view=$j\">Prev</a>"; 
} 
if ($screen < $pages) { 
$j = $screen + 1; 
echo "<a href=\"$PHP_SELF?view=$j\">Next</a>"; 
}
 
$title	      = submitTags($sql->row[8], 1, 1, $siteurl, $directory, $imgalt, $vf);
$name	      = submitTags($sql->row[4], 1, 1, $siteurl, $directory, $imgalt, $vf);
$news	      = submitTags($sql->row[7], 1, 1, $siteurl, $directory, $imgalt, $vf);
$email	      = submitTags($sql->row[2], 1, 1, $siteurl, $directory, $imgalt, $vf);

echo "
Title: {$title}<br />
Submitted by: {$name}<br />
{$news}<br />
";
}
Same code without using the DB Class

Code: Select all

$query = "select * from news where published='1' order by did desc "; 
$result = mysql_query($query) or die(mysql_error()); 

$screen      = $_GET['view']; 
$PHP_SELF = $_SERVER['PHP_SELF']; 

$rows_per_page = 1; 
$total_records     = mysql_num_rows($result); 
$pages                = ceil($total_records / $rows_per_page); 

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

while ($row = mysql_fetch_array($result)) 
{ 

if ($screen > 0) { 
$j = $screen - 1; 
$url = "$PHP_SELF?view=$j"; 
echo "<a href=\"$url\">Prev</a>"; 
} 

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

$newposts = array ( "Title:" => $row['lova'], "Submitted By" => $row['name'], "News" => $row['news'] );  
    foreach ( $newposts as $postfield => $postinput ) 
    { 
      $postinput = submitTags($postinput, 1, 1, $siteurl, $directory, $imgalt, $vf); 
      echo "<b>{$postfield}</b>  {$postinput}<br />"; 
    }
  }
Last edited by blacksnday on Fri Nov 18, 2005 8:16 pm, edited 1 time in total.
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

Aha! i got it...
I shouldve put the while after the first output.
figured it out by studying the class from the above link :wink:

Code: Select all

if ($screen > 0) { 
$j = $screen - 1; 
echo "<a href=\"$PHP_SELF?view=$j\">Prev</a>"; 
} 
if ($screen < $pages) { 
$j = $screen + 1; 
echo "<a href=\"$PHP_SELF?view=$j\">Next</a>"; 
} 

$title          = submitTags($sql->row[8], 1, 1, $siteurl, $directory, $imgalt, $vf); 
$name          = submitTags($sql->row[4], 1, 1, $siteurl, $directory, $imgalt, $vf); 
$news          = submitTags($sql->row[7], 1, 1, $siteurl, $directory, $imgalt, $vf); 
$email          = submitTags($sql->row[2], 1, 1, $siteurl, $directory, $imgalt, $vf); 

echo " 
Title: {$title}<br /> 
Submitted by: {$name}<br /> 
{$news}<br /> 
";
while($sql->next_row()) 
{ 
}
Post Reply