jbrill Posted March 5, 2008 Share Posted March 5, 2008 hey, just need some help with a simple array here.. using php and javascript. i have this array being generated (model,id,type) example: var Array1 = new Array("('Select a Model','','',true,true)","('Veyron','91','1')","('')"); The javascript: function populatemodel(inForm,selected) { var selectedArray = eval("Array"+selected); while (selectedArray.length < inForm.model.options.length) { inForm.model.options[(inForm.model.options.length - 1)] = null; } for (var i=0; i < selectedArray.length; i++) { eval("inForm.model.options[i]=" + "new Option" + selectedArray[i]); } } My drop down (the value should be the id, and each item in the drop down shoudl display "model - type") Example: <select name="model"> <option>Select a Model</option> <? $loadsubcat = "SELECT * FROM dynamic_models WHERE make='".$current['make']."'"; $sclist = mysql_query($loadsubcat); $subcats = mysql_fetch_array($sclist); do { echo "<option value=\"".$subcats['id']."\""; if ($current['model'] == $subcats['id']) {echo "selected";} echo ">".$subcats['model']."</option>"; } while($subcats = mysql_fetch_array($sclist)); ?> </select> The problem is that i do not know why i cannot get the " - type" to show in the dropdown! once again, the array is like so: (model,id,type) thanks in advance! Link to comment https://forums.phpfreaks.com/topic/94560-need-help-with-a-simple-array/ Share on other sites More sharing options...
paul2463 Posted March 5, 2008 Share Posted March 5, 2008 have you tried <?php do { echo "<option value=\"".$subcats['id']."\""; if ($current['model'] == $subcats['id']) {echo "selected";} echo ">".$subcats['model']." - ".$subcats['type']." </option>"; } while($subcats = mysql_fetch_array($sclist)); ?> Link to comment https://forums.phpfreaks.com/topic/94560-need-help-with-a-simple-array/#findComment-484183 Share on other sites More sharing options...
jbrill Posted March 5, 2008 Author Share Posted March 5, 2008 yes, i have Link to comment https://forums.phpfreaks.com/topic/94560-need-help-with-a-simple-array/#findComment-484187 Share on other sites More sharing options...
jbrill Posted March 5, 2008 Author Share Posted March 5, 2008 i forgot to mention, the array's are generated by a function: function loadmodel($a) { $loadsub = "SELECT * FROM dynamic_models WHERE make='".$a."' ORDER BY model ASC"; $clist = mysql_query($loadsub); $subcats = mysql_fetch_array($clist); do { echo "\"('".$subcats["model"]."','".$subcats["id"]."','".$subcats["type"]."')\","; } while($subcats = mysql_fetch_array($clist)); } Link to comment https://forums.phpfreaks.com/topic/94560-need-help-with-a-simple-array/#findComment-484190 Share on other sites More sharing options...
jbrill Posted March 5, 2008 Author Share Posted March 5, 2008 anyone? Link to comment https://forums.phpfreaks.com/topic/94560-need-help-with-a-simple-array/#findComment-484216 Share on other sites More sharing options...
jbrill Posted March 5, 2008 Author Share Posted March 5, 2008 BUMP Link to comment https://forums.phpfreaks.com/topic/94560-need-help-with-a-simple-array/#findComment-484369 Share on other sites More sharing options...
paul2463 Posted March 6, 2008 Share Posted March 6, 2008 I have tried to come up with a way to try and make it work for you, a function like what you have will not make the drop down menu, I have made it create a multi dimensional array object and return it when it is complete, then use that array object to create the menu items <?php function loadmodel($a) { $loadsub = "SELECT * FROM dynamic_models WHERE make='".$a."' ORDER BY model ASC"; $clist = mysql_query($loadsub); $subcats = mysql_fetch_array($clist); do { $newarray[] = array($subcats["model"],$subcats["id"],$subcats["type"]) } while($subcats = mysql_fetch_array($clist)); return $newarray; } $menuArray = loadmodel(whatever); // creates a multi dimensional array of the subcats $count = count($menuArray); //equals the number of entries //create the drop down menu for ($i = 0; $i < $count; $i++) { echo "<option value=\"".$menuArray[$i]['id']."\""; if ($current['model'] == $menuArray[$i]['id']) {echo "selected";} echo ">".$menuArray[$i]['model']." - ".$menuArray[$i]['type']." </option>"; } ?> Link to comment https://forums.phpfreaks.com/topic/94560-need-help-with-a-simple-array/#findComment-485363 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.