Jump to content

need help with a simple array


jbrill

Recommended Posts

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

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));
}

 

 

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>";
}
?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.