Page 1 of 2

[SOLVED] spot my incorrection

Posted: Sun Aug 29, 2004 7:54 am
by buddok
hey well im using this bit of code in my pagination and it conintues to link to the next page even though theres no more data ?

Code: Select all

<?php
    if(($totalrows % $limit) != 0){
        if($i == $page){
            echo($i."&nbsp;");
        }else{
            echo("<a href="$PHP_SELF?page=$i">$i</a>&nbsp;");
        }
    }

    if(($totalrows - ($limit * $page)) >= 0){
        $pagenext = $page+1;
         
        echo("<a href="$PHP_SELF?page=$pagenext"> [N] </a>");
    }else{
        echo("[N]");
    }
?>
anyone got n e ideas why becuase i realy cant see it ?

Posted: Sun Aug 29, 2004 8:49 am
by markl999
Add some debugging code:

Code: Select all

echo 'Totalrows: '.$totalrows.'<br />';
echo 'Limit: '.$limit.'<br />';
echo 'Page: '.$page.'<br />';
echo 'Calc: '.($totalrows - ($limit * $page)).'<br />';
if(($totalrows - ($limit * $page)) >= 0){
//rest of your code here..

Posted: Sun Aug 29, 2004 9:25 am
by buddok
wat does that do :S ?

Posted: Sun Aug 29, 2004 9:27 am
by malcolmboston
it will print out all the variables on each page so you can check where / if your formula is going wrong

Posted: Sun Aug 29, 2004 9:41 am
by markl999
If you're seeing the next page link then if(($totalrows - ($limit * $page)) >= 0){ must be true, which means one of the values in that calculation isn't what you think it is, so by echoing them out you can see where it's going wrong.

Posted: Sun Aug 29, 2004 10:39 am
by buddok
ok im a newbie realy sowwi but i dont get that :(

Posted: Sun Aug 29, 2004 12:58 pm
by John Cartwright
did you try inserting that debugging code?

Show us the results.

Posted: Sun Aug 29, 2004 1:08 pm
by buddok
yer it cam up with an error

Posted: Sun Aug 29, 2004 1:37 pm
by tim
buddok wrote:yer it cam up with an error
well, hmm...


lets think.....

- thinking.....

......thinking -

wouldnt you think it would be useful for us to know that error?

Help us help you

Posted: Sun Aug 29, 2004 1:41 pm
by buddok
why do u have to be a smurfy smurf about it tim ?

feyd | smurfilized

Posted: Sun Aug 29, 2004 1:43 pm
by tim
well phen asked:

"Show us the results"

We are trying to be nice by helping you, and you must have been caught in a "duh" moment b/c posting "yeah I got an error, but I am not going to tell you it."

Can you please post the error you got?

Posted: Sun Aug 29, 2004 1:56 pm
by buddok
no because after i tried it an it didnt work i got rid of it is there no obvious incorrections in my script ? how is it usualy done ?

Posted: Sun Aug 29, 2004 2:15 pm
by feyd
your code is highly dependant on variables.. we need to know what those variables are, in detail, before we can make an accurate assessment. if the code mark was kind enough to spend the time to write out for you was added in the proper place.. it should work without any problems. (that code, not necessarily yours)

Posted: Sun Aug 29, 2004 2:47 pm
by buddok

Code: Select all

<?php
  @mysql_connect("localhost", "user", "pass") or die("ERROR--CAN'T CONNECT TO SERVER");
    @mysql_select_db("db") or die("ERROR--CAN'T CONNECT TO DB");

    $limit          = 2;               
    $query_count    = "SELECT * FROM news";    
    $result_count   = mysql_query($query_count);    
    $totalrows      = mysql_num_rows($result_count);

    if(empty($page)){
        $page = 1;
    }
        

    $limitvalue = $page * $limit - ($limit);
    $query  = "SELECT * FROM news order by id desc LIMIT $limitvalue, $limit";        
    $result = mysql_query($query) or die("Error: " . mysql_error());

    if(mysql_num_rows($result) == 0){
        echo("Nothing to Display!");
    }

    $bgcolor = "#E0E0E0"; // light gray

    echo("<table class='notable'>");
    
    while($row = mysql_fetch_array($result)){


   $date=$row["dt"]; 
   $comments=$row["comments"];


$comments = str_replace(";)", "<img src='smilies/wink.gif'>", $comments);
$comments = str_replace(":)", "<img src='smilies/happy.gif'>", $comments);
$comments = str_replace(":(", "<img src='smilies/sad.gif'>", $comments);
$comments = str_replace(":O", "<img src='smilies/shocked.gif'>", $comments);
$comments = str_replace(":D", "<img src='smilies/grin.gif'>", $comments);
$comments = str_replace(":P", "<img src='smilies/tongue.gif'>", $comments);
$comments = str_replace(":@", "<img src='smilies/mad.gif'>", $comments);
$comments = str_replace("8-)", "<img src='smilies/confused.gif'>", $comments);
$comments = str_replace(":'(", "<img src='smilies/cry.gif'>", $comments);
$comments = str_replace(":S", "<img src='smilies/sick.gif'>", $comments);


print("

<ul class='dots'>
<li>
$comments
</li>
</ul>
<p class='para' style='margin-left:450; margin-right:0;'>On : <font style='font-size:9'><b>$date</b></font>
<br><br><br><br><br>

"); 





    }







    echo("</table>");

    if($page != 1){
        $pageprev = $page-1;
        
        echo("<a href="$PHP_SELF?page=$pageprev"> [P] </a>&nbsp;");
    }else{
        echo("[P] &nbsp;");
    }

    $numofpages = $totalrows / $limit;
    
    for($i = 1; $i <= $numofpages; $i++){
        if($i == $page){
            echo($i."&nbsp;");
        }else{
            echo("<a href="$PHP_SELF?page=$i">$i</a>&nbsp;");
        }
    }


    if(($totalrows % $limit) != 0){
        if($i == $page){
            echo($i."&nbsp;");
        }else{
            echo("<a href="$PHP_SELF?page=$i">$i</a>&nbsp;");
        }
    }

    if(($totalrows - ($limit * $page)) >= 0){
        $pagenext = $page+1;
         
        echo("<a href="$PHP_SELF?page=$pagenext"> >> </a>");
    }else{
        echo("[N]");
    }
    
    mysql_free_result($result);
?>

Posted: Sun Aug 29, 2004 8:48 pm
by markl999
Ok, i'll try again.
If you're seeing the next page link when you shouldn't be doing then it means if(($totalrows - ($limit * $page)) >= 0){ is true when you expect it not to be.
So that means the variables probably are not what you think they are, you can find out what they are by echo'ing them out, such as:

Code: Select all

echo 'Totalrows: '.$totalrows.'<br />';
echo 'Limit: '.$limit.'<br />';
echo 'Page: '.$page.'<br />';
echo 'Calc: '.($totalrows - ($limit * $page)).'<br />';
if(($totalrows - ($limit * $page)) >= 0){
//rest of your code here..
By doing this you will see what the values of the variables are and why the calculation is returning true instead of false.