?
Solved

option value in php

Posted on 2009-07-14
14
Medium Priority
?
275 Views
Last Modified: 2012-05-07
Hi

please find attached snippet of my code the problem that $val brings me the last value of the table (database table called type)

<form action="form.php" method="GET" >
<fieldset><legend><b>Type Product</b></legend>
<table><tr>
<td>Type Product:</td>
<?php echo "<option value='$val'>$tt</option>";?>
<td><select name="type" size="1"> 
<?php
$query="select * from type";
$result=mysql_query($query);
$i=0;
          for($i=0;$i<mysql_num_rows($result);$i++){
              $tt=mysql_result($result,$i,"type");
              $type=mysql_result($result,$i,"type_id");
              $val=mysql_result($result,$i,"valeur");
               
              echo "<option value='$type'>$val.$tt</option>\n";
        
              
  }
?>    
<td><input type="hidden" name="val" value='<?php echo $val;?>'</td></tr> 
</select>  
</tr></table>
<table>
<tr><td><input type="submit" name="submit" value="Submit"></td></tr></table>
<?php echo"<h2><a href='index.php'>Return to The Main Menu</a></h2>";?>
</table>
</form>
</body>
</html>

Open in new window

0
Comment
Question by:asaidi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 24847789
Hi asaidi,

what do you want '$val' to be set to? In the code you iterate through all entries in the table 'type' - with each iteration you set '$val' to the value of 'valeur', so after the for-loop '$var' contains the last entries 'valeur'.

ZOPPO
0
 
LVL 14

Expert Comment

by:flob9
ID: 24847796
Since you use the $val variable in the loop, it's normal to get it to the last value from the database ...

Use another variable name inside the loop.
0
 

Author Comment

by:asaidi
ID: 24847857
Hi
$val i want to set for the value i choose in my <option value>
there is valeur in my database set to 21.5 and when i choose it ,i have everytimes the value 1 means the last value of my database table
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 14

Expert Comment

by:profya
ID: 24850266
Replace your php script block with this:
<?php
$query="select * from type";
$result=mysql_query($query);
if ($result && mysql_num_rows($result)>)
while($rec=mysql_fetch_array($result)
{
	echo "<option value='".$rec['type_id']."'>".$rec['valeur'].".".$rec['type']."</option>\n";
}
?>    

Open in new window

0
 

Author Comment

by:asaidi
ID: 24850382
Hi PROFYA

if ($result && mysql_num_rows($result)>)
greater in what ?

and how i can pass then the $rev['valeur'] to a variable to another page
0
 
LVL 14

Expert Comment

by:profya
ID: 24850480
>0
Sorry for that. This checks if there is data returned by the query.
0
 
LVL 14

Expert Comment

by:profya
ID: 24850522
In this case you need to use javascript. When the user selects an option then you pass it to the hidden input. When the form is then posted, the value is sent. Why? Because you have a loop, the last valuer will then be sent.
I'll give you an example:
0
 
LVL 14

Expert Comment

by:profya
ID: 24850591
Change the select tag to:
<select name="type" size="1" onchange="document.forms[0].elements.val.value=this.options[this.selectedIndex].value">

Open in new window

0
 
LVL 7

Expert Comment

by:ajkhalifa
ID: 24850676
cancel the code from line 11 till line 16, and instead put this:
while($R=mysql_fetch_array($result)){
              echo "<option value='".$R[0]."'>".$R[1].".".$R[2]."</option>\n"
}
0
 
LVL 14

Expert Comment

by:profya
ID: 24850703
"and how i can pass then the $rev['valeur'] to a variable to another page"
Since your select is within the form, when form is posted, its value (user selection) sent with other form inputs, you can get the value from the other page via:
$_GET['type']
But you don't want to send the type you want them both the type and the value, you choosed to add a new hidden input, the code I attached, updates the hidden input with the value selected by the user.

You can also acheive this by butting both the type and value in the option attribute VALUE, separated by comma. Then when you get the value in the other page you split the value by comma. For example:
change the option line to:
echo "<option value='".$rec['type_id'].", ".$rec['valeur']."'>".$rec['valeur'].".".$rec['type']."</option>\n"
each option value now holds the type and the value. In the other form
list($mytype, $myvalue)=split(",", $_GET['type']);
echo $mytype;
echo $myvalue;

I hope this helps.
You added a new hidden input
0
 

Author Comment

by:asaidi
ID: 24851192
Hi Profya

it is working but the type holds the type and the value and how i can make seperates because i have to select from a table with where type =$type for the moment i have (type,value)
and how i can make them type=type and value=value(= not equal it is the set option)
0
 
LVL 14

Expert Comment

by:profya
ID: 24851229
What technique you are using now, the javascript or the joining of the two into one?
0
 

Author Comment

by:asaidi
ID: 24851245
no not the java one the second technique
0
 
LVL 14

Accepted Solution

by:
profya earned 2000 total points
ID: 24851302
Since the select input type now holds the type and the value, when the form is submitted, $_GET['type'] gives us the selection, for example: "type1, 2091"
All we need is to split it by comma, to get "type1" in a variable and then "2091" in a variable. The code attached does this, it splits the value into two:
$mytype which contains the type
$myvalue which contains the value.

Then you can use these variables in the rest of your code.
list($mytype, $myvalue)=split(",", $_GET['type']);
echo $mytype;
echo $myvalue;

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question