-1

at first sorry if my english sux. Well I need a bit of help from you.

I got "Parse error: syntax error, unexpected 'if' (T_IF)" with the following code:

<?php

 $ssimple='';
 $smultiUno='';
 $smultiDos='';

 $deportes = array("Nadar","Fitness","Baloncesto");
 $cine = array("StarWars","El se&ntilde;or de los Anillos","Matrix","Pulp Fiction");
 $musica = array("Rock","Metal","Goth");

foreach ($deportes as $value) {
 $ssimple.= "<option value=\"$value\" ".if(isset($deportes) and $deportes == $value) echo 'selected="selected"'.">$value</option>";
}

foreach ($cine as $value) {
 $smultiUno.= "<option value=\"$value\"".if(isset($cine) and $cine == $value) echo 'selected="selected"'.">$value</option>";
}

foreach ($musica as $value) {
 $smultiDos.= "<option value=\"$value\"".if(isset($musica) and $musica == $value) echo 'selected="selected"'.">$value</option>";
}
?>

How can it works?

Thanks to all! :)

Edit:

Finally get the answer:

<?php
$dep='';
$cin='';
$mus='';
$sdeportes = array("Natacion",
                 "Fitness",
                 "Baloncesto",
                 "Tenis",
                 "Patinaje Artistico");
$scine = array("StarWars",
            "Matrix",
            "Indiana Jones",
            "Pulp Fiction",
            "El Hobbit");
$smusica = array("Rock",
                "Metal",
                "Goth",
                "Chill Step",
                "Alternativa");
foreach ($sdeportes as $value) {
    if(isset($deportes) and $value == $deportes){
        $a = "selected=\"selected\"";
    }else{
        $a = '';
    }
    $dep .= "<option value =\"$value\" $a>$value</option>";
}
foreach ($scine as $value) {
    if(isset($cine) and in_array($value, $cine)){
        $a="selected=\"selected\"";
    }else{
        $a='';
    }
    $cin .= "<option value =\"$value\" $a>$value</option>";
}
foreach ($smusica as $value) {
    if(isset($musica) and in_array($value, $musica)){
        $a = "selected=\"selected\"";
    }else{
        $a='';
    }
    $mus .= "<option value =\"$value\" $a>$value</option> ";
}
?>

Thanks you all guys. Can close this topic :)

i7ach1
  • 1
  • 3

1 Answers1

0

Try something like this :

 foreach ($deportes as $value) {
     $selected = (isset($deportes) and $deportes == $value) ? 'selected="selected"' : '' ;
     $simple .= '<option value="'.$value.'"'.$selected.'>'.$value.'</option>';
 }

Please ignore using condition in a string

Happy Coding
  • 2,517
  • 1
  • 13
  • 24