pixeltrace Posted August 2, 2007 Share Posted August 2, 2007 hi, i need help, i need to develop a list menu which have category and subcategory the html code looks like this <select name="select"> <option value="keyword"><b>category 1</b></option> <option value="keyword">subcategory1a</option> <option value="keyword">subcategory1b</option> <option value="keyword"><b>category 2</b></option> <option value="keyword">subcategory2a</option> <option value="keyword">subcategory2b</option> </select> but items for this list menu will be taken from the database my table fields are id | parent | keyword | name | 1 | 0 | keyword | category1 22 | 1 | keyword | category1a 23 | 1 | keyword | category1b 2 | 0 | keyword | category2 24 |2 | keyword |category2a 25 | 2 | keyword | category2b how will i generate the query so my outout will look similar to the html code? hope you could help me with this. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/62988-need-help-to-code-list-menu-with-categories/ Share on other sites More sharing options...
pixeltrace Posted August 2, 2007 Author Share Posted August 2, 2007 hi, any help on this? thanks! Quote Link to comment https://forums.phpfreaks.com/topic/62988-need-help-to-code-list-menu-with-categories/#findComment-313737 Share on other sites More sharing options...
Iceman512 Posted August 2, 2007 Share Posted August 2, 2007 Hi there, I think I understand you correctly; you want to populate a drop-down list with data from the db? If so, this will put you on the right track: <select name="select"> <?php require_once('config.php'); // Contains DB info and connection $sql = ("SELECT * FROM table ORDER BY name DESC"); // For speed, only pull the rows you need while($row = mysql_fetch_array( $sql )) { $id = $row['id']; $parent = $row['parent']; $keyword = $row['keyword']; $name = $row['name']; ?> <option value="<?php echo $keyword ?>"><b><?php echo $name ?></b></option> <?php } mysql_close($con); // Close the connection as specified in config.php ?> </select> I haven't tested the above code yet and you may have to modify it some to get it working for you. I hope it works! Regards, Iceman Quote Link to comment https://forums.phpfreaks.com/topic/62988-need-help-to-code-list-menu-with-categories/#findComment-313749 Share on other sites More sharing options...
pixeltrace Posted August 2, 2007 Author Share Posted August 2, 2007 hi, thanks this is what i actually did on my php code, but what i want is that the list menu will be in order by category my current code is like this <select name="poitype" class="style2" style="width:200px"> <option value="">--select one--</option> <? $uSql = "SELECT code, provider_name, id, parent FROM jos_findit_product_service_providers ORDER by provider_name ASC"; $uResult = mysql_query($uSql, $connection); if(!$uResult){ echo 'no data found'; }else{ while($uRow = mysql_fetch_array($uResult)){ $code = $uRow['code']; $provider = $uRow['provider_name']; $parent = $uRow['parent']; $id = $uRow['id']; ?> <? if ($parent = '0'){ echo "<option value=\""."$code"."\"><b>"."$provider"."</b></option>"; }elseif ($parent !='0' && $id == '$parent'){ echo "<option value=\""."$code"."\">"."$provider"."</option>"; } } } ?> </select> but currently its returning me nothing. this is how my table in my DB looked like id | parent | keyword | name | 1 | 0 | keyword | category1 22 | 1 | keyword | category1a 23 | 1 | keyword | category1b 2 | 0 | keyword | category2 24 |2 | keyword |category2a 25 | 2 | keyword | category2b hope you could help me with this. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/62988-need-help-to-code-list-menu-with-categories/#findComment-313759 Share on other sites More sharing options...
Iceman512 Posted August 2, 2007 Share Posted August 2, 2007 Hi again, The code I have written should order the drop-down list by category as you require. The key to arranging the data by category is here: ... ORDER BY name DESC"); Please copy this modified piece of code to your page and try it.... it should work <select name="select"> <option value=""><b> ----- PLEASE SELECT ONE ----- </b></option> <?php require_once('config.php'); // Contains DB info and connection $sql = ("SELECT * FROM table ORDER BY name DESC"); // For speed, only pull the rows you need while($row = mysql_fetch_array( $sql )) { $id = $row['id']; $parent = $row['parent']; $keyword = $row['keyword']; $name = $row['name']; echo '<option value="'.$keyword.'"><b>'.$name.'</b></option>'; } mysql_close($con); // Close the connection as specified in config.php ?> </select> NOTE: You say that the columns in the DB are id, parent, keyword and name. However in your code, the columns are code, provider_name, id, parent. Is this intentional? Let us know if it works! Iceman Quote Link to comment https://forums.phpfreaks.com/topic/62988-need-help-to-code-list-menu-with-categories/#findComment-313769 Share on other sites More sharing options...
pixeltrace Posted August 2, 2007 Author Share Posted August 2, 2007 sorry for not being able to explain clearly. i want to create an output that looks like this http://mango.resonance.com.sg/myoochi/findit/testa.html hope you could help me with this thanks! Quote Link to comment https://forums.phpfreaks.com/topic/62988-need-help-to-code-list-menu-with-categories/#findComment-313774 Share on other sites More sharing options...
Iceman512 Posted August 2, 2007 Share Posted August 2, 2007 Hi again, Ok.... sorry, I did misunderstand you. Well, let us try the following: <select name="select"> <option value=""><b> ----- PLEASE SELECT ONE ----- </b></option> <?php require_once('config.php'); // Contains DB info and connection $sql = ("SELECT * FROM table ORDER BY name DESC"); // For speed, only pull the rows you need while($row = mysql_fetch_array( $sql )) { $id = $row['id']; $parent = $row['parent']; $keyword = $row['keyword']; $name = $row['name']; if ($parent = '0' || $parent == '0'){ echo '<option value=""><b>» » '.$name.'</b></option>'; } elseif ($parent != '0' || $parent !== '0'){ echo '<option value="'.$keyword.'">'.$name.'</option>'; } else { echo 'An uknown error has occured.'; } } mysql_close($con); // Close the connection as specified in config.php ?> </select> I hope that sets you on the right path. Again, I haven't tested the above code yet. Regards, Iceman Quote Link to comment https://forums.phpfreaks.com/topic/62988-need-help-to-code-list-menu-with-categories/#findComment-313789 Share on other sites More sharing options...
pixeltrace Posted August 2, 2007 Author Share Posted August 2, 2007 hi, what is inside config.php? Quote Link to comment https://forums.phpfreaks.com/topic/62988-need-help-to-code-list-menu-with-categories/#findComment-313817 Share on other sites More sharing options...
Iceman512 Posted August 2, 2007 Share Posted August 2, 2007 Hi, It will have to have the following: <?php /* ----- DB CONFIGURATION ----- */ $dbhost = 'host'; $dbuser = 'user'; $dbpass = 'password'; $dbname = 'database'; /* ----- DB CONNECTION ----- */ $con = mysql_connect($dbhost, $dbuser, $dbpass); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($dbname, $con); ?> Just use it as an include. Iceman Quote Link to comment https://forums.phpfreaks.com/topic/62988-need-help-to-code-list-menu-with-categories/#findComment-313822 Share on other sites More sharing options...
pixeltrace Posted August 2, 2007 Author Share Posted August 2, 2007 hi, this code is working. but the only thing that is missing is that the subcategories are not in line with its main category kindly check my test site http://mango.resonance.com.sg/myoochi/findit/providerfindit2.php click on advanced search tab click 'search by product & service providers' and see what i mean. the order should be something like this http://mango.resonance.com.sg/myoochi/findit/testa.html hope you could help me with this. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/62988-need-help-to-code-list-menu-with-categories/#findComment-313835 Share on other sites More sharing options...
Iceman512 Posted August 2, 2007 Share Posted August 2, 2007 Hi there, Ok... give the following a try and see if it works. <select name="select"> <option value=""><b> ----- PLEASE SELECT ONE ----- </b></option> <?php require_once('config.php'); // Contains DB info and connection $i='0' $sql = ("SELECT * FROM table ORDER BY name DESC"); // For speed, only pull the rows you need while($row = mysql_fetch_array( $sql )) { $id = $row['id']; $parent = $row['parent']; $keyword = $row['keyword']; $name = $row['name']; if ($parent = '0' || $parent == '0'){ echo '<option value=""><b>» » '.$name.'</b></option>'; $i++ if ($parent = $i || $parent == $i){ echo '<option value="'.$keyword.'">'.$name.'</option>'; } else { } } } mysql_close($con); // Close the connection as specified in config.php ?> </select> Let's hope so.... Iceman Quote Link to comment https://forums.phpfreaks.com/topic/62988-need-help-to-code-list-menu-with-categories/#findComment-313860 Share on other sites More sharing options...
Iceman512 Posted August 2, 2007 Share Posted August 2, 2007 Hi, Sorry, I have just spotted a mistake in the code: $i++ should be: $i++; Iceman Quote Link to comment https://forums.phpfreaks.com/topic/62988-need-help-to-code-list-menu-with-categories/#findComment-313930 Share on other sites More sharing options...
pixeltrace Posted August 3, 2007 Author Share Posted August 3, 2007 hi, thanks for the response again. i tried the codes you gave me, but its returning 0 results this is my current code <select name="poitype" class="style2" style="width:200px"> <option value="">--select one--</option> <? $i='0'; $sql = ("SELECT * FROM jos_findit_product_service_providers ORDER by provider_name ASC"); // For speed, only pull the rows you need while($row = mysql_fetch_array( $sql )) { $id = $row['id']; $parent = $row['parent']; $code = $row['code']; $name = $row['provider_name']; if ($parent = '0' || $parent == '0'){ echo '<option value="'.$code.'"><b>» » '.$name.'</b></option>'; $i++; if ($parent = $i || $parent == $i){ echo '<option value="'.$code.'">'.$name.'</option>'; } else { } } } mysql_close($connection); // Close the connection as specified in config.php ?> </select> whatelse did we miss? hope you could help me with this. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/62988-need-help-to-code-list-menu-with-categories/#findComment-314632 Share on other sites More sharing options...
teng84 Posted August 3, 2007 Share Posted August 3, 2007 are you sure you have the value on the select print the result first to see if you really fetch something Quote Link to comment https://forums.phpfreaks.com/topic/62988-need-help-to-code-list-menu-with-categories/#findComment-314636 Share on other sites More sharing options...
pixeltrace Posted August 3, 2007 Author Share Posted August 3, 2007 hi, i tried this <select name="poitype" class="style2" style="width:200px"> <option value="">--select one--</option> <? $i=0; $uSql = ("SELECT * FROM jos_findit_product_service_providers ORDER by provider_name ASC"); // For speed, only pull the rows you need $uResult = mysql_query($uSql, $connection); if(!$uResult){ echo 'no data found'; }else{ while($uRow = mysql_fetch_array($uResult)){ $id = $uRow['id']; $parent = $uRow['parent']; $code = $uRow['code']; $name = $uRow['provider_name']; if ($parent = 0 || $parent == 0){ echo '<option value="'.$code.'"><b>» » '.$name.'</b></option>'; $i++; if ($parent = $i || $parent == $i){ echo '<option value="'.$code.'">'.$name.'</option>'; } else { } } } } mysql_close($connection); // Close the connection as specified in config.php ?> but its only returing the main category but not the subcategory in my database the parent of my subcategory is equal to the id of the main category and main category has a 0 value for the parent id | parent | code | provider_name | 1 0 wer werer 2 0 rty rtytyt 4 1 tytu rtyttut 5 2 erere rtrtrtrtr something like that. check my test site http://mango.resonance.com.sg/myoochi/findit/providerfindit2.php click on advance tab and on that page, click 'search for product & service providers' you will see the returned value. hope you could help me fix the code thanks! Quote Link to comment https://forums.phpfreaks.com/topic/62988-need-help-to-code-list-menu-with-categories/#findComment-314640 Share on other sites More sharing options...
teng84 Posted August 3, 2007 Share Posted August 3, 2007 wrong if ($parent = '0' || $parent == '0'){ > > it should be if ($parent =='0' || $parent == '0'){ and about that the same condition tell us what you what with that Quote Link to comment https://forums.phpfreaks.com/topic/62988-need-help-to-code-list-menu-with-categories/#findComment-314641 Share on other sites More sharing options...
teng84 Posted August 3, 2007 Share Posted August 3, 2007 try echo'<select name="poitype" class="style2" style="width:200px"> <option value="">--select one--</option>'; $i='0'; $sql = ("SELECT * FROM jos_findit_product_service_providers ORDER by provider_name ASC"); // For speed, only pull the rows you need while($row = mysql_fetch_array( $sql )) { $id = $row['id']; $parent = $row['parent']; $code = $row['code']; $name = $row['provider_name']; if ($parent == '0'){ echo '<option value="'.$code.'"><b>» » '.$name.'</b></option>'; } if ($parent == $i){ echo '<option value="'.$code.'">'.$name.'</option>'; } $i++; } echo ' </select>'; mysql_close($connection); note not tested<---------- Quote Link to comment https://forums.phpfreaks.com/topic/62988-need-help-to-code-list-menu-with-categories/#findComment-314646 Share on other sites More sharing options...
pixeltrace Posted August 3, 2007 Author Share Posted August 3, 2007 hi, i tried using this code <? $i=0; $uSql = ("SELECT * FROM jos_findit_product_service_providers ORDER by provider_name ASC"); // For speed, only pull the rows you need $uResult = mysql_query($uSql, $connection); if(!$uResult){ echo 'no data found'; }else{ while($uRow = mysql_fetch_array($uResult)){ $id = $uRow['id']; $parent = $uRow['parent']; $code = $uRow['code']; $name = $uRow['provider_name']; if ($parent == '0' || $parent == '0'){ echo '<option value="'.$code.'"><b>» » '.$name.'</b></option>'; $i++; if ($parent == $i || $parent == $i){ echo '<option value="'.$code.'"> '.$name.'</option>'; } else { } } } } mysql_close($connection); // Close the connection as specified in config.php ?> but its only showing me the main category is there aways that the logic on the second condition will be like this $parent ='0'; echo '<option value="'.$code.'"><b>» » '.$name.'</b></option>'; $parent !=0 and $id = maincategory id; echo '<option value="'.$code.'"> '.$name.'</option>'; hope you could help me fix this code because i really cant visualize how the could flow will be. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/62988-need-help-to-code-list-menu-with-categories/#findComment-314744 Share on other sites More sharing options...
Barand Posted August 3, 2007 Share Posted August 3, 2007 does this topic help? http://www.phpfreaks.com/forums/index.php/topic,116245.msg473517.html#msg473517 Quote Link to comment https://forums.phpfreaks.com/topic/62988-need-help-to-code-list-menu-with-categories/#findComment-314820 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.