tags:

views:

118

answers:

6

need help...

the getting input form:

<select name="age">     
<option value="25-30">25-30</option>
<option value="31-40">31-40</option>        
<option value="41-50">41-50</option>
<option value="51-60">51-60</option>
</select>

example url

search/?age=25-30

the function php:

if ( $_GET['age'] !="25-30") $age = '("A")'; 
elseif ( $_GET['age'] !="31-40") $age = '("B")'; 
elseif ( $_GET['age'] !="41-50") $age = '("C")'; 
elseif ( $_GET['age'] !="51-60") $age = '("D")'; 
else ( $_GET['age'] !="25-30") $age = '("A")'; 

$search ="http://domain.com/?q='.$age;

the problem: the $age inside $search always returns A (or 25-30) even though i have selected other values (31-40, 41-50, 51-60)

please help..thanks

+2  A: 

I think you mean

==

not

!=

Delan Azabani
+5  A: 

else should represent a default value and ( $_GET['age'] !="25-30") should be removed from it and here is what you should have actually:

if ( $_GET['age'] =="25-30") $age = '("A")'; 
elseif ( $_GET['age'] =="31-40") $age = '("B")'; 
elseif ( $_GET['age'] =="41-50") $age = '("C")'; 
elseif ( $_GET['age'] =="51-60") $age = '("D")'; 
else $age = '("A")'; // modify accordingly
Sarfraz
thanks..it works now :p
webdev28
@webdev28: You are welcome :)
Sarfraz
+2  A: 

You could do this cleaner using a switch statement or an array with values, but before going into examples: Wouldn't it be easier to just set the value to A, B, C... in the select in the first place?

Re @Col.Shrapnels comment. Which one looks better. The if soup above, or this?

switch($_GET["age"])
 {
   case "25-30": $age = "A"; break;
   case "31-40": $age = "B"; break;
   case "41-50": $age = "C"; break;
   case "51-60": $age = "D"; break;
   default:      $age = "E"; break;  // or whatever

 }
Pekka
Oh no. Why switch? I think it's most useless operator. For the few comparisons `if` is much more readable and arrays for the rest.
Col. Shrapnel
@Col Why not? Switch makes the code much, much better readable IMO. See my update. Don't you think? Are there any actual downsides to using it? Anyway, anything beyond 10 elements should go into an array.
Pekka
In my opinion. `if` notation is much nearer to the natural language. Though it doesn't really matter and only matter of taste. Anyway. I'd go for array if number of choices more than 2.
Col. Shrapnel
+2  A: 

How do you think, what does != operator mean?

Anyway,

<select name="age">     
<option value="A">25-30</option>
<option value="B">31-40</option>        
<option value="C">41-50</option>
</select>
Col. Shrapnel
i did not realize the != :pthe select value would be different if i use the above
webdev28
@web yeah, that's intention of this code. if you search for your own site, the code would be even smaller.
Col. Shrapnel
@webdev: You want value `A` for `25-30`, `B` for `31-40`, etc, why not just define that value in the `<option>` directly? That's the whole point of this answer. This way you can just do `$age = $_GET['age'];` without hassling with cumbersome `if-else` blocks.
BalusC
+1  A: 

You are using the "Not Equal To" ("!=") Operator, and so, unless you select 25-30 from the list (which should result in (B), it will always stop at the first line.

For the full code, I would use:

switch( $_GET['age'] ) {
  case '25-30' :
    $age = '("A")'; break;
  case '31-40' :
    $age = '("B")'; break;
  case '41-50' :
    $age = '("C")'; break;
  case '51-60' :
    $age = '("D")'; break;
  default :
    $age = '("A")';
}
Lucanos
hm..i am not sure if i should use switch
webdev28
It is an option, whether it is the best or not, of course, depends on the circumstance.
Lucanos
A: 
if ( $_GET['age'] !="25-30") $age = '("A")'; 
elseif ( $_GET['age'] !="31-40") $age = '("B")'; 
elseif ( $_GET['age'] !="41-50") $age = '("C")'; 
elseif ( $_GET['age'] !="51-60") $age = '("D")'; 
else ( $_GET['age'] !="25-30") $age = '("A")'; 
-----^^^^^^^^^^^^^^^^^^^^^^^^^

Parse error: syntax error, unexpected T_VARIABLE in test.php on line 5

I am wondering why there are so many answers!

Salman A
@Salman A: What have you posted here, read the answers of others, you have posted the wrong code. When there are so many correct answers unlike you, vote them up actually :)
Web Logic
I bothered to run the code posted by OP BEFORE answering, seems like he posted the wrong code.
Salman A
@Salman A: Exactly and that is the reason he has posted that code here so that we could correct him.
Web Logic