Hi All day today I've been trying to do an admin panel for creating categories for the website. I would like to create this effect using php and a front-end form. Cat 1 - Sub 1 - Sub 2 - Sub 3 - Sub 4 - Sub 5 - Sub 6 - Sub 7 - Sub 8 Cat 2 - Sub 1 - Sub 2 Cat 3 - Sub 1 - Sub 2 - Sub 3 PHP: So how can I go about it? It's been a problem which has bothered me for a couple of hours. Thanks
Use multi-dimensional arrays: <?php $tree = array( 'Cat 1' => array('Sub 1' => 'file.php', 'Sub 2' => 'file2.php'), 'Cat 2' => array('Sub 1' => 'file3.php', 'Sub 2' => 'file4.php') ); foreach ($tree as $sub => $subtree){ echo "{$sub}<br>"; foreach ($subtree as $title => $link){ echo "- $title [$link]<br>"; } echo "<br>"; } ?> PHP: Should give something like: Cat 1 - Sub1 [file1.php] - Sub2 [file2.php] Cat 2 - Sub1 [file3.php] - Sub2 [file4.php] PHP: Hope this helps, Jay Note: This code is untested and is provided as a theory / guideline only.
Ah! I thought you didn't want MySQL Just use two tables categories subs and do SELECT * FROM catagories then for each category SELECT * FROM subs WHERE catid='x' Jay
OK if I do that how can i put them in order so cat 1 would have its sub cats under it? and also make it loop for all the values of cat
Make 1 table called Categories Fields for Categories: -ID (int) (autoincrement) (primary key) -Name (text) -ParentID (int) (default: 0) <-- 0 means top-level category (is not a subcategory) $sql = "SELECT * FROM Categories WHERE ParentID = 0"; $res = mysql_query($sql); while ($row = mysql_fetch_assoc($res)) { echo $row["Name"]; $sql_sub = "SELECT * FROM Categories WHERE ParentID = " . $row["ID"]; $res_sub = mysql_query($sql_sub); while ($row_sub = mysql_fetch_assoc($res_sub)) { echo "<br/> -" . $row_sub["Name"]; } echo "<br/>"; } Code (markup): This is very basic, but you could do up a recursive function to have subcategories of subcategories, down as many levels as you want. What I wrote above will give you the top 2 levels (Top-level Categories and their immediate subcategories.
did you create the table exactly as described and enter your categories? For subcategories, the ParentID is the ID of the category you want it to be a subcategory of. Try this: $sql = "SELECT * FROM Categories WHERE ParentID = 0"; $res = mysql_query($sql) or die($sql . "<br><br>" . mysql_error()); while ($row = mysql_fetch_assoc($res)) { echo $row["Name"]; $sql_sub = "SELECT * FROM Categories WHERE ParentID = " . $row["ID"]; $res_sub = mysql_query($sql_sub) or die($sql_sub . "<br><br>" . mysql_error()); while ($row_sub = mysql_fetch_assoc($res_sub)) { echo "<br/> -" . $row_sub["Name"]; } echo "<br/>"; } Code (markup): and post the message you get back..
That issue has been fixed now I am trying to make this work with htaccess file now. So I created a file which adds the categories. The bit I am stuck with right now is how do I get it to write the fillowing: cat1/subcat-name-1/ cat.php?id=5
hey there, sorry, I'm not sure exactly what you mean. Can you explain what you're trying to do in a bit more detail?
I am trying to use .htaccess to make the URLs SEO friendly. So if I have a page that displays the content of category with ID 5 then rather than using show.php?id=5 instead make the URL like: cat5_name/ and the same with sub-cats. If it has ID 7 and is a sub cat of cat3, then go to cat3_name/subcat_name/ Is that any clearer?