yandoo Posted September 10, 2007 Share Posted September 10, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/68719-solved-if-statement-not-worknig-why/ Share on other sites More sharing options...
GingerRobot Posted September 10, 2007 Share Posted September 10, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/68719-solved-if-statement-not-worknig-why/#findComment-345442 Share on other sites More sharing options...
yandoo Posted September 10, 2007 Author Share Posted September 10, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/68719-solved-if-statement-not-worknig-why/#findComment-345457 Share on other sites More sharing options...
Psycho Posted September 10, 2007 Share Posted September 10, 2007 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/68719-solved-if-statement-not-worknig-why/#findComment-345487 Share on other sites More sharing options...
sasa Posted September 10, 2007 Share Posted September 10, 2007 $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 Quote Link to comment https://forums.phpfreaks.com/topic/68719-solved-if-statement-not-worknig-why/#findComment-345524 Share on other sites More sharing options...
yandoo Posted September 10, 2007 Author Share Posted September 10, 2007 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 ?? Quote Link to comment https://forums.phpfreaks.com/topic/68719-solved-if-statement-not-worknig-why/#findComment-345588 Share on other sites More sharing options...
yandoo Posted September 11, 2007 Author Share Posted September 11, 2007 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 Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/68719-solved-if-statement-not-worknig-why/#findComment-345658 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.