andz Posted August 23, 2007 Share Posted August 23, 2007 How to make the select tag view content like this? Desktop -Hardware -Software Health -Good tips -Food Poisoning Security Guides All of the records above are in database format. category_table catid, name, parentid 1 Desktop 0 2 Hardware 1 3 Software 1 4 Health 0 5 Good tips 4 6 Food Poisoning 4 7 Security 0 8 Guides 0 I'm also wondering if where is this implemented? Upon query or on the select tag itself. Please include code if possible.. Thanks. Link to comment https://forums.phpfreaks.com/topic/66297-solved-questions-about-select-tag-embeded-with-php-code/ Share on other sites More sharing options...
matthewhaworth Posted August 23, 2007 Share Posted August 23, 2007 $sql = "SELECT * FROM categories WHERE parentid = 0;"; $query = mysql_query($sql); while($row = mysql_fetch_assoc($query)) { echo $row['name']; $sql = "SELECT * FROM categories WHERE parentid =".$row['catid'].";"; $query = mysql_query($sql); while($rows = mysql_fetch_assoc($query)) { echo "-".$rows['name']; } } There's my solution. IT's crap, but meh. Link to comment https://forums.phpfreaks.com/topic/66297-solved-questions-about-select-tag-embeded-with-php-code/#findComment-331660 Share on other sites More sharing options...
andz Posted August 23, 2007 Author Share Posted August 23, 2007 Thanks for the solution. I'll give it a try. Thanks. Link to comment https://forums.phpfreaks.com/topic/66297-solved-questions-about-select-tag-embeded-with-php-code/#findComment-331662 Share on other sites More sharing options...
matthewhaworth Posted August 23, 2007 Share Posted August 23, 2007 Thanks for the solution. I'll give it a try. Thanks. Hmm, I wouldn't, it'll crash if it can't find anything to put into mysql_fetch_assoc.. <?php $sql = "SELECT * FROM categories WHERE parentid = 0;"; $query = mysql_query($sql); while ($row = mysql_fetch_assoc($query)) { echo $row['name']; $sql = "SELECT * FROM categories WHERE parentid =" . $row['catid'] . ";"; $query = mysql_query($sql); if (mysql_numrows($query) > 0) { while ($rows = mysql_fetch_assoc($query)) { echo "-" . $rows['name']; } } } ?> There, that should fix it. Link to comment https://forums.phpfreaks.com/topic/66297-solved-questions-about-select-tag-embeded-with-php-code/#findComment-331665 Share on other sites More sharing options...
vijayfreaks Posted August 23, 2007 Share Posted August 23, 2007 Hi. you can use the following query to get reocrds with cat and sub cat, suppose ur table name "cat1" and having ur sample data select newCatTab.name as maincat,cat1.name as subcat from cat1 right join (select name,catid from cat1 where parentid=0) as newCatTab on (newCatTab.catid = cat1.parentid) will result in ____________________ maincat | subcat ____________________ desktop | software desktop | hardware health | Good ... Regards, Vijay Link to comment https://forums.phpfreaks.com/topic/66297-solved-questions-about-select-tag-embeded-with-php-code/#findComment-331669 Share on other sites More sharing options...
andz Posted August 23, 2007 Author Share Posted August 23, 2007 Thanks for all your help. Link to comment https://forums.phpfreaks.com/topic/66297-solved-questions-about-select-tag-embeded-with-php-code/#findComment-331672 Share on other sites More sharing options...
matthewhaworth Posted August 23, 2007 Share Posted August 23, 2007 Thanks for all your help. Did my code work? *is interested* Link to comment https://forums.phpfreaks.com/topic/66297-solved-questions-about-select-tag-embeded-with-php-code/#findComment-331673 Share on other sites More sharing options...
vijayfreaks Posted August 23, 2007 Share Posted August 23, 2007 Hi.. Is it working @ ur side..? if its solved then make it solved.. -Viajy Link to comment https://forums.phpfreaks.com/topic/66297-solved-questions-about-select-tag-embeded-with-php-code/#findComment-331675 Share on other sites More sharing options...
andz Posted August 23, 2007 Author Share Posted August 23, 2007 The only record it fetches are : Desktop-Hardware-Software Link to comment https://forums.phpfreaks.com/topic/66297-solved-questions-about-select-tag-embeded-with-php-code/#findComment-331691 Share on other sites More sharing options...
andz Posted August 23, 2007 Author Share Posted August 23, 2007 Ive modified your code and it worked. Here's the modified one: <select name="select"> <?php $sql = "SELECT * FROM test WHERE parentid = 0;"; $query = mysql_query($sql); while ($row = mysql_fetch_assoc($query)) { ?> <option value="#"><?=$row['name']?></option> <?php $catid = $row['catid']; $result = mysql_query("SELECT * FROM test WHERE parentid='$catid'"); if (!$result) { die(mysql_error()); } else { $res = array(); if (mysql_num_rows($result)) { while ($row1 = mysql_fetch_assoc($result)) { $res[] = '<option value="#">'.'-'.$row1['name'].'</option>'; } } echo implode('', $res); } // $sql = "SELECT * FROM test WHERE parentid =" . $row['catid'] . ";"; // $query = mysql_query($sql); // if (mysql_num_rows($query) > 0) // { // while ($rows = mysql_fetch_assoc($query)) // { // echo "-" . $rows['name']; // } // } } ?> </select> Thanks for all your replies.... GodSpeed Link to comment https://forums.phpfreaks.com/topic/66297-solved-questions-about-select-tag-embeded-with-php-code/#findComment-331693 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.