Mr.Canuck Posted April 29, 2010 Share Posted April 29, 2010 Hey everyone. I am a PHP novice and I was wondering if anyone can assist me with this if else statement. I don't have something right, because it doesn't work. So, if conditions are true, it won't display the <div> code and if the conditions are false, then it will display the <img> instead. Thanks. <?php $cpage = $_GET['cpage']; ?> <?php if ($cpage != 'intro' && $cpage !='directions' && $cpage !='hours' && $cpage !='jam' && $cpage !='pointe' && $cpage !='about' && $cpage !='tc' && $cpage !='privacy' && $xlspg !='contact_us') { ?> <div id="contain2"><?php $this->crumbTrail->Render(); ?></div> <?php else { <img class="img1" src="css/images/bcreplace.jpg" width="695" height="29" alt="bcreplace" /> } ?> Link to comment https://forums.phpfreaks.com/topic/200222-help-with-simple-if-else-statement/ Share on other sites More sharing options...
coupe-r Posted April 29, 2010 Share Posted April 29, 2010 You didn't close the bracket before the else clause. You have 1 open { at the end of your condition but didn't close it. Link to comment https://forums.phpfreaks.com/topic/200222-help-with-simple-if-else-statement/#findComment-1050723 Share on other sites More sharing options...
coupe-r Posted April 29, 2010 Share Posted April 29, 2010 Try this: <?php if ($cpage != 'intro' && $cpage !='directions' && $cpage !='hours' && $cpage !='jam' && $cpage !='pointe' && $cpage !='about' && $cpage !='tc' && $cpage !='privacy' && $xlspg !='contact_us') { ?> <div id="contain2"><?php $this->crumbTrail->Render(); ?></div> <?php }else { <img class="img1" src="css/images/bcreplace.jpg" width="695" height="29" alt="bcreplace" /> } ?> Link to comment https://forums.phpfreaks.com/topic/200222-help-with-simple-if-else-statement/#findComment-1050725 Share on other sites More sharing options...
Psycho Posted April 29, 2010 Share Posted April 29, 2010 You have no closing braket on the IF condition. But, I would clean that code up by doing this: <?php $cpage = $_GET['cpage']; $cpage_list = ('intro', 'directions', 'hours', 'jam', 'pointe', 'about', 'tc', 'privacy'); if (!in_array($cpage, $cpage_list) && $xlspg !='contact_us') { echo "<div id=\"contain2\">" . $this->crumbTrail->Render() . "</div>"; } else { echo "<img class=\"img1\" src=\"css/images/bcreplace.jpg\" width=\"695\" height=\"29\" alt=\"bcreplace\" />"; } ?> Link to comment https://forums.phpfreaks.com/topic/200222-help-with-simple-if-else-statement/#findComment-1050726 Share on other sites More sharing options...
Mr.Canuck Posted April 29, 2010 Author Share Posted April 29, 2010 Thanks for the replies guys. However, it does not seem to be working. If I run this code (without the "else") the page loads and it does what it is supposed to do for the "if". When I use the code above with the "else", the page does not load: <?php $cpage = $_GET['cpage']; ?> <?php if ($cpage != 'intro' && $cpage !='directions' && $cpage !='hours' && $cpage !='jam' && $cpage !='pointe' && $cpage !='about' && $cpage !='tc' && $cpage !='privacy' && $xlspg !='contact_us') { ?> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/200222-help-with-simple-if-else-statement/#findComment-1050730 Share on other sites More sharing options...
cweathers Posted April 29, 2010 Share Posted April 29, 2010 http://www.phpbuilder.com/manual/en/migration.if-endif.php Link to comment https://forums.phpfreaks.com/topic/200222-help-with-simple-if-else-statement/#findComment-1050732 Share on other sites More sharing options...
seventheyejosh Posted April 29, 2010 Share Posted April 29, 2010 http://www.phpbuilder.com/manual/en/migration.if-endif.php A different coding style isn't going to magically fix the fact that he has a missing bracket, it is just a different way. OP: Try using a syntax highlighting editor, to find your errors. Link to comment https://forums.phpfreaks.com/topic/200222-help-with-simple-if-else-statement/#findComment-1050734 Share on other sites More sharing options...
seventheyejosh Posted April 29, 2010 Share Posted April 29, 2010 You have no closing braket on the IF condition. But, I would clean that code up by doing this: <?php $cpage = $_GET['cpage']; $cpage_list = ('intro', 'directions', 'hours', 'jam', 'pointe', 'about', 'tc', 'privacy'); if (!in_array($cpage, $cpage_list) && $xlspg !='contact_us') { echo "<div id=\"contain2\">" . $this->crumbTrail->Render() . "</div>"; } else { echo "<img class=\"img1\" src=\"css/images/bcreplace.jpg\" width=\"695\" height=\"29\" alt=\"bcreplace\" />"; } ?> I think you used ( ) instead of " " on your string declaration: EDIT: I see it is an array.. just missed the "array" <?php $cpage = $_GET['cpage']; //$cpage_list = ('intro', 'directions', 'hours', 'jam', 'pointe', 'about', 'tc', 'privacy'); // unexpected ',' .... //$cpage_list = "'intro', 'directions', 'hours', 'jam', 'pointe', 'about', 'tc', 'privacy'"; // not supposed to be a string... $cpage_list = array('intro', 'directions', 'hours', 'jam', 'pointe', 'about', 'tc', 'privacy'); // much better if (!in_array($cpage, $cpage_list) && $xlspg !='contact_us') { echo "<div id=\"contain2\">" . $this->crumbTrail->Render() . "</div>"; } else { echo "<img class=\"img1\" src=\"css/images/bcreplace.jpg\" width=\"695\" height=\"29\" alt=\"bcreplace\" />"; } ?> Link to comment https://forums.phpfreaks.com/topic/200222-help-with-simple-if-else-statement/#findComment-1050736 Share on other sites More sharing options...
Mr.Canuck Posted April 29, 2010 Author Share Posted April 29, 2010 Thanks guys!! Seventheyejosh, you got it buddy! It works! Thanks Everyone! Link to comment https://forums.phpfreaks.com/topic/200222-help-with-simple-if-else-statement/#findComment-1050741 Share on other sites More sharing options...
seventheyejosh Posted April 29, 2010 Share Posted April 29, 2010 No problem. Like I said, syntax highlighting is amazing. I use Komodo Edit, but Notepad++, Eclipse, Coda, etc all do it. There is a thread in the MISC section I believe devoted solely to editors. I'd check it out Link to comment https://forums.phpfreaks.com/topic/200222-help-with-simple-if-else-statement/#findComment-1050742 Share on other sites More sharing options...
Mr.Canuck Posted April 29, 2010 Author Share Posted April 29, 2010 I'll do that. Thanks again Link to comment https://forums.phpfreaks.com/topic/200222-help-with-simple-if-else-statement/#findComment-1050743 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.