Hi All, I am trying to pass the value from drop down menu generated from the database and I am trying to pass the value from the first drop down selection to the next the value in red is the variable. I have tried the $category_id=$nt; $category_id=$_GET; but this does not seem to work. the value cannot be found in the next SQL query.

Thanks in advance

//Start of form

 echo "<form action=\"cat.php\" method=\"post\">\n";
 	
	$query="SELECT category_id, category_name
	FROM category ";
	// start to read database values
	$result = mysql_query ($query);
	echo "<select name=category_id value=''><option> &nbsp; Select your category &nbsp; </option>";// printing the list box select command
	
		while($nt=mysql_fetch_array($result))/* Option values are added by looping through the array */
			{		
 				echo "<option value=$nt[category_id]>&nbsp; $nt[category_name]</option>";//Array or records stored in $nt
		
			}
 
			echo "</select>";// Closing of list box */
			echo "<br/>";
			$category_id=$nt['category_id'];
			$category_id=$_GET['category_id'];

 			$category_id=2;// here I default the value and the db produces the correct result

			//$query="SELECT DISTINCT manufacturer_name, category_id
			//FROM manufacturer
			//";
			$query="SELECT DISTINCT manufacturer.manufacturer_name
			FROM category as category
			JOIN vehicles as vehicles on category.category_ID = vehicles.FK_category_ID
			JOIN products as products on products.products_id = vehicles.FK_products_ID
			JOIN manufacturer as manufacturer on manufacturer.manufacturer_ID = vehicles.FK_manufacturer_ID 
			WHERE vehicles.FK_category_ID = '$category_id' ";
			
			
			
			// start to read database values
			$result = mysql_query ($query);
			echo "<select name=manufacturer_name value=''><option> &nbsp; Select your category &nbsp; </option>";
			
			
			// printing the list box select command

		while($nt=mysql_fetch_array($result))
			{		//Array or records stored in $nt
 			  echo "<option value=$nt[manufacturer_id]>&nbsp; $nt[manufacturer_name]</option>";
		
			/* Option values are added by looping through the array */

			}
 		
					echo "</select>";// Closing of list box */

//Should this be capturing data from the highlighted red code?
 
 echo"<input type=\"hidden\" name=\"make\" value=\"$make\">";// this value is know in the page
// echo"<input type=\"hidden\" name=\"category_id\" value=\"$category_id\">";// this value is know in the page
  echo "<br/>";
  echo "<br/>";
  echo"&nbsp; &nbsp; <input type=\"submit\" value=\"Search\">\n";
  echo"</form>";

Recommended Answers

All 5 Replies

Hi David;

First let me gently suggest that the run-on sentences in your post make it hard to identify your problem. Clarity is critical when trying to explain what you need.

Also, your code needs to be in [code=php] tags so we can read it clearly and respond quoting line numbers.

Please don't take offense, I'm trying to help ;-}

Having said that, it looks like in <select name=category_id value=''> on line 9 you are trying to access a value from the database, but you haven't read a row yet, that doesn't happen till line 11, so the value is not yet accessible. You've read all the rows into an array on line 8, but you haven't actually started the loop that reads each row, that starts on line 11. That's what you need to do to access the values.

However, the name of the <select> control doesn't normally come from a database unless you are trying to loop through the categories creating a select list from each, which is a whole different ball game. Doable, but different.

I'm sort of guessing here, because your question is not clear.

I hope that helps a little bit.

Cheers,

Rob

//Start of form

 echo "<form action=\"cat.php\" method=\"post\">\n";
 	
	$query="SELECT category_id, category_name
	FROM category ";
	// start to read database values
	$result = mysql_query ($query);
	echo "<select name=category_id value=''><option> &nbsp; Select your category &nbsp; </option>";// printing the list box select command
	
		while($nt=mysql_fetch_array($result))/* Option values are added by looping through the array */
			{		
 				echo "<option value=$nt[category_id]>&nbsp; $nt[category_name]</option>";//Array or records stored in $nt
		
			}
 
			echo "</select>";// Closing of list box */
			echo "<br/>";
			$category_id=$nt['category_id'];
			$category_id=$_GET['category_id'];

 			$category_id=2;// here I default the value and the db produces the correct result

			//$query="SELECT DISTINCT manufacturer_name, category_id
			//FROM manufacturer
			//";
			$query="SELECT DISTINCT manufacturer.manufacturer_name
			FROM category as category
			JOIN vehicles as vehicles on category.category_ID = vehicles.FK_category_ID
			JOIN products as products on products.products_id = vehicles.FK_products_ID
			JOIN manufacturer as manufacturer on manufacturer.manufacturer_ID = vehicles.FK_manufacturer_ID 
			WHERE vehicles.FK_category_ID = '$category_id' ";
			
			
			
			// start to read database values
			$result = mysql_query ($query);
			echo "<select name=manufacturer_name value=''><option> &nbsp; Select your category &nbsp; </option>";
			
			
			// printing the list box select command

		while($nt=mysql_fetch_array($result))
			{		//Array or records stored in $nt
 			  echo "<option value=$nt[manufacturer_id]>&nbsp; $nt[manufacturer_name]</option>";
		
			/* Option values are added by looping through the array */

			}
 		
					echo "</select>";// Closing of list box */

