dsh1956 2012-02-10 18:41
浏览 93
已采纳

PHP if语句里面的echo创建HTML选项列表

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.
  • 写回答

2条回答 默认 最新

  • doushi1847 2012-02-10 19:21
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题