newbiz Posted August 5, 2009 Share Posted August 5, 2009 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" /> Quote Link to comment https://forums.phpfreaks.com/topic/168933-solved-read-array-prior-to-fetch/ Share on other sites More sharing options...
wildteen88 Posted August 5, 2009 Share Posted August 5, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/168933-solved-read-array-prior-to-fetch/#findComment-891311 Share on other sites More sharing options...
newbiz Posted August 5, 2009 Author Share Posted August 5, 2009 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/168933-solved-read-array-prior-to-fetch/#findComment-891752 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.