Jump to content

category order?


jagguy

Recommended Posts

Hi,

 

I am wondering how to do this efficiently in php.

I have a table of data with 1 col has the category_id and another has a parent_id

 

eg

category_id category  parent_id

1                hardware  0

2                DVD        0

3                action    2

4                carchase    3

5                cars        0

etc

 

action is a subcategory of DVD and carchase is a subcategory of action.

How can i list all this data in some sort of order of category with subcategories listed below each category with php/myql.

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/44330-category-order/
Share on other sites

<?php

function recurList($start)
{
global $master_array;
$temp = array_keys($master_array['parent_id'],$master_array['parent_id'][$start]);
if	(
	count($temp)	>	0
	)
{
?>
<ul>
<?php
foreach($temp as $key => $val)
{
?>
<li><?php echo $master_array['category'][$val]; ?>
<?php
if	(
	array_keys($master_array['parent_id'],$master_array['category_id'][$val])
	)
{
	recurList($val);
}
?>
</li>
<?php
}
?>
</ul>
<?php
}
}

$qry = "
	SELECT
		*
	FROM
		`yourtable`
	ORDER BY
		`parent_id`	ASC,
		`category_id`	ASC
	";
$qry = mysql_query($qry);

$master_array = NULL;

while($row = mysql_fetch_assoc($qry))
{
foreach($row as $key => $val)
{
	$master_array[$key][] = $val;
}
}

recurList(0);
?>

Link to comment
https://forums.phpfreaks.com/topic/44330-category-order/#findComment-215293
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.