tryingtolearn Posted July 24, 2007 Share Posted July 24, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/61630-solved-help-displaying-categories-w-subs/ Share on other sites More sharing options...
Mutley Posted July 24, 2007 Share Posted July 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/61630-solved-help-displaying-categories-w-subs/#findComment-306769 Share on other sites More sharing options...
tryingtolearn Posted July 25, 2007 Author Share Posted July 25, 2007 Thanks Mutly I tried it and it gives me this error Undefined variable: title_sub Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/61630-solved-help-displaying-categories-w-subs/#findComment-306775 Share on other sites More sharing options...
tryingtolearn Posted July 25, 2007 Author Share Posted July 25, 2007 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); } } Quote Link to comment https://forums.phpfreaks.com/topic/61630-solved-help-displaying-categories-w-subs/#findComment-306861 Share on other sites More sharing options...
tryingtolearn Posted July 25, 2007 Author Share Posted July 25, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/61630-solved-help-displaying-categories-w-subs/#findComment-307029 Share on other sites More sharing options...
sasa Posted July 25, 2007 Share Posted July 25, 2007 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/61630-solved-help-displaying-categories-w-subs/#findComment-307416 Share on other sites More sharing options...
tryingtolearn Posted July 26, 2007 Author Share Posted July 26, 2007 Thanks so much sasa! That first one worked great Thank you for taking the time for me. Quote Link to comment https://forums.phpfreaks.com/topic/61630-solved-help-displaying-categories-w-subs/#findComment-307559 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.