Jump to content

[SOLVED] help displaying categories w/ subs


tryingtolearn

Recommended Posts

Hopefully someone has an idea on how to accomplish this.

 

I have a mysql table called categories

3 fields

The ones with 0 as the parent are the main headings

or if parent has the value of ID it is a subcat of that title.

 

id  title  parent

1    A      0

2    B      0

3    C      0

4    zz    2

4    ww  3

 

I can get it to diplay in a list format but I need something like this

 

A    B    C

      zz  ww

 

Does that make sense??

All the main headings up top with the subs below the proper main in a column.

 

Can someone offer any advice?

Link to comment
https://forums.phpfreaks.com/topic/61630-solved-help-displaying-categories-w-subs/
Share on other sites

You could do it in 2 select queries. If I understand you correctly, something like:

 

<?php
$result = mysql_query("SELECT id, title FROM table WHERE parent = '0' ");
while($row = mysql_fetch_array( $result )){
	$id = $row['id'];
	$title_parent = $row['title'];
}

$result2 = mysql_query("SELECT title FROM table WHERE id = '$id' ");
while($row2 = mysql_fetch_array( $result2 )){
	$title_sub = $row2['title'];
}

echo $title_parent;// The parent
echo $title_sub;// The parents subcategory
?>

 

I hope that makes sense, the first query selects the parent and creates it into a variable, so the next query can use that variable to find all subcategories of it.

Well I read it again and I suppose that isnt what I was looking for anyway.

I should have posted the code Im using already

 

This is what I am using to generate the vertical list of Main cats and subcats.

 

Is there a way to modify this to produce - I guess what would be a table.

without knowing how many Main categories there will be going across the top.

 

Here is the code Im using now.

function navcats ($parent, $level=0) {
         $res = mysql_query ("SELECT id, title, parent FROM categories WHERE parent = $parent ORDER BY title");
	       while (list($id, $title) = mysql_fetch_row($res)) {
                $indent = str_repeat('»', $level);
			if($level==0){
			$title ="<b>$title</b>";
			}
			echo " <li>$indent <a href=\"result.php?cat_id=$id\">$title</a></li>\n";
                navcats($id, $level+1);
         }
}

This has got me a little closer

function navcats ($parent, $level=0) {
         $res = mysql_query ("SELECT id, title, parent FROM categories WHERE parent = $parent ORDER BY title");
	       while (list($id, $title) = mysql_fetch_row($res)) {
                $indent = str_repeat('»', $level);
			if($level==0){
			$title ="<b>$title</b><br>";
			$rw = "<td valign=\"top\"> \n";			
			}else{
			$title ="<a href=\"result.php?cat_id=$id\">$title</a><br>";
			$rw = " \n";	
			}				
			echo " $rw $indent $title \n";		
                navcats2($id, $level+1);
         }
}

echo "<table cellspacing=\"0\" cellpadding=\"5\" border=\"5\"> \n";
navcats(0); 
echo "</table> \n";

 

I get the results the way I want but the table is not correct

<table cellspacing="0" cellpadding="5" border="5"> 
<td valign="top"> 
<b></b><br> 
<td valign="top"> 
<b>main1</b><br> 
  
» <a href="result.php?cat_id=32">sub1</a><br>
» <a href="result.php?cat_id=33">sub2</a><br> 
» <a href="result.php?cat_id=30">sub3</a><br> 
» <a href="result.php?cat_id=31">aub4</a><br> 
<td valign="top"> 
<b>main2</b><br> 
  
» <a href="result.php?cat_id=53">sub1</a><br>   
» <a href="result.php?cat_id=57">sub2</a><br>   

</table> 

 

Any ideas on outputting the correct table code?

try

<?php
mysql_connect('localhost');
mysql_select_db('test');
$row1 = array();
$rows = array();
$res = mysql_query ("SELECT id, title, parent FROM categories ORDER BY title");
while (list($id, $title,$parent) = mysql_fetch_row($res)) {
if ($parent) $rows[$parent][$id] = $title; else $row1[$id] = $title;
}
echo '<table border="15" cellpadding="5">';
echo '<tr><td>', implode('</td><td>', $row1), '</td></tr>';
echo '<tr valign="top">';
foreach ($row1 as $k => $v) {
echo'<td>';
if (isset($rows[$k])) {
	foreach ($rows[$k] as $id => $title) echo "<a href=\"result.php?cat_id=$id\"> $title </a><br>";
} else echo ' ';
echo '</td>';
}
echo '</tr></table>';
?>

or

<?php
mysql_connect('localhost');
mysql_select_db('test');
$row1 = array();
$rows = array();
$res = mysql_query ("SELECT id, title, parent FROM categories ORDER BY title");
while (list($id, $title,$parent) = mysql_fetch_row($res)) {
if ($parent) $rows[$parent][] = array($id, $title); else $row1[$id] = $title;
}
echo '<table border="5" cellpadding="5">', "\n";
echo '<tr><td>', implode('</td><td>', $row1), '</td></tr>', "\n";
echo '<tr valign="top">';
$r = 0;
while (count($rows)){
foreach ($row1 as $k => $v) {
echo "\n", '<td>';
if (isset($rows[$k][$r])) {
	echo "<a href=\"result.php?cat_id=", $rows[$k][$r][0],'">',  $rows[$k][$r][1], "</a>\n";
	unset($rows[$k][$r]);
	if (!count($rows[$k])) unset($rows[$k]);
} else echo ' ';

echo '</td>', "\n";
}
echo '</tr>', "\n";
$r++;
}
echo '</table>';
?>

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.