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
Solved

option value in php

Posted on 2009-07-14
14
271 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How would I do this...? 2 32
Is there an elegant way to view sql statements written in PDO? 2 22
php56-php-mcrypt for rhel7 php56 1 42
IP 10.0.1.2 / 255.0.0.0 61 54
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
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 dynamically set the form action using jQuery.

765 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