whiskedaway Posted August 29, 2010 Share Posted August 29, 2010 I have a series of PHP files that are referenced in index.php like this: <?php include("credentials.inc"); $cxn = mysqli_connect($host, $user, $password, $dbname) or die ("Connection failed."); include('header.php'); include('menu.php'); include('contact.php'); include('home.php'); ?> One of my files, menu.php, brings up a JavaScript menu that was coded in Dreamweaver (yes, I cheated). However, it seems to get to the first "for" statement and stops. I can't work out what I've done wrong. <?php $h1 = mysqli_query($cxn, "SELECT areaID, area FROM area ORDER BY areaID"); echo("<ul id='MenuBar1' class='MenuBarHorizontal'>"); echo("<li><a href='index.php'>HOME</a></li>"); echo("<li><a href='#' class='MenuBarItemSubmenu'>PRODUCTS</a><ul>"); for ($a=0;$a<=count($h1);$a++) { echo("<li><a href='#' class='MenuBarItemSubmenu'>".$h1[$a]['area']."</a><ul>"); $h2 = mysqli_query($cxn, "SELECT catID, category, areaID, link FROM category WHERE areaID = '".$h1[$a]['areaID']."' ORDER BY catID"); for ($b=0;$b<count($h2);$b++) { echo("<li><a href='".$h2[$b]['link']."'>".$h2[$b]['category']."</a></li>"); } echo("</ul></li>"); } echo("</ul></li>"); echo("<li><a href='services.php'>SERVICES</a></li>"); echo("<li><a href='aboutus.php'>ABOUT US</a></li>"); echo("<li><a href='contact.php'>CONTACT</a></li></ul>"); ?> Hopefully someone has an idea of where the problem is. Thanks so much in advance! Quote Link to comment Share on other sites More sharing options...
joel24 Posted August 29, 2010 Share Posted August 29, 2010 are the services, about us and contact buttons echoed? It seems your for loop is not executing, try putting this and see if any errors are echoed. $h1 = mysqli_query($cxn, "SELECT areaID, area FROM area ORDER BY areaID") OR die(mysqli_error($cxn)); Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 29, 2010 Share Posted August 29, 2010 You're not processing the results of your queries properly. mysqli_query() only returns a result resource. It doesn't return a multi-dimensional array of results. To grab the results from your query you'll want to use one of the MySQLi Result functions, such as mysqli_fetch_assoc(). You'd then use a while loop to loop through the results. As per the (procedural style) example for mysqli_fetch_assoc here So your code should be <?php $area_result = mysqli_query($cxn, "SELECT areaID, area FROM area ORDER BY areaID"); echo("<ul id='MenuBar1' class='MenuBarHorizontal'>"); echo("<li><a href='index.php'>HOME</a></li>"); echo("<li><a href='#' class='MenuBarItemSubmenu'>PRODUCTS</a><ul>"); while( $h1 = mysqli_fetch_assoc($area_result) ) { echo("<li><a href='#' class='MenuBarItemSubmenu'>".$h1['area']."</a><ul>"); $category_result = mysqli_query($cxn, "SELECT catID, category, areaID, link FROM category WHERE areaID = '".$h1['areaID']."' ORDER BY catID"); while($h2 = mysqli_fetch_assoc($category_result)) { echo("<li><a href='".$h2['link']."'>".$h2['category']."</a></li>"); } echo("</ul></li>"); } echo("</ul></li>"); echo("<li><a href='services.php'>SERVICES</a></li>"); echo("<li><a href='aboutus.php'>ABOUT US</a></li>"); echo("<li><a href='contact.php'>CONTACT</a></li></ul>"); ?> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted August 29, 2010 Share Posted August 29, 2010 Edit: Basically the same as what WT88 wrote ^^^ mysqli_query() returns a result set that you must fetch data from, assuming your query returned any matching rows. There are a family of mysqli_fetch_xxxxx() statements you can use to do this. Your variables $h1 and $h2 are not data. You cannot directly use count() or access data elements in them. I recommend reading a mysqli tutorial or the examples in the mysqli section of the php documentation to learn how you would execute a query and fetch the data it returns. Quote Link to comment Share on other sites More sharing options...
whiskedaway Posted August 30, 2010 Author Share Posted August 30, 2010 Thanks, for that, it's all working now. I suppose I should have known that, but I haven't used mysqli for a while (have been using query() instead) and have forgotten everything. Quote Link to comment 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.