0

Is there a way to get a distinct value from a table and echo it as a id for a div tag and still have the rest of the values echo too?

Say I have a table with these values:
category
item

With 3 categories each having 3 items
I'd like category to be distinct and all the items in that category to be listed in the divs like this:

<div id="category1">
item1
item2
item3
</div>

<div id="category2">
item1
item2
item3
</div>

<div id="category3">
item1
item2
item3
</div>

I use this to get the div array:

$result = mysql_query("SELECT DISTINCT category FROM $mysql_table ORDER BY category");
while($row = mysql_fetch_array($result)) {
  echo "<div id=\"".$row['category']."\">
";
  echo "".$row['category']."";
  echo "
</div>
";

But I would like to get the rest of the values to list the items for each category where echo "".$row.""; is now.
Kind of hard to explain. Hope this makes sense?

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by bobgodwin
0

I figured it out.
Here's the code:

$result = mysql_query("SELECT DISTINCT category FROM $mysql_table");
while($row = mysql_fetch_array($result)) {
$cat = $row['category'];
if ( $cat == $row['category'] ) {
  $cat = $row['category'];
}

echo "<div id=\"$cat\" class=\"dropmenudiv\" style=\"width: 150px;\">
";
$results = mysql_query("SELECT * FROM $mysql_table WHERE category='$cat'");
while($rows = mysql_fetch_array($results))
  echo "<a href=\"".$rows['linkname'].".php\">".$rows['page_title']."</a>
";
echo "</div>
";

My second query was using the same name for $result and $row, so I put a 's' on the end of them and it worked.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.