Jump to content

Problem with for statement


whiskedaway

Recommended Posts

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!

Link to comment
Share on other sites

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));

 

Link to comment
Share on other sites

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>");

?>

Link to comment
Share on other sites

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.

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.