PDA

View Full Version : [php] looping through this data


ELD
July 6th, 2009, 09:00 AM
Okay say i have this back from some sql:


id | name | parent | is_cat
1 | Category 1 | 0 | 1
5 | Category 2 | 0 | 1
2 | forum 1 | 1 | 0
6 | forum 2 | 5 | 0
7 | forum 3 | 5 | 0


I want to first loop through where "is_cat" = 1, then for each of those loop through where the "parent" = their "id"

So it would end up like:
Category 1
--forum 1
Cateogry 2
--forum 2
--forum 3

I know it is probably using some foreach loop but i am unsure?

credobyte
July 6th, 2009, 09:48 AM
Pardon me if it doesn't work as expected - I'm at work and can't test it ;)

<?php

$query = mysql_query("SELECT * FROM somewhere");

while ($data = mysql_fetch_array($query)) {
if ($data['is_cat'] == 1) {
echo "<b>" . $data['name'] . "<b> <br />";
$sql_findparent = mysql_query("SELECT * FROM somewhere WHERE parent='{$data['id']}'");
while ($parent = mysql_fetch_array($sql_findparent)) {
echo "-- " . $parent['name'] . "<br />";
}
}
}

?>

ELD
July 6th, 2009, 09:49 AM
I'm doing it in one query, i have since learnt i need to do the sorting in the query.

http://forums.devshed.com/showpost.php?p=2289918&postcount=2

masux594
July 6th, 2009, 10:08 AM
the "tree" structure is perfect for recursive methods.. A recursive method is a little bit hard to create, but is more flexible and smarter!
i think that you can build with a single query, but when you create the query, you're "fixed" with the example that you show.. I'm a developer and by the experience i know that "a fixed thing, in the future, it will come changed!"

Sysc, A

ELD
July 6th, 2009, 10:26 AM
It is fine the way i linked to on devshed, i only need that many.

masux594
July 6th, 2009, 10:33 AM
i don't understand.. do u need the query for your db?

Sysc, A

ELD
July 6th, 2009, 04:19 PM
It's just a simple output to show categorys, forums and then their subforums no deeper than that for the frontpage, it's sorted now :)