[SOLVED] spot my incorrection

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

User avatar
buddok
Forum Commoner
Posts: 50
Joined: Tue May 25, 2004 2:44 pm

[SOLVED] spot my incorrection

Post 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 ?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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..
User avatar
buddok
Forum Commoner
Posts: 50
Joined: Tue May 25, 2004 2:44 pm

Post by buddok »

wat does that do :S ?
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

it will print out all the variables on each page so you can check where / if your formula is going wrong
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
User avatar
buddok
Forum Commoner
Posts: 50
Joined: Tue May 25, 2004 2:44 pm

Post by buddok »

ok im a newbie realy sowwi but i dont get that :(
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

did you try inserting that debugging code?

Show us the results.
User avatar
buddok
Forum Commoner
Posts: 50
Joined: Tue May 25, 2004 2:44 pm

Post by buddok »

yer it cam up with an error
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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
User avatar
buddok
Forum Commoner
Posts: 50
Joined: Tue May 25, 2004 2:44 pm

Post by buddok »

why do u have to be a smurfy smurf about it tim ?

feyd | smurfilized
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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?
User avatar
buddok
Forum Commoner
Posts: 50
Joined: Tue May 25, 2004 2:44 pm

Post 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 ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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)
User avatar
buddok
Forum Commoner
Posts: 50
Joined: Tue May 25, 2004 2:44 pm

Post 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);
?>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
Post Reply