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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.