Jump to content

[SOLVED] Weird issue with "if" / "else" function


David Nelson

Recommended Posts

Well, I have this snippet of code I wrote to look at what somebody has typed into an input box and yield different results depending on what they typed. I have the following file included on my page, oddly enough, it does nothing.. It doesn't change out the specified variables if they're inputted. Even more weird, it was working for me when I was only using 2 different 'if' functions, now that I have 5 I'm not sure what happened.  :-\ No errors come up either.

 

Thanks! I appreciate it.

 

 

<?php






//THIS FILE IS FOR THE BAND PIC IN THE SIDEBAR




//copy and paste entries to make more, just follow the patterns
//you can copy any entry except for the last one to use as a template
//for more, also, you cannot add these below the last entry, as that
//makes it no longer the last entry.





//entry 1 - if ivete sangalo is typed, this will apply.

if ($artist == "ivete sangalo") {
echo '

<img src=\"link to band image\">'; }


//entry 2 - if rammstein is typed, this will apply.

if ($artist == "rammstein") {
echo '

<img src=\"link to band image\">'; }


//entry 3 - if nickelback is typed, this will apply.


if ($artist == "nickelback") {
echo '

<img src=\"link to band image\">'; }

//entry 4 - if 3 doors down is typed, this will apply.


if ($artist == "3 doors down") {
echo '

<img src=\"link to band image\">'; }

//entry 5 - if sorriso maroto is typed, this will apply, because this is the last entry
//of the list, there differences in the code, so to produce more artists to modify, never
//copy and paste the last one as a template. Use any of the ones above this instead.

if ($artist == "sorriso maroto") {
echo '

<img src=\"link to band image\">';

}else{
include("bandpic.php?artist=$altartist");
}


?>

Link to comment
Share on other sites

How is it supposed to work?  Is the "else" supposed to apply to all "if" or just the last one?

 

For a start with the debugging, I would add this line at the top:

 

print "artist is $artist<br>";

 

That will at least tell you if $artist is set.

 

To assist with debugging, can you give us a few sets of input and output.  Eg "I type '3 doors down' as the artist, and I get a blank page"

Link to comment
Share on other sites

Aha.  Then you should write it like this:

 

//entry 1 - if ivete sangalo is typed, this will apply.

if ($artist == "ivete sangalo") {
echo '

<img src=\"link to band image\">'; }


//entry 2 - if rammstein is typed, this will apply.

elseif ($artist == "rammstein") {
echo '

<img src=\"link to band image\">'; }

 

The difference is the "elseif" instead of "if".  If you miss that out, then the final else will only apply to the final test.  You should find with your current code that the final "else" is being executed for all cases except when $artist = "sorriso maroto"

 

elseif should be added for all "if" except the first one.  The general structure is:

 

if (...) {
}
elseif (...) {
}
elseif (...) {
} else {
}

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.