j.taylor Posted November 6, 2009 Share Posted November 6, 2009 I have a menu that looks something like this: <div id="menu"> <ul> <?php gmv_menu(); ?> <?php if (isset($sub)) { echo "<li><a href=\"$link\">$location</a> > </li>\n"; echo "<li>map</li>\n"; } else { echo "<li>map</li>\n"; } ?> </ul> </div> Works fine as is. What I'm trying to do also, however, is wrap the whole thing in another if-else so that none of that code gets executed if the variable $link does not contain a certain string. Everything I try results in errors or incorrectly nested code (mainly because I don't know what I'm doing). Not even sure if my current approach is the best one. As some background info: This code is part of a breadcrumb menu which kinda becomes useless when the page is linked to from another domain since it uses ['HTTP_REFERER'] to contruct part of the breadcrumb. So, when $link = "someotherdomain" I'd like to not build the breadcrumb menu and just print out the name of another variable instead. Hope that makes sense? Any ideas? Link to comment https://forums.phpfreaks.com/topic/180591-help-with-if-else/ Share on other sites More sharing options...
JJ2K Posted November 6, 2009 Share Posted November 6, 2009 Like This? if($variable = "somestring"){ if (isset($sub)){ echo "<li><a href=\"$link\">$location</a> > </li>\n"; echo "<li>map</li>\n"; } else{ echo "<li>map</li>\n"; } } If $variable = some string it goes to the if else statement, if not it does nothing. Link to comment https://forums.phpfreaks.com/topic/180591-help-with-if-else/#findComment-952775 Share on other sites More sharing options...
j.taylor Posted November 6, 2009 Author Share Posted November 6, 2009 Almost working. Cool, thanks. I can get it to work if $variable = "the URL of the page that called the map page". However, since $variable is just $_SERVER['HTTP_REFERER'] I'm wondering if I can make make it work when $varible =/= "my domain"? You can see the problem here - note that the second item in the menu is "forums" (that comes from the directory that this forum page resides in and not from my site), which is why I'm looking to just print out the map title instead. Same story with the third menu item. Link to comment https://forums.phpfreaks.com/topic/180591-help-with-if-else/#findComment-952798 Share on other sites More sharing options...
JJ2K Posted November 6, 2009 Share Posted November 6, 2009 Sorry not to sure what you mean here? wondering if I can make make it work when $varible =/= "my domain"? Link to comment https://forums.phpfreaks.com/topic/180591-help-with-if-else/#findComment-952820 Share on other sites More sharing options...
j.taylor Posted November 7, 2009 Author Share Posted November 7, 2009 The variable in question contains the full url of the referring page. I was trying to determine if the string contained within that variable was a url from my own domain or was from another site. (=/= was shorthand for "does not equal"). I've done some more work and it now works perfectly. The only trouble now is that code seems horribly bloated. I've went from two lines of code in my very first menu to 9 lines in the code I posted earlier and now to this crazyness: <?php if(strstr($link,'oceandots') && (isset($sub))){ echo "<div id=\"menu_large\">\n"; echo "<ul>\n"; gmv_menu(); echo "<li><a href=\"$link\">$place</a> > </li>\n"; echo "<li>location map</li>\n"; echo "</ul>\n"; echo "</div>\n"; echo "</div>\n"; } elseif (strstr($link,'oceandots') && (is_null($sub))){ echo "<div id=\"menu_large\">\n"; echo "<ul>\n"; gmv_menu(); echo "<li>location map</li>\n"; echo "</ul>\n"; echo "</div>\n"; echo "</div>\n"; } elseif(strstr($link,'oceandots') === false && (isset($sub))){ echo "<div id=\"menu_large\">\n"; echo "<ul>\n"; echo "<li>$place ></li>\n"; echo "<li>location map</li>\n"; echo "</ul>\n"; echo "</div>\n"; echo "</div>\n"; } elseif(strstr($link,'oceandots') === false && (is_null($sub))){ echo "<div id=\"menu_large\">\n"; echo "<ul>\n"; echo "<li>$place ></li>\n"; echo "<li>location map</li>\n"; echo "</ul>\n"; echo "</div>\n"; echo "</div>\n"; } ?> As I said at the top, this works and the problem I had no longer exists. But I can't help but feel I've taken an awfully long-winded approach to do something relatively straightforward. Link to comment https://forums.phpfreaks.com/topic/180591-help-with-if-else/#findComment-952908 Share on other sites More sharing options...
JJ2K Posted November 7, 2009 Share Posted November 7, 2009 Well couldn't you take the common information out and put them at the top and bottom like: <?php echo "<div id=\"menu_large\">\n"; echo "<ul>\n"; if(strstr($link,'oceandots') && (isset($sub))){ gmv_menu(); echo "<li><a href=\"$link\">$place</a> > </li>\n"; echo "<li>location map</li>\n"; } elseif (strstr($link,'oceandots') && (is_null($sub))){ gmv_menu(); echo "<li>location map</li>\n"; } elseif(strstr($link,'oceandots') === false && (isset($sub))){ echo "<li>$place ></li>\n"; echo "<li>location map</li>\n"; } elseif(strstr($link,'oceandots') === false && (is_null($sub))){ echo "<li>$place ></li>\n"; echo "<li>location map</li>\n"; } echo "</ul>\n"; echo "</div>\n"; echo "</div>\n"; ?> But this is assuming one of the conditions is always met whatever happens? Also your last 2 cases output the same so could you not combine them like: <?php echo "<div id=\"menu_large\">\n"; echo "<ul>\n"; if(strstr($link,'oceandots') && (isset($sub))){ gmv_menu(); echo "<li><a href=\"$link\">$place</a> > </li>\n"; echo "<li>location map</li>\n"; } elseif (strstr($link,'oceandots') && (is_null($sub))){ gmv_menu(); echo "<li>location map</li>\n"; } elseif(strstr($link,'oceandots') === false && ((isset($sub)) || (is_null($sub)) ){ echo "<li>$place ></li>\n"; echo "<li>location map</li>\n"; } echo "</ul>\n"; echo "</div>\n"; echo "</div>\n"; ?> That shortens the exisiting code, but as for the logic I have a feeling it could probably be a bit better. How exactly does this breadcrumb menu work? Why would you be displaying navigation based on the refferer anyhow, unless it was a back button? Why not just display the breadcrumb as it is in your site regardless of where the user has come from? e.g check out the breadcrumb for this particular topic on this site: * PHP Freaks Forums > * PHP Coding > * PHP Coding Help > * Post reply ( Re: help with "if else" ) It will always be this regardless of the refferer. Link to comment https://forums.phpfreaks.com/topic/180591-help-with-if-else/#findComment-952932 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.