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
Share on other sites

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

 

 

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.