unplugged_web Posted February 3, 2011 Share Posted February 3, 2011 I wonder if somebody can help me please. I'm trying to get a list of categories with sub-categories listed under each. For example:Category One Sub-Category One/One Sub-Category One/Two Sub-Category One/ThreeCategory Two Sub-Category Two/One Sub-Category Two/TwoCategory Three Sub-Category Three/Oneetc.I've tried this, but when I got to check it the page all I get is the category titles <?php $sql = 'SELECT * FROM categories ORDER BY name ASC'; $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { echo "<h1>" . $row['name'] . </h1>"; echo "<ul class='rounded'>"; $sql = 'SELECT * FROM sub_categories WHERE categoryid = "$categoryid"'; $result2 = mysql_query($sql); while($row2 = mysql_fetch_array($result2)) { echo "<li>"; echo "<a href='list.php?id={$row['subid']}&{$row['subname']}'>" . $row['subname'] . "</a>"; echo "</li>"; } echo "</ul>"; } ?> Thanks Link to comment Share on other sites More sharing options...
Synook Posted February 3, 2011 Share Posted February 3, 2011 You're fetching the results as an object - to use array notation fetch it as an array instead, using mysql_fetch_array(). Link to comment Share on other sites More sharing options...
unplugged_web Posted February 3, 2011 Author Share Posted February 3, 2011 You're fetching the results as an object - to use array notation fetch it as an array instead, using mysql_fetch_array().I've just tried that, but it only got the categories, none of the sub categories.This is what I tried:<?php$sql = 'SELECT * FROM categories ORDER BY name ASC'; $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { echo "<h1>" . $row['name'] . </h1>";echo "<ul class='rounded'>"; $sql = 'SELECT * FROM sub_categories WHERE categoryid = "$categoryid"'; $result2 = mysql_query($sql); while($row2 = mysql_fetch_array($result2)) { echo "<li>"; echo "<a href='list.php?id={$row['subid']}&{$row['subname']}'>" . $row['subname'] . "</a>"; echo "</li>"; } echo "</ul>"; } ?> Link to comment Share on other sites More sharing options...
dsonesuk Posted February 3, 2011 Share Posted February 3, 2011 where exactly is the value for $categoryid coming from? Link to comment Share on other sites More sharing options...
unplugged_web Posted February 3, 2011 Author Share Posted February 3, 2011 where exactly is the value for $categoryid coming from?It's one of the fields in the 'categories' table. Should I specify it rather than just * ? Link to comment Share on other sites More sharing options...
dsonesuk Posted February 3, 2011 Share Posted February 3, 2011 There's nothing at the moment tell it should equal a value from a field in categories table, i would expectwhile($row = mysql_fetch_array($result)) {$categoryid=$row['id'];echo "<h1>" . $row['name'] . </h1>";echo "<ul class='rounded'>";.....at least. Link to comment Share on other sites More sharing options...
unplugged_web Posted February 3, 2011 Author Share Posted February 3, 2011 There's nothing at the moment tell it should equal a value from a field in categories table, i would expectwhile($row = mysql_fetch_array($result)) {$categoryid=$row['id'];echo "<h1>" . $row['name'] . </h1>";echo "<ul class='rounded'>";.....at least.That didn't work, it still didn't show the sub categories Link to comment Share on other sites More sharing options...
dsonesuk Posted February 3, 2011 Share Posted February 3, 2011 Don't use single quotes for sql, use double$sql = "SELECT * FROM categories ORDER BY name ASC";$sql = "SELECT * FROM sub_categories WHERE categoryid = '$categoryid'";are these accessing the correct table? shouldn't it be $row2['subname']; as inecho "<a href='list.php?id=".$row2['subid'].$row2['subname']."'>" . $row2['subname'] . "</a>"; <?php$sql = "SELECT * FROM categories ORDER BY name ASC"$result = mysql_query($sql);while($row = mysql_fetch_array($result)) {$categoryid=$row['id']; // is this the correct field name?echo "<h1>" . $row['name'] . </h1>";echo "<ul class='rounded'>" $sql = "SELECT * FROM sub_categories WHERE categoryid = '$categoryid'"; $result2 = mysql_query($sql); while($row2 = mysql_fetch_array($result2)) { echo "<li>"; echo "<a href='list.php?id=".$row2['subid'].$row2['subname']."'>" . $row2['subname'] . "</a>"; echo "</li>"; }echo "</ul>"; } ?> Link to comment Share on other sites More sharing options...
birbal Posted February 3, 2011 Share Posted February 3, 2011 echo the $categoryid..see what value is it passing to the server...do some var_dump to see what values are actually returning from the serveri think your $categoryid is passing empty value..as you are not assighning any value to $categoryid while($row = mysql_fetch_array($result)){$categoryid=$row['id'];echo "<h1>" . $row['name'] . </h1>";echo "<ul class='rounded'>";.....here its not nescery that your db column name must be id..it should be the name which name you gave to your id column in your db... Link to comment Share on other sites More sharing options...
unplugged_web Posted February 3, 2011 Author Share Posted February 3, 2011 Don't use single quotes for sql, use double$sql = "SELECT * FROM categories ORDER BY name ASC";$sql = "SELECT * FROM sub_categories WHERE categoryid = '$categoryid'";are these accessing the correct table? shouldn't it be $row2['subname']; as inecho "<a href='list.php?id=".$row2['subid'].$row2['subname']."'>" . $row2['subname'] . "</a>";<?php$sql = "SELECT * FROM categories ORDER BY name ASC"$result = mysql_query($sql);while($row = mysql_fetch_array($result)) {$categoryid=$row['id']; // is this the correct field name?echo "<h1>" . $row['name'] . </h1>";echo "<ul class='rounded'>" $sql = "SELECT * FROM sub_categories WHERE categoryid = '$categoryid'"; $result2 = mysql_query($sql); while($row2 = mysql_fetch_array($result2)) { echo "<li>"; echo "<a href='list.php?id=".$row2['subid'].$row2['subname']."'>" . $row2['subname'] . "</a>"; echo "</li>"; }echo "</ul>"; } ?> Perfect thank you, I changed the quotes to double quotes and it now works perfectly thanks. I had actually put the rows and row2 for the sub categories, I'd just forgotten to say here I had.Thanks for your help Link to comment Share on other sites More sharing options...
unplugged_web Posted February 3, 2011 Author Share Posted February 3, 2011 echo the $categoryid..see what value is it passing to the server...do some var_dump to see what values are actually returning from the serveri think your $categoryid is passing empty value..as you are not assighning any value to $categoryidhere its not nescery that your db column name must be id..it should be the name which name you gave to your id column in your db...Thanks for that, I followed dsonesuk advice to put the query in double quotes and it now works perfectly. Thanks Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.