Jump to content

[SOLVED] If Statement Not Worknig, Why???


yandoo

Recommended Posts

Hi,

 

Im having trouble getting If statement to work. It begins i think at this line of code:

 

if($page != 1){ 
        $pageprev = $page--;

 

It doesnt seem to work as none of the other If statments that rely on the value created by this one work either!!! I think im just missing something really simple here.

 

The problem is asociated with pagnation. The result of the NOT WORKING iIf statements means that the NEXT and PREVIOUS navigation text appears (as it should do accordging to the ELSE) BUT Neither are hyperlinked????!!!!!  (which is what the If statment should be doing)

 

 

If you could please have a peak at my full code and lend me a hand that would be great. 

 

<?php
$dbservertype='mysql';
$servername='localhost';
// username and password to log onto db server
$dbusername='root';
$dbpassword='';
// name of database
$dbname='lopesarms';


connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
  error_reporting(E_ALL);
ini_set('display_errors', '1'); 

    $limit          = 5;               
    $query_count    = "SELECT count(*) FROM guest_book";    
    $result_count   = mysql_query($query_count);    
    $totalrows      = mysql_num_rows($result_count); 

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

    $limitvalue = $page * $limit - ($limit); 
    $query  = "SELECT * FROM guest_book 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>");
    
    while($row = mysql_fetch_array($result)){
        if ($bgcolor == "#E0E0E0"){
            $bgcolor = "#FFFFFF";
        }else{
            $bgcolor = "#E0E0E0";
        }

    echo("<tr bgcolor=".$bgcolor.">n<td>");
    echo($row["name"]);
    
    echo($row["email"]);
    echo("</td></tr>");
    }

    echo("</table>");

    if($page != 1){ 
        $pageprev = $page--;
        


     //   echo("<a href=\"$self?page=$pageprev\">PREV".$limit."</a> "); 
        echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> "); 
    }else{
        echo("PREV".$limit." ");
    }

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


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

    if(($totalrows - ($limit * $page)) > 0){
        $pagenext = $page++;
         



        echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>");

    }else{
        echo("NEXT".$limit); 

    }
    
    mysql_free_result($result);

?>

 

Thank you

Link to comment
Share on other sites

With just a quick glance, i would guess your problem is actually with this piece of code:

 

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

 

Unless you have register_globals turned on, $page will always be undefined. You need to retrieve it from the $_GET array. Replace the above code with:

 

$page = (!isset($_GET['page'])) ? 1 : $_GET['page'];

 

And give it a whirl.

Link to comment
Share on other sites

Hi,

 

Firstly thank you for your help its much appreciated as im feeling slighty out of my depth here now.

 

the gloabal registers were actually set to On. I also tried the line of code you suggested and it hasnt made any change to the result???

 

What could it be then????

 

Thank You

 

Tom

 

 

 

Link to comment
Share on other sites

Debugging a simple IF statement such as that is a very easy process. Simply use a couple echo commands to determine what is heppening.

 

<?php

echo "Page value before IF: '$page' <br>";
if($page != 1){ 
        $pageprev = $page--;

echo "Page value after IF: '$page' <br>";

?>

Link to comment
Share on other sites

$pageprev = $page--;

do:

1st assign value of variable $page to variable $pageprev

2nd AFTER that change value of variable $page

 

if $page = 7

after $pageprev = $page--;

you got

$pageprev = 7 and $page = 6 !!!

 

you want $pageprev = $page - 1;

if $page = 7

after $pageprev = $page - 1;

you got

$pageprev = 6 and $page = 7

Link to comment
Share on other sites

Ive tried a bit of debugging on the statments for variables $limit and $page. They seem to be inorder, yet the PREV and NEXT navigation text is stil not hyperlinked???

 

Could it be that infact the staements are actually parsing and theres a problem with the PREV and NEXT    hyperlink syntax????

 

I have also tried the turning around the IF $pageprev = $page code to:

      $pageprev = $page-1;
      if($page != 1){   

 

It hasnt either made and change...

 

Anymore suggestions please ;D ??

Link to comment
Share on other sites

Ive done it! Whata a nightmare though.

 

Hers what i did:

 

Change all the $page++ to +1 or -1

 

Swapped around order to this:

 

$pageprev = $page-1;

      if($page != 1){

 

Changed the following variable calculations from:

 

$numofpages = $totalrows / $limit;

if(($totalrows % $limit) != 0){

if(($totalrows - ($limit * $page)) > 0){

 

To:

 

$numofpages =  $limit / $totalrows;

if(( $limit % $totalrows) != 0){

if(($limit * $page) - ($totalrows) > 0){

 

 

And thats it... Im realy chuffed  :D

 

Thanks for the help

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.