PHP if 语句在 echo 中创建 HTML 选项列表

发布于 2025-01-04 06:09:18 字数 2022 浏览 1 评论 0原文

我有一个包含 10 个数字的 HTML 选项列表。我希望根据从数据库检索的值预先选择这些选项之一。如果检索到的值为 0 或 FALSE,则应选择 value="0",如果检索到的值为 1,则应选择 value="1",等等。

我查看过: if 块位于 echo 语句内? 但是,我无法使用其信息来解决我的问题。

我还能这样处理吗?我在第一个 if 语句调用时遇到解析错误,我的语法肯定是严重错误的。

代码:

$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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千纸鹤 2025-01-11 06:09:18

使用三元运算符 ?:

echo "Blah".($somecondition == true ? "something" : "another thing")."more blah";

Use the ternary operator ?:

echo "Blah".($somecondition == true ? "something" : "another thing")."more blah";
梦旅人picnic 2025-01-11 06:09:18

您可以将该过程分为两部分:

$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);
    }
}

在此处构建您的选择菜单:

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;
}

查看示例 http://codepad.org/1O0PpQ7R

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文