spacepoet Posted April 28, 2011 Share Posted April 28, 2011 Hello: I am using this code to try and display one of two statements: <?php $query = mysql_query("SELECT menuID,menuTitle FROM theOfficeMenus"); if(trim($menuData["menuTitle"]) == "") { echo "<span class=\"myBold\">Currently there are no Menus</span>"; } else { echo "<span class=\"myBold\">Current Menus</span>"; } while($menuData = mysql_fetch_array($query)) { echo "<li style=\"margin: 0 0 20px 0;\"> <a href=\"a_MenusDelete.php?menuID=".$menuData['menuID']."\" onclick=\"return confirm('Are you sure you want to delete this Menu? There is NO undo! Click OK to continue deleting, or CANCEL to stop deleting.');\">Delete Menu</a> || <a href=\"a_MenusEdit.php?menuID=".$menuData['menuID']."\"><span class=\"myBold\">". $menuData['menuTitle'] ."</span></a></li>"; } ?> If "menuTitle" is empty display "Currently there are no Menus" , if it is not empty display "Current Menus" However ever, it keeps displaying only the first statement. Why ?? Link to comment https://forums.phpfreaks.com/topic/234946-if-else-not-working-why/ Share on other sites More sharing options...
doddsey_65 Posted April 28, 2011 Share Posted April 28, 2011 $menuData isnt set when you are using it in the statements. Link to comment https://forums.phpfreaks.com/topic/234946-if-else-not-working-why/#findComment-1207412 Share on other sites More sharing options...
cyberRobot Posted April 28, 2011 Share Posted April 28, 2011 To clarify, $menuData isn't set until your while loop. Instead of using $menuData, you should look into mysql_num_rows() http://php.net/manual/en/function.mysql-num-rows.php You could change the if/else to say if(no rows found) { display message } else { run through while loop and display menus } Link to comment https://forums.phpfreaks.com/topic/234946-if-else-not-working-why/#findComment-1207532 Share on other sites More sharing options...
spacepoet Posted April 28, 2011 Author Share Posted April 28, 2011 Excellent! Got it: <?php $query = mysql_query("SELECT menuID,menuTitle FROM theOfficeMenus"); if(mysql_num_rows($query) == 0) { echo "<span class=\"myBold\">Currently there are no Menus</span>"; } else { echo "<span class=\"myBold\">Current Menus</span>"; } while($menuData = mysql_fetch_array($query)) { echo "<li style=\"margin: 0 0 20px 0;\"> <a href=\"a_MenusDelete.php?menuID=".$menuData['menuID']."\" onclick=\"return confirm('Are you sure you want to delete this Menu? There is NO undo! Click OK to continue deleting, or CANCEL to stop deleting.');\">Delete Menu</a> || <a href=\"a_MenusEdit.php?menuID=".$menuData['menuID']."\"><span class=\"myBold\">". $menuData['menuTitle'] ."</span></a></li>"; } ?> Thanks for the tip! Link to comment https://forums.phpfreaks.com/topic/234946-if-else-not-working-why/#findComment-1207685 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.