Help - Search - Members - Calendar
Full Version: The Case Of The Misbehaving If-else Statement
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
kevin_k
Hi Everybody:

I have some code that displays buttons dynamically based on the state of session variables. The "First" button is almost exactly the same as the "Prev" button except for the image names but the "First" button's code isn't working right. Here's the code. Note, the debug statements were put in to address this problem.

CODE

<?php
echo "Debug: comic_b2: session pos: " . $_SESSION['comicpos'] .
"<br />";
echo "Debug: comic_b2: session max: " . $_SESSION['comicmax'] .
"<br /";

// Dynamic comic button display code.
// First button.
// Set button off or on.
// First button.
// Set button off or on.
if ($_SESSION['comicpos'] == $_SESSION['comicmax']) {
echo "<img src=\"images/firstoff.png\" width=\"21\"
height=\"20\" border=\"0\" title=\"First\"
alt=\"first button\" /> ";
} else {
echo "<a href=\"comic-nav.php?act=first\"><img
src=\"images/firston.png\" width=\"21\" height=\"20\"
border=\"0\" title=\"First\"
alt=\"first button\" /></a> ";
}

// Previous button.
// Set button off or on.
if ($_SESSION['comicpos'] == $_SESSION['comicmax']) {
echo "<img src=\"images/prevoff.png\" width=\"21\"
height=\"20\" border=\"0\" title=\"Previous\"
alt=\"prev button\" /> ";
} else {
echo "<a href=\"comic-nav.php?act=prev\"><img
src=\"images/prevon.png\" width=\"21\" height=\"20\"
border=\"0\" title=\"Previous\"
alt=\"prev button\" /></a> ";
}

// Next button.
// Set button off or on.
if ($_SESSION['comicpos'] == 0) {
echo "<img src=\"images/nextoff.png\" width=\"21\"
height=\"20\" border=\"0\" title=\"Next\"
alt=\"next button\" /> ";
} else {
echo "<a href=\"comic-nav.php?act=next\"><img
src=\"images/nexton.png\" width=\"21\" height=\"20\"
border=\"0\" title=\"Next\"
alt=\"next button\" /></a> ";
}

// Current button.
// Set button off or on.
if ($_SESSION['comicpos'] == 0) {
echo "<img src=\"images/currentoff.png\" width=\"37\"
height=\"20\" border=\"0\" title=\"Current\"
alt=\"current button\" />";
} else {
echo "<a href=\"comic-nav.php?act=current\"><img
src=\"images/currenton.png\" width=\"37\"
height=\"20\" border=\"0\" title=\"Current\"
alt=\"current button\" /></a>";
}

?>


The problem is the very first if-else statement. You can see that it is functionally the same as the next if-else code block.

When $_SESSION['comicpos'] == $_SESSION['comicmax'] it is supposed to drop through and display a button that is gray and can't be clicked on. When they are NOT equal it is supposed to display a white button that can be clicked on.

Here's what happens:

When the two session variables are NOT equal it displays the white button but you can't click on it,and when they are equal it displays nothing. What in the world is going on here? The script that processes the button presses seems to be working as intended although I have some errant session variables that I have to track down. All this code displays a webcomic and you can step through all of the pages that have been released. Here is the order of the buttons for reference:

First Prev Next Current

I have not uploaded the current code to the host yet I always develop locally first and work out the bugs before going live. You can see an example of what I'm doing at comic.vndv.com. Note the buttons are not functional on this page.

As always, any help is greatly appreciated and you have my thanks in advance.
kevin_k
Hey All: Just a quick note. I re-wrote the file and changed a few things around and got it working. I'm still not sure what went wrong with the original code though - a programming mystery I guess. If anyone has any ideas about what happened I would still like to hear people's thoughts. Jeez, I seem to be posting a lot during this project...
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.