display label based on dropdown value from mysql

hi,

am new to php. Have managed to display the contents of a table called menuitems from mysql into a dropdown. Next i need to change the label value based on the dropdown value selected. For eg. if the menuItem is selected from the dropdown i need the corresponding price to be displayed in the label. Am not sure how to do this as the label has no value attribute. Please advise

[php]

<? @ $db = mysql_pconnect("localhost","root","administrator"); if (!$db) { echo "Error: Could not connect to database. Please try again later."; exit; } mysql_select_db("orders"); $query = "select * from menuitems "; $result = mysql_query($query); $num_results = mysql_num_rows($result); for($i=0; $i < $num_results; $i++) { $row = mysql_fetch_array($result); echo "".$row["itemPrice"].""; } echo ""; ?>
[/php]

Just to start with your query can hold the price list of the id that you have already queried with a where clause on it say menuid is the field name and the variable from dropdown if coming from another page or you will have to do some jquery.

$query = “select * from menuitems WHERE menuid=’$id’”;

[php]echo “<label for=“itemPrice” value=”.$row[“itemPrice”].">".$row[“itemPrice”]."";[/php]needs to be
[php]echo “<option for=“itemPrice” value=”.$row[“itemPrice”].">".$row[“itemPrice”]."";[/php]

or
[php]echo “”.$row[“itemPrice”]."";[/php]

hi,

when i enter the code as mentioned in the previous post
[php]
echo “”.$row[“itemPrice”]."";
[/php]
or
[php]
echo “<option for=“itemPrice” value=”.$row[“itemPrice”].">".$row[“itemPrice”]."";
[/php]

i get an error message

Parse error: syntax error, unexpected T_STRING, expecting ‘,’ or ‘;’ in C:\xampp\htdocs\orders\tableorders.php on line 108

I have not changed anything in your code apart from turning label in to option, removing the value from a label because there is no value in a label.

So if you get the error now it was there before surely ?

But would try single quotes in your array values’s
[php]$row[‘itemPrice’][/php]

What does for do should it be id ?

for="

Also you select everything in your query when all you need is the itemPrice you should get used to using just what you want from the row.

there was no error before. if i comment out the line
[php]
echo “”.$row[“itemPrice”]."";
it does not show any error on the page. Have tried setting $row[‘itemPrice’] but the double quote is not an issue.
[/php]
my query is
[php]
$query = “select * from menuitems where itemPrice = ‘itemPrice’”;
[/php]
Please advise.

The value of the FOR attribute must match the value of the associated form control’s ID attribute.

[php]
echo “”.$row[“itemPrice”]."";[/php].

after this i still get an error on the line.

I do not really believe you that there is no error on your code but if you say so.
I know where the eror is its because you have double quotes around everything.

[php]echo “”.$row[‘itemPrice’]."";[/php]

The itemPrice in the single quotes must be a number or a variable, itemPrice is just a word you do not know what the itemPrice value is yet so how will mysql know what it is in the query?

[php]$query = “select * from menuitems where itemPrice = ‘itemPrice’”;[/php]

The where clause needs to be something that you know already like the id of it.

forgot to mention to you that the field names in the menutiems table consists of ID, itemName, itemPrice.

FOR is for form label not just a label.

How do you get the drop down values ?

Next i need to change the label value based on the dropdown value selected

also there is no opening for the select tag or form so this is redundant.
[php]echo “”;[/php]

this is the code that gets the itemName and displays it in the dropdown.

[php]

Item Name:

Select an option
<?
@ $db = mysql_pconnect(“localhost”,“root”,“administrator”);
if (!$db)
{
echo “Error: Could not connect to database. Please try again later.”;
exit;
}
mysql_select_db(“orders”);
$query = "select itemName from menuitems aesc ";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
for($i=0; $i < $num_results; $i++)
{
	$row = mysql_fetch_array($result);
	//echo htmlspecialchars( stripslashes($row["tableNo"])); 
	echo "<option value=".$row["itemName"]." >".$row["itemName"]."</option>"; 
	//echo "</p>";
}	echo "</select>";	

	?>[/php]

To get the itemPrice, below is the code
Have left out the statement to connect to the database as this statement is declared above as it opens a persistent connection.
Have also remove the element below.

[php]

Item Price:

<?
if (!$db)
{
echo “Error: Could not connect to database. Please try again later.”;
exit;
}
mysql_select_db(“orders”);
$query = “select * from menuitems where itemPrice = ‘itemPrice’”;
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
for($i=0; $i < $num_results; $i++)
{
	$row = mysql_fetch_array($result);
	//echo htmlspecialchars( stripslashes($row["tableNo"])); 
	//echo "<option value=".$row["itemName"]." >".$row["itemName"]."</option>"; 
	//echo "</p>";
	//echo "<label for="itemPrice" value=".$row["itemPrice"].">".$row["itemPrice"]."</label>";
	//echo "<option for="itemPrice" value=".$row["itemPrice"].">".$row["itemPrice"]."</option>";
	echo "<label for="itemPrice" id="itemPrice">".$row["itemPrice"]."</label>";
}		

	?>[/php]

i think that my $query is wrong, so will work on this.

by changing $query = “select itemPrice from menuitems where itemName = “.$row[“itemName”].””;

am not sure if this will work as row[‘itemName’] is declared later in the for loop.

The problem is you need to send the form option you picked to start with to show the itemprice (if you want the result to show on the same page at the same time then its will need jquery rather than posting the result).
So by sending the value of the id in the menu form to the next part the label the id(or the itemName) is all you need to get the label result, but the id is much easy and a better result than the name because then you could have errors in the name white space and duplicates.

I am guessing you left out the form tags ?

The first query could be
[php]$query = "select itemName, id from menuitems aesc ";[/php]

Then use the id as the value for the label value where clause.

with a
[php]$_GET[‘id’];[/php]
or
[php]$_POST[‘id’];[/php]

[php]
$id = int($_POST[‘id’]);
$query = “select * from menuitems where itemPrice = ‘$id’”;[/php]

thanks for this. All making sense now. Will give jquery a try as i need the value displayed on the same page.

You can get the value displayed on the same page but you will need to submit the form to get the result.
Jquery will get the result without a refresh of the page.

Sponsor our Newsletter | Privacy Policy | Terms of Service