Forum Moderators: coopster

Message Too Old, No Replies

Menu not made correctly with PHP

Menu incomplete when generating them with a array

         

dbzfyam

9:20 am on Aug 5, 2005 (gmt 0)

10+ Year Member



I am working on a layout for someone which also wanted a sort of CMS. When I was working on the menu (which will be generated automatically from the database) I run into a problem. I have put all category names in a table in the database which I retreive and put in a array. With those, I generate the menu (which generates the links aswell). Instead of displaying all menu's, it gives the error Warning: Invalid argument supplied for foreach() in /home/narutouz/public_html/layout/menu.php on line 29. Can anyone tell me what I am doing wrong. The code I have so far:

<?php
include_once ("admin/database.php");

$connect = mysql_connect($dbhost,$dbuser,$dbpass) or die ("Couldn't connect to database.");
$selectdb = mysql_select_db($dbname) or die ("Couldn't select database.");
$query = "SELECT * FROM category ORDER BY Name";
$result = mysql_query($query);
$categories = mysql_fetch_array($result);

foreach ($categories as $name)
{
$n = 1;
echo "<table border=\"0\" width=\"140\" align=\"center\" style=\"border: 1px solid #000; background: #9C9C9C;\"><tr><td>\n";
echo "<div style=\"border: 1px solid #000; padding: 0px; background: #5d5d5d;\">\n";
echo "<table border=\"0\" width=\"130\" cellspacing=\"0\" cellpadding=\"2\" style=\"background: #fff; color: #000;\">\n";
echo "<tr><td><font color=\"#000\">&nbsp;$name:</font></td><td align=\"right\">\n";
echo "[<a title=\"show/hide\" id=\"exp{$n}_link\" href=\"javascript:void(0);\" onclick=\"toggle(this, \"exp{$n}\");\" style=\"text-decoration: none; color: #000; \">&#8722;</a>]\n";
echo "</td></tr></table><div id=\"exp{$n}\" style=\"padding:3px;\">";

$queryInside = "select * from content WHERE Category=$name ORDER BY Page";
$resultInside = mysql_query($queryInside);
for ($i=0; $i < mysql_num_rows($resultInside); $i++)
{
$row = mysql_fetch_array($resultInside);
echo "<a class=\"special\" href=\"main.php?page={$row['ID']}\">{$row['Page']}</a><br>";
}

echo "</div></div><script language=\"javascript\" type=\"text/javascript\">toggle(getObject('exp{$n}_link'), 'exp{$n}');\n";
echo "</script></td></tr></table><BR>";
$n++;
}
?>

dreamcatcher

10:01 am on Aug 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

Yes, thats not correct the way you have your syntax.

Replace these two lines:


$categories = mysql_fetch_array($result);

foreach ($categories as $name)

with simply:


while ($name = mysql_fetch_array($result))

You then need to change your $name variable in the loop to:

$name['name']

dc

dbzfyam

10:58 am on Aug 5, 2005 (gmt 0)

10+ Year Member



Thank you very much for the quick reply Dreamcatcher. That part now seems to work. Now the for loop doesn't seem to work anymore:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/narutouz/public_html/layout/menu.php on line 40

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/narutouz/public_html/layout/menu.php on line 41

I also tried this:

$queryInside = "SELECT * FROM content WHERE Category={$name['Name']} ORDER BY Page";
$resultInside = mysql_query($queryInside);
$row = mysql_fetch_array($resultInside);
for ($i=0; $i < mysql_num_rows($resultInside); $i++)
{
echo "<a class=\"special\" href=\"main.php?page={$row['ID']}\">{$row['Page']}</a><br>";
}

But I still get the same error. Do you know what I am doing wrong (I don't have much experience with PHP. I have only made simple PHP scripts so far).

jatar_k

2:19 pm on Aug 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> supplied argument is not a valid MySQL result resource

that means the query died, try adding a little debugging

$queryInside = "SELECT * FROM content WHERE Category={$name['Name']} ORDER BY Page";
echo $queryInside;
$resultInside = mysql_query($queryInside) or die (mysql_error());
$row = mysql_fetch_array($resultInside);

that will output the query itself so you can make sure it is properly constructed and also give yoou the error directly from mysql.

dbzfyam

3:06 pm on Aug 5, 2005 (gmt 0)

10+ Year Member



I think I forgot to surround the category name with ' (thanks for the tip jatar_k) since it said this first:

Unknown column 'Information' in 'where clause'

There is some improvement, but instead of getting all rows from the database with for example Information as the category name, I now get about 20 links with the same name and link to the same ID. I am quite lost really. I just checked a PHP book to see how to get all the data out of the database and compared their example to my script. Everything seems to be fine..

jatar_k

3:33 pm on Aug 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> I now get about 20 links with the same name and link to the same ID

yup, that's what code ion your last post is doing, the thing is for every time the loop goes around you need to call mysql_fetch_array again. I think the misunderstanding is that you think it grabs all the rows with a single call. It only grabs the next row each time it is called. What you need to do is have the call to mysql_fetch_array as your test in your loop as dreamcatcher mentioned above.

$queryInside = "SELECT * FROM content WHERE Category='" . $name['Name'] . "' ORDER BY Page";
$resultInside = mysql_query($queryInside);
while ($row = mysql_fetch_array($resultInside)) {
echo "<a class=\"special\" href=\"main.php?page={$row['ID']}\">{$row['Page']}</a><br>";
}

try that out, I also changed the way the query was constructed, I hate using braces and would rather concatenate the string. Just personal preference.

dbzfyam

4:50 pm on Aug 5, 2005 (gmt 0)

10+ Year Member



>> I think the misunderstanding is that you think it grabs all the rows with a single call.

Indeed. I thought it would grab everything at once.

>> What you need to do is have the call to mysql_fetch_array as your test in your loop as dreamcatcher mentioned above.

Just noticed that aswell when I was testing the individual parts. I changed it and now it does work the way I want. The only problem is that it places the links between the tables and the divs for some reason (while I did use the same code as my previous code). I hope I can fix that.

Thank you very much for the help, jatar_k and dreamcatcher.

jatar_k

5:42 pm on Aug 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> places the links between the tables and the divs for some reason

at least the php is working, now on to the html and css ;)

dbzfyam

5:56 pm on Aug 5, 2005 (gmt 0)

10+ Year Member



;). Just fixed it. It was easier than I thought. All menus need to have a unique ID. That's why I added $n. I only accidently placed it inside the loop. So every menu would get the ID 1. Now I only have to work on workarounds for Opera, since it doesn't display the collapsable menu correctly (IE, Netscape and Firefox do display it the right way).

dbzfyam

6:11 pm on Aug 5, 2005 (gmt 0)

10+ Year Member



Workarounds completed aswell. It now works in Netscape, Opera, IE/Mac, Safari and Firefox. CSS.. Wonderfull ;). Now I only need to update the admin panel to add/edit/delete categories and the option to rearrange the categories. Shouldn't be too hard to implement (since I only need to change the values in the database).