Jump to content

If Else not working .. why .. ??


spacepoet

Recommended Posts

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

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

}

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!

 

:)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.