Jump to content

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!

 

:)

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.