Print record from a table

Hi All, this is my first post (but wont be my last!)

I have created a three tier drop down function using php.

The purpose is to locate car parts.

The first drop down is make, the second is model and the third submodel.

I have created a database with these 3 columns and have added a fourth column with the actual car part data e.g 0123456

My code is below;

[php]<?php

function getTierOne()
{
$result = mysql_query(“SELECT DISTINCT make FROM carparts”)
or die(mysql_error());

  while($tier = mysql_fetch_array( $result )) 

	{
	   echo '<option value="'.$tier['make'].'">'.$tier['make'].'</option>';
	}

}

if($_GET[‘func’] == “drop_1” && isset($_GET[‘func’])) {
drop_1($_GET[‘drop_var’]);
}

function drop_1($drop_var)
{
include_once(‘db.php’);
$result = mysql_query(“SELECT DISTINCT model FROM carparts WHERE make=’$drop_var’”)
or die(mysql_error());

echo '<select name="drop_2" id="drop_2">
      <option value=" " disabled="disabled" selected="selected">Choose one</option>';

	   while($drop_2 = mysql_fetch_array( $result )) 
		{
		  echo '<option value="'.$drop_2['make'].'">'.$drop_2['make'].'</option>';
		}

echo '</select>';
echo "<script type=\"text/javascript\">

$(’#wait_2’).hide();
$(’#drop_2’).change(function(){
$(’#wait_2’).show();
$(’#result_2’).hide();
$.get(“func.php”, {
func: “drop_2”,
drop_var: $(’#drop_2’).val()
}, function(response){
$(’#result_2’).fadeOut();
setTimeout(“finishAjax_tier_three(‘result_2’, '”+escape(response)+"’)", 400);
});
return false;
});
";
}

if($_GET[‘func’] == “drop_2” && isset($_GET[‘func’])) {
drop_2($_GET[‘drop_var’]);
}

function drop_2($drop_var)
{
include_once(‘db.php’);
$result = mysql_query(“SELECT * FROM carparts WHERE model=’$drop_var’”)
or die(mysql_error());

echo '<select name="drop_3" id="drop_3">
      <option value=" " disabled="disabled" selected="selected">Choose one</option>';

	   while($drop_3 = mysql_fetch_array( $result )) 
		{
		  echo '<option value="'.$drop_3['submodel'].'">'.$drop_3['submodel'].'</option>';
		}

echo '</select> ';
echo '<input type="submit" name="submit" value="Submit" />';

}

?>[/php]

The fourth column is called “parts” - I can’t seem to workout how to print data from the fourth column when the user has selected the previous three columns - I hope this makes sense!

Thank you.

Well, I do not see any display of “parts” or any reference to it in your code.
You did mention about a fourth col named parts. Now, you load the first drop-down (select) and when
this is changed, you load drop-down #2. When, drop-down 2 is changed, you load drop-down #3.
All of these are done thru queries.

So, if you have the list of 3 drop-downs. Each are the 3 cols of data. Using these three, you would
need to do another query. Use the data in each of the first 3 drop-downs (Select’s) and query the database
to pull all “parts” for the 3 selections. The query would need to query on all of the first 3. The “WHERE” clause would have all 3 in it. Then, do what you want with the data. You can access a drop-down’s actual value with PHP with something like $value3=$_POST[‘drop_3’]; When a value is posted the current value of the drop-down will be the posted value. You can also use it in Javascript or Jquery just as you did before with something like: drop_var: $(’#drop_3’).val()

Okay, so you already understand the basic’s. Just run one more query and pull the parts, then process them
as you need to. Either show the parts if the user needs to select one part or list them all by displaying them.

Not sure if that is what you were asking, but, if not explain further… Good luck…

Sponsor our Newsletter | Privacy Policy | Terms of Service