//Should this be capturing data from the highlighted red code?
 
 echo"<input type=\"hidden\" name=\"make\" value=\"$make\">";// this value is know in the page
// echo"<input type=\"hidden\" name=\"category_id\" value=\"$category_id\">";// this value is know in the page
  echo "<br/>";
  echo "<br/>";
  echo"&nbsp; &nbsp; <input type=\"submit\" value=\"Search\">\n";
  echo"</form>";

Hi Rob, no offence taken at all. I am reading the drop down list from the database. correct me if I am wrong line 9 is just the first line of the option box and if I move it after line 11. I will have a repeated <option> &nbsp; Select your category &nbsp; </option> The values are read at line 13 so if you have time to explain a bit more your help would be appreciated

Thanks in advance

David

I'm starting to understand this as bit better, and I think everything up to line 18 is fine.

I've taken the order of shifting and commenting your code to fix indentation and clarify it for my own understanding, so I'll put that below and my line number will refer to the code below.

There are a couple if things I can see here.

One at line 21, I don't think the $nt values you're looking for are accessible outside the where loop that was just closed. Would have to confirm, but i'm pretty sure that's a non-starter. I believe those values are only visible (aka 'in scope') inside the where loop.

Second, are you looking for the selection of a new value in the first select list to change the content in the second select list? If so, that's entirely practical, but the answer is in JavaScript. Search Google on something like "javascript dynamic lists". A bit tricky to implement, but hugely useful for certain applications.

Also note on line 37 the name attribute is not quoted, so may cause problems.

I hope this helps.

Cheers,

Rob

// ---------- BUILD FIRST SELECT LIST: CATEGORIES ---------- //
// get categories from db
$query="SELECT category_id, category_name FROM category ";
$result = mysql_query ($query);

// open the form
echo "<form action=\"cat.php\" method=\"post\">\n";

// build the select list 
echo "<select name=category_id value=''><option> &nbsp; Select your category &nbsp; </option>";// printing the list box select command
while($nt=mysql_fetch_array($result))/* Option values are added by looping through the array */
	{
		echo "<option value=$nt[category_id]>&nbsp; $nt[category_name]</option>";//Array or records stored in $nt
	}
echo "</select>";
echo "<br/>";

// ---------- BUILD SECOND SELECT LIST: MANUFACTURERS ---------- //

// initialize some values
$category_id = $nt['category_id'];
$category_id = $_GET['category_id'];
$category_id = 2;  // here I default the value and the db produces the correct result

// set up the SQL query
$query="SELECT DISTINCT manufacturer.manufacturer_name
FROM category as category
JOIN vehicles as vehicles on category.category_ID = vehicles.FK_category_ID
JOIN products as products on products.products_id = vehicles.FK_products_ID
JOIN manufacturer as manufacturer on manufacturer.manufacturer_ID = vehicles.FK_manufacturer_ID 
WHERE vehicles.FK_category_ID = '$category_id' ";

// query the database 
$result = mysql_query ($query);

// set up the next select list.
echo "<select name=manufacturer_name value=''><option> &nbsp; Select your category &nbsp; </option>";

// printing the list box select command

while($nt=mysql_fetch_array($result))
	{		//Array or records stored in $nt
		echo "<option value=$nt[manufacturer_id]>&nbsp; $nt[manufacturer_name]</option>";

	/* Option values are added by looping through the array */

	}

				echo "</select>";// Closing of list box */

//Should this be capturing data from the highlighted red code?

echo"<input type=\"hidden\" name=\"make\" value=\"$make\">";// this value is know in the page
// echo"<input type=\"hidden\" name=\"category_id\" value=\"$category_id\">";// this value is know in the page
echo "<br/>";
echo "<br/>";
echo"&nbsp; &nbsp; <input type=\"submit\" value=\"Search\">\n";
echo"</form>";

Hi Rob, Just to clarify, both options populate the correct information from the database.

Line 23 has been defaulted to test the database output i.e. $category_id =2 which seems to be not visible from the BUILD FIRST SELECT LIST: CATEGORIES section of the code.

The value $category_id passes to a destination page and is visible at that point but is not visible in section two for the manufacturers build?

Line 53 is not required

Thanks

David

Try putting echo "Test for category_id: ".$nt['category_id']." - ".$category_id = $_GET['category_id']; around line 19 of my version.

I have a strong feeling you you'll find that those values are not in scope at that point. That's the first problem I see in the code reading from the top, so that's the first place I would concentrate.

The value would go to a second page because when you click Submit you are sending the value to that page through the action attribute of the <form> tag. It's not visible in the second section because $nt went out of scope when the where loop closed, and $_GET doesn't exist in that page - it only exists in cat.php when it is POSTed there by the Submit. (note in <form> action="cat.php" method="post")

Rob

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.