Jump to content

Creating Lists


unplugged_web
 Share

Recommended Posts

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

Edited by thehappyappy
Link to comment
Share on other sites

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

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

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>";  }    ?>

Edited by dsonesuk
Link to comment
Share on other sites

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

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...