Ad:
 
  • PHP Discussion Thread
  • Unsolved
  • Views: 5993
  • PHP RSS
Similar Threads
Jun 8th, 2010
0

PHP value problem

Expand Post »
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']; $category_id=$_GET['category_id']; 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>";
Reputation Points: 10
Solved Threads: 0
Light Poster
davidjennings is offline Offline
43 posts
since Sep 2009
Jun 8th, 2010
0

Re: PHP value problem

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


php Syntax (Toggle Plain Text)
  1. //Start of form
  2.  
  3. echo "<form action=\"cat.php\" method=\"post\">\n";
  4.  
  5. $query="SELECT category_id, category_name
  6. FROM category ";
  7. // start to read database values
  8. $result = mysql_query ($query);
  9. echo "<select name=category_id value=''><option> &nbsp; Select your category &nbsp; </option>";// printing the list box select command
  10.  
  11. while($nt=mysql_fetch_array($result))/* Option values are added by looping through the array */
  12. {
  13. echo "<option value=$nt[category_id]>&nbsp; $nt[category_name]</option>";//Array or records stored in $nt
  14.  
  15. }
  16.  
  17. echo "</select>";// Closing of list box */
  18. echo "<br/>";
  19. $category_id=$nt['category_id'];
  20. $category_id=$_GET['category_id'];
  21.  
  22. $category_id=2;// here I default the value and the db produces the correct result
  23.  
  24. //$query="SELECT DISTINCT manufacturer_name, category_id
  25. //FROM manufacturer
  26. //";
  27. $query="SELECT DISTINCT manufacturer.manufacturer_name
  28. FROM category as category
  29. JOIN vehicles as vehicles on category.category_ID = vehicles.FK_category_ID
  30. JOIN products as products on products.products_id = vehicles.FK_products_ID
  31. JOIN manufacturer as manufacturer on manufacturer.manufacturer_ID = vehicles.FK_manufacturer_ID
  32. WHERE vehicles.FK_category_ID = '$category_id' ";
  33.  
  34.  
  35.  
  36. // start to read database values
  37. $result = mysql_query ($query);
  38. echo "<select name=manufacturer_name value=''><option> &nbsp; Select your category &nbsp; </option>";
  39.  
  40.  
  41. // printing the list box select command
  42.  
  43. while($nt=mysql_fetch_array($result))
  44. { //Array or records stored in $nt
  45. echo "<option value=$nt[manufacturer_id]>&nbsp; $nt[manufacturer_name]</option>";
  46.  
  47. /* Option values are added by looping through the array */
  48.  
  49. }
  50.  
  51. echo "</select>";// Closing of list box */
  52.  
  53. //Should this be capturing data from the highlighted red code?
  54.  
  55. echo"<input type=\"hidden\" name=\"make\" value=\"$make\">";// this value is know in the page
  56. // echo"<input type=\"hidden\" name=\"category_id\" value=\"$category_id\">";// this value is know in the page
  57. echo "<br/>";
  58. echo "<br/>";
  59. echo"&nbsp; &nbsp; <input type=\"submit\" value=\"Search\">\n";
  60. echo"</form>";
Reputation Points: 10
Solved Threads: 4
Junior Poster in Training
scaiferw is offline Offline
71 posts
since May 2010
Jun 8th, 2010
0

Re: PHP value problem

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
Reputation Points: 10
Solved Threads: 0
Light Poster
davidjennings is offline Offline
43 posts
since Sep 2009
Jun 8th, 2010
0

Re: PHP value problem

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['attribute'] 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

php Syntax (Toggle Plain Text)
  1. // ---------- BUILD FIRST SELECT LIST: CATEGORIES ---------- //
  2. // get categories from db
  3. $query="SELECT category_id, category_name FROM category ";
  4. $result = mysql_query ($query);
  5.  
  6. // open the form
  7. echo "<form action=\"cat.php\" method=\"post\">\n";
  8.  
  9. // build the select list
  10. echo "<select name=category_id value=''><option> &nbsp; Select your category &nbsp; </option>";// printing the list box select command
  11. while($nt=mysql_fetch_array($result))/* Option values are added by looping through the array */
  12. {
  13. echo "<option value=$nt[category_id]>&nbsp; $nt[category_name]</option>";//Array or records stored in $nt
  14. }
  15. echo "</select>";
  16. echo "<br/>";
  17.  
  18. // ---------- BUILD SECOND SELECT LIST: MANUFACTURERS ---------- //
  19.  
  20. // initialize some values
  21. $category_id = $nt['category_id'];
  22. $category_id = $_GET['category_id'];
  23. $category_id = 2; // here I default the value and the db produces the correct result
  24.  
  25. // set up the SQL query
  26. $query="SELECT DISTINCT manufacturer.manufacturer_name
  27. FROM category as category
  28. JOIN vehicles as vehicles on category.category_ID = vehicles.FK_category_ID
  29. JOIN products as products on products.products_id = vehicles.FK_products_ID
  30. JOIN manufacturer as manufacturer on manufacturer.manufacturer_ID = vehicles.FK_manufacturer_ID
  31. WHERE vehicles.FK_category_ID = '$category_id' ";
  32.  
  33. // query the database
  34. $result = mysql_query ($query);
  35.  
  36. // set up the next select list.
  37. echo "<select name=manufacturer_name value=''><option> &nbsp; Select your category &nbsp; </option>";
  38.  
  39. // printing the list box select command
  40.  
  41. while($nt=mysql_fetch_array($result))
  42. { //Array or records stored in $nt
  43. echo "<option value=$nt[manufacturer_id]>&nbsp; $nt[manufacturer_name]</option>";
  44.  
  45. /* Option values are added by looping through the array */
  46.  
  47. }
  48.  
  49. echo "</select>";// Closing of list box */
  50.  
  51. //Should this be capturing data from the highlighted red code?
  52.  
  53. echo"<input type=\"hidden\" name=\"make\" value=\"$make\">";// this value is know in the page
  54. // echo"<input type=\"hidden\" name=\"category_id\" value=\"$category_id\">";// this value is know in the page
  55. echo "<br/>";
  56. echo "<br/>";
  57. echo"&nbsp; &nbsp; <input type=\"submit\" value=\"Search\">\n";
  58. echo"</form>";
Last edited by scaiferw; Jun 8th, 2010 at 2:28 pm.
Reputation Points: 10
Solved Threads: 4
Junior Poster in Training
scaiferw is offline Offline
71 posts
since May 2010
Jun 8th, 2010
0

Re: PHP value problem

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
Reputation Points: 10
Solved Threads: 0
Light Poster
davidjennings is offline Offline
43 posts
since Sep 2009
Jun 8th, 2010
0

Re: PHP value problem

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['category_id'] went out of scope when the where loop closed, and $_GET['category_id'] 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
Reputation Points: 10
Solved Threads: 4
Junior Poster in Training
scaiferw is offline Offline
71 posts
since May 2010
Message:
Previous Thread in PHP Forum Timeline: sql packet size
Next Thread in PHP Forum Timeline: problem on screen scraping





About Us | Contact Us | Advertise | Acceptable Use Policy
Build Custom RSS Feed


Follow us on Twitter


© 2010 DaniWeb® LLC