Skip to content
Advertisement

PHP if statements inside of echo to create HTML option list

I have a HTML option list of 10 numbers. I want one of these options to be pre-selected based on a value retrieved from a database. If the retrieved value is 0 or FALSE then value=”0″ should be selected, if the retrieved value is 1 then value=”1″ should be selected, etc.

I have looked at: if block inside echo statement? however, I haven’t been able to fix my problem using its information.

Can I even go about it this way? I get a parse error on the first if statement call, my syntax is surely horribly wrong.

The Code:

$uv = FALSE;

        while ($r2_rows = mysqli_fetch_array($r2, MYSQLI_ASSOC)) {
            if (array_key_exists($r1_rows['article_id'], $r2_rows)) {
                $uv = $r2_rows['user_vote']; // Get user_vote value.
                $selected = 'selected="selected"';
                echo'
                    <td align="left">
                        <select class="articlevote" name="article_vote">
                            <option id="vote0" value="0"'.if($uv == 0 || $uv == FALSE) $selected.'>No Vote</option>
                            <option id="vote1" value="1"'.if($uv == 1) $selected.'>1</option>
                            <option id="vote2" value="2"'.if($uv == 2) $selected.'>2</option>
                            <option id="vote3" value="3"'.if($uv == 3) $selected.'>3</option>
                            <option id="vote4" value="4">4</option>
                            <option id="vote5" value="5">5</option>
                            <option id="vote6" value="6">6</option>
                            <option id="vote7" value="7">7</option>
                            <option id="vote8" value="8">8</option>
                            <option id="vote9" value="9">9</option>
                            <option id="vote10" value="10">10</option>          
                        </select>   
                    </td>';
            } // End of IF - article_id has a user vote.

        } // End of WHILE - checking current article_id against every user_id vote.

Advertisement

Answer

You could break the process into two parts :

$uv = FALSE;    

while ($r2_rows = mysqli_fetch_array($r2, MYSQLI_ASSOC)) {
    if (array_key_exists($r1_rows['article_id'], $r2_rows)) {
        echo buildVotes($r2_rows['user_vote'], $uv);
    }
}

Build your selectmenu here:

function buildVotes($selectedIndex, $uv){
    $noOfVotes = 10;
    $selectMenu = '<td align="left"><select class="articlevote" name="article_vote">';
    $options = "";
    for($i = 0; $i<$noOfVotes; $i++){
        if($uv == true){
            $selected = ($selectedIndex == $i) ? 'selected="selected"'  : "";
        }
        if($i == 0){
            if($uv == false){
                $selected = 'selected="selected"';
            }
            $options .= '<option id="vote'.$i.'" value="'.$i.'" '.$selected.' >No Vote</option>';
        }else{
            $options .= '<option id="vote'.$i.'" value="'.$i.'" '.$selected.' >'.$i.'</option>';
        }
    }
    $selectMenu .= $options."</select></td>";
    return $selectMenu;
}

see sample http://codepad.org/1O0PpQ7R

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement