Jump to content

Recommended Posts

Hi,

 

I am building a dynamic menu that is driven from a mysql database for a Contant Managed site. The menu is a drop down smooth menu from dynamic drive. It all works fine, apart from the submenu items which expand regardless of having data or not.  I have made a comment where i want to insert an if statment saying if the mysql query result in the $result2 array is null then skip out of the loop. I tried an if (isset ($result2)) statement, but obvioulsy the $result2 array always has data. the problem i am getting is with this menu, if the sub menu items are assigned a <ul> tag i the sub menu expands once but is empty. I hope this makes sense.

 

Following is the menu code so far:

 

<ul>
<?php 
//$root = "http://www.soaringachievements.com.au/newsite/rhg.php";
$root = "http://localhost/NewBiz Solutions/Soaring Achievements/website/index.php";

$sql1 = "SELECT * FROM main_menu
   ORDER BY position";
$result1 = $db->query($sql1);

while ($row1 = $result1->fetch())
{

echo '<li><a href="' . $root .'?page='. $row1['page_ref'] . '">' . $row1['title'] . '</a>';

$ref = $row1["page_ref"];

$sql2 = "SELECT * FROM sub_menu
   WHERE catergory = '$ref'
   ORDER BY position";
   $result2 = $db->query($sql2);
   
   //Trying to see if mysql query in $reslut2 array has data prior to next line. If there is no data i want it so skip out of this loop.
   
   //if (isset($result2))
  //{
echo "<ul>";
while ($row2 = $result2->fetch())	
{
  	echo '<li><a href="' . $root .'?page='. $row2['page_ref'] . '">' . $row2['title'] . '</a></li>';
    }
  	echo "</ul>";
	//}
  ?>

</li>
<?php 
}
?> 
</ul>
<br style="clear: left" />

Link to comment
https://forums.phpfreaks.com/topic/168933-solved-read-array-prior-to-fetch/
Share on other sites

You'll want to check if your query returns any rows before your while loop. Something like

 

     if ($db->num_rows() != 0)
     {
   echo "<ul>";
   while ($row2 = $result2->fetch())   
   {
     echo '<li><a href="' . $root .'?page='. $row2['page_ref'] . '">' . $row2['title'] . '</a></li>';
    }
     echo "</ul>";
      }

 

$db->num_rows() may not be correct. As I don't know your database class I cannot suggest the proper function to use. Your class should have a function that returns the number of results from a query, eg mysql_num_rows

Thankyou!! You are a life saver, got in 2 mins what took me 2 hours last night!! The following works beautifully, size() was the function in the database class.  Thanks again wildteen88!!

 

   if ($result2->size() != 0)
  {
echo "<ul>";
while ($row2 = $result2->fetch())	
{
  	echo '<li><a href="' . $root .'?page='. $row2['page_ref'] . '">' . $row2['title'] . '</a></li>';
    }
  	echo "</ul>";
	}

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.