Forum Moderators: coopster

Message Too Old, No Replies

Displaying MySQL results question about formatting

         

dwalls32

6:21 pm on Sep 13, 2006 (gmt 0)

10+ Year Member



I am working on our Intranet page (PHP/MySQL) and would like to include the ability for admin's to post links for users in a categorized format. I had originally thought about storing each link in a table with an index, URL, title, description, and category. I would like to know how to place the results in order on the page so that it would show like below:

category 1
url1
url2
url3

category 2
url1
url2
url3

Thank you in advance!

Psychopsia

7:13 pm on Sep 13, 2006 (gmt 0)

10+ Year Member



You can order the link list by the index (ORDER BY id) or with a custom order, adding a field like "link_order" (ORDER BY link_order).

dwalls32

8:07 pm on Sep 13, 2006 (gmt 0)

10+ Year Member



Thanks for your reply. I understand that I could order by, but I wanted to know how to split them up by category with a category heading for each set...

Psychopsia

8:22 pm on Sep 13, 2006 (gmt 0)

10+ Year Member



Ok. If you want to show the category title and then items, create a table "category" with fields:

cat_id INT and cat_name VARCHAR

For the "items" table:

item_id INT
item_cat INT
item_name VARCHAR

--

$sql = 'SELECT * FROM category ORDER BY cat_id';
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result))
{
echo $row['cat_name'] . '<br />';

$sql_item = 'SELECT * FROM _items WHERE item_cat = ' . $row['cat_id'];
$result_item = mysql_query($sql_item);

while ($row_item = mysql_fetch_array($result_item))
{
echo $row['item_name'] . '<br />';
}

echo '<br />';
}

jatar_k

11:40 pm on Sep 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could use GROUP BY with ORDER BY

dwalls32

2:46 am on Sep 14, 2006 (gmt 0)

10+ Year Member



Psychopsia, thanks! That worked perfectly!