Jump to content

nested if statements for accordion style menu


jrobles

Recommended Posts

I cant seem to put my finger on why this isnt working. I have a nested if statement that checks if there are sub categories in the menu_sub table. I am getting the following error

 

Notice: Trying to get property of non-object in C:\wamp\www\BSP\includes\left_nav.php on line 26

 

this is my code

 

//QUERY TO GET LEFT MENU ITEMS
$query="SELECT* FROM menu WHERE type=2 ORDER BY sort ASC";
$result= mysql_query($query);

echo"<div class=\"glossymenu\">";

if (mysql_num_rows($result)>0)
{while($row=mysql_fetch_object($result))
//echo "<a class='menuitem' href='$row->url'>$row->text</a>";


//QUERY TO GET SUB MENUS
$sub_query ="SELECT* FROM menu_sub WHERE menu_id = $row->menu_id";
$sub_result = mysql_query($sub_query);

if (mysql_num_rows($sub_result)>0)
{while($row_sub=mysql_fetch_object($sub_result))
echo "<a class='menuitem submenuheader' href='$row->url' >$row->title</a>
<div class='submenu'>
<li><a href='$row_sub->url'>$row_sub->text</a></li>
</ul>}
</div>";}
else
{echo "<a class='menuitem' href='$row->url'>$row->text</a>";}

}//CLOSING TAG FOR LINE 10
else
{echo"NO MENU ITEMS AVAILABLE";}

echo "</div>";

 

line 26 is my first else where i refer to the rw from the first query

{echo "<a class='menuitem' href='$row->url'>$row->text</a>";}

Link to comment
Share on other sites

On the line in question...

 

{echo "<a class='menuitem' href='$row->url'>$row->text</a>";}

 

... should the href not be $row_sub->url.

 

no, thats the else statement that outputs the regular button with no sub menus and the class of 'menuitem'

Link to comment
Share on other sites

My bad, didn't notice the second loop, I find your code very hard to read through at a glance because of the layout you have used and lack of syntax highlighting. Personally I would recommend using one of these formats for all language constructs.

 

<?php
if($something) {
   // code here
}
// or
if($something)
{
   // code here
}
?>

 

As far as I can tell the problem is actually caused quite a few lines earlier than stated. $row is not an object that is in scope at the point you are trying to use it. With a while block it will perform the code in the curly bracket set following it whilst the argument is true. There is a 'cheap hack' that means if you only have one line of code the curly brackets aren't required (but I highly recommend against using this). In your case though you commented out the single line of code and have not got brackets. This is essentially what your code looks like...

 

<?php
if (mysql_num_rows($result)>0) {
   while($row=mysql_fetch_object($result)) {
      // do nothing as you don't have any code as part of the while loop
   }
   // you are trying to use $row here, but it doesn't exist since the exit clause of the while
   // loop is $row being equal to FALSE
}
?>

Link to comment
Share on other sites

Hi

 

Think the issue might be around these lines:-

 

{while($row=mysql_fetch_object($result))

//echo "<a class='menuitem' href='$row->url'>$row->text</a>";

 

That looks like it is just looping round and echoing out that line (and only that line), but being commented out it will just loop round the next line instead ($sub_query ="SELECT* FROM menu_sub WHERE menu_id = $row->menu_id";).

 

Think you might need some extra curly brackets to get it to loop around the block of code rather than just a single line.

 

All the best

 

Keith

Link to comment
Share on other sites

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.