I have this table named "categories" with three fields as below
cat | order | id
--------|-------|----
News | 3 | 23
Hi-Tech | 2 | 15
Biz | 5 | 8
Health | 1 | 3
Also, I have another table named "links" like below
link | order | cat-id
--------|-------|-------
link1 | 2 | 23
link2 | 8 | 15
link3 | 5 | 8
link4 | 6 | 15
link5 | 2 | 15
link6 | 4 | 23
link7 | 1 | 3
link8 | 1 | 8
What I want to achieve is to sort categories and below each category to sort the links of that category cat-id / id like this below:
Health
link7
Hi-Tech
link5
link4
link2
News
link1
link6
Biz
link8
link3
I succeeded on showing the categories sorted but I am loosing it after it.
My aim is to show this on a page so I guess I have to use PHP and mySQL.
The query is simply this:
SELECT c.cat, l.link, c.id
FROM links l INNER JOIN categories c
ON l.cat-id = c.id
ORDER BY c.`order`, l.`order`
And here it is at work.
In order to show it on a page, it would look like this:
mysql_connect(<host>, <username>, <password>);
$q = mysql_query("<query from above>");
$last_cat = 0;
while($row = mysql_fetch_assoc($q)) {
if($row['id'] != $last_cat) {
print '<h3>' . $row['cat'] . '</h3>';
$last_cat = $row['id'];
}
print $row['link'] . '<br>';
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With