Forum Moderators: coopster
<?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\"> $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; \">−</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++;
}
?>
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).
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.
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..
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.
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.