Hey Having a problem with PHP/MySQL. My MySQL database table: The category hierarchy will be as follows: cat1 sub3 cat2 sub1 sub4 sub2 cat3 -- I want to show all the categories in a dropdown menu in the following way: How should I go about doing this, thanks. -- Sorry if this sounds confusing, and thanks in advance to any help
Use this as a start. just wrote for you. need some edits tho. edit $the_table before start. and edit the form to do what u want to do eg: when user click on it // after connected to sql print '<select name="">'; $the_table = ''; // the table name of the category $get_parent = mysql_query("SELECT * FROM $the_table WHERE parentid=0"); if (mysql_num_rows($get_parent)>0) { while ($parent = mysql_fetch_assoc($get_parent)) { print '<option value="">'.$parent['name'].'</option>'; $get_child = mysql_query("SELECT * FROM $the_table WHERE parentid={$parent['id']}"); if (mysql_num_rows($get_child)>0) { while ($child = mysql_fetch_assoc($get_child)) { print '<option value=""> |__ '.$child['name'].'</option>'; $get_grandson = mysql_query("SELECT * FROM $the_table WHERE parentid={$child['id']}"); if (mysql_num_rows($get_grandson)>0) { while ($grandson = mysql_fetch_assoc($get_grandson)) { print '<option value=""> |__ '.$grandson['name'].'</option>'; } // end while } } // end while } } // end while } print '</select>'; PHP:
Thanks! Now say I get these info from MySQL, and I want to echo it in a HTML table. But this table has two columns, how do I split the results into a table with two columns? Like <tr> <td>results</td> <td>results2</td> </tr> <tr> <td>results</td> <td>results2</td> </tr> Instead of simply results <br /> results <br /> THANKS!
It's like the same as above, but ignoring the hierarchy. So say I want to echo the above categories in a table, print '<table width="100%" cellpadding="3"> <tbody>'; while ($cat = mysql_fetch_assoc($get_cat)) { print '<tr><td>'.$cat[name].'</td></tr>'; } PHP: That will print: But how do I go about doing it, such that I can get something like: <table width="100%" cellpadding="3"> <tbody> <tr><td>cat1</td><td>cat2</td></tr> <tr><td>cat3</td><td>sub1</td></tr> HTML: and so on. Thanks!
maybe something like this : print '<table width="100%" cellpadding="3"> <tbody>'; $counter = 0; while ($cat = mysql_fetch_assoc($get_cat)) { if($counter % 2 == 0) { print("<tr>"); } $counter += 1; print '<td>'.$cat[name].'</td>'; if($counter % 2 == 0) { print("</tr>"); } } PHP:
So based on your HTML it will be $limit_per_row = 2; // since you want two only print '<table width="100%" cellpadding="3"> <tbody>'; while ($cat = mysql_fetch_assoc($get_cat)) { if (!isset($loop)) { $loop = 1; } else {} if ($loop == 1) { print "<tr>\r\n"; } print '<td>'.$cat['name'].'</td>'."\r\n"; if ($loop == $limit_per_row) { print "</tr>\r\n"; unset($loop); } $loop++; } // remember to close the table PHP: