Jump to content

[SOLVED] Make -> Model. Pull models from the database after 'Make' is selected.


tontox5

Recommended Posts

Hello lads, I'm new here and was hoping I could get some help with a hurdle I ran into.  I'm making a motorcycle classified site and have all of my makes and models stored in a mysql database. What I want to do is have the Models load after Make is selected without having to submit a form. Do I need to use javascript? Here is my code:

 

<form name="form1" method="post" action="test-manufacturers.php"><?php 
mysql_connect('localhost','***','***');
mysql_select_db('database');

if (isset($_REQUEST['make'])) $make=$_REQUEST['make'];

$result= mysql_list_fields ('database','manufacturers');
$max=mysql_num_fields($result);


//start of the drop down menu
echo "<select name='make'>";

$farray=Array();
$i=0;

for ($x=0; $x < $max; $x++) {

if ($make==mysql_field_name($result, $x)) {
$farray[$i++]="<option value='". mysql_field_name($result, $x)."' selected>". mysql_field_name($result, $x). "</option >";
}
else {
$farray[$i++]="<option value='". mysql_field_name($result, $x)."'>". mysql_field_name($result, $x). "</option>";
}

}

sort($farray);

foreach($farray as $f){
echo $f;
}

echo "</select><br/><br/>";
// end of the drop down menu


if (isset($make)) {

echo "<select name='model'>";

$sql="SELECT * FROM manufacturers WHERE $make !=''";
$result= mysql_query($sql);
$farray=Array();

while ($row = mysql_fetch_array($result)) {
$farray[$i++]="<option value='". $row[$make]."'>". $row[$make]. "</option>";
}
sort ($farray);

foreach($farray as $f){
echo $f;
}
echo "<option value='Other'>Other</option>";
echo "</select><br/><br/>";

}

?>
  <input type="submit" name="submit" id="submit" value="List">
</form>

 

 

Link to comment
Share on other sites

No offense to the writer, the concept looks good and there are 50,000 views, but the example is very hard to understand. The writer says it all works as one file, but then says "you can replace with" and then finally says "try this first" for just the bottom snippet of code.

 

I copied and pasted the code, and I am getting the following error

 

Parse error: syntax error, unexpected T_IF in /var/www/vhosts/***.com/httpdocs/menu.php on line 26

 

It is being caused by the following code, line 26 being

if (!mysql_query("SELECT * FROM table WHERE ID=$P"))

 

$dbh = mysql_connect($hostname, $username, $password) 
or die("Unable to connect to MySQL");
$selected = mysql_select_db("first_test",$dbh) 
if (!mysql_query("SELECT * FROM table WHERE ID=$P"))
{
  echo "Database is down";
}
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
   $NewData .= "<option value='".$row['ID']."'>".$row['Name']."</option>\n";
}
mysql_close($dbh);

 

Any ideas?

Link to comment
Share on other sites

if you have a structure like this, with numeric PKs and FKs then the baaSelect link in my sig should help. Otherwiise there's an ajax sample too.

[pre]

make              model

---------        -------------

id (PK)  ---+    id  (PK)

make        |    model

            +---< makeID (FK)

Link to comment
Share on other sites

Thank you Barand, your baaSelect example was exactly what I needed. It's much simpler than the first link. Now that it works, I just need to analyze it until I understand it fully or my brain explodes, whichever comes first.

 

Thanks for your help, solved.

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.