使用php为选择框创建选项

使用php为选择框创建选项,php,html,Php,Html,我想创建一个非常简单的框,其中只包含数字、1、2、3等。。所以很简单我想要 <select> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> <option>6</option>

我想创建一个非常简单的
框,其中只包含数字、1、2、3等。。所以很简单我想要

<select>
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
  <option>6</option>
</select>

1.
2.
3.
4.
5.
6.
但是,我希望使用PHP生成选项,而不是手动创建所有选项

我该怎么做呢?

您可以使用以下方法:

<select>

<?php

    define('MAX_OPTIONS', 6);

    for($optionIndex=1; $optionIndex <= MAX_OPTIONS; $optionIndex++){
        echo '<option>' . $optionIndex . '</option>';
    }

?>

</select>

请注意,
的open和close标记是直接输出,而不是PHP代码,因此是PHP标记

您也可以通过PHP打印所有内容:

<?php

    define('MAX_OPTIONS', 6);

    echo '<select>';
    for($optionIndex=1; $optionIndex <= MAX_OPTIONS; $optionIndex++){
        echo '<option>' . $optionIndex . '</option>';
    }
    echo '</select>';

?>

提示

最后,为了向更结构化的编程迈出一小步,您可以创建一个函数:

<?php

    function createSelectBox($optionCount){
        $out = '<select>';
        for($idx=1; $idx <= $optionCount; $idx++){
            $out .= '<option>' . $idx . '</option>';
        }
        $out .= '</select>';
        return $out;
    }

?>

然后从PHP(!!)中调用它,如下所示:

<?php

    echo createSelectBox(6);

?>

由于生成的HTML代码外观美观且功能强大,因此它不会有任何实际用途,因为如果没有为
标记提供
属性(该属性应包含表示选项的值),任何选择框都无法工作

对于($i=0;$i


此函数用于输出请求的代码。

您可以使用sprintf创建选择框,以便于组织数据

<?php

    function createSelectBox($arr){

        $options= '<select>';
        for($i=0; $i <= count($arr); $i++){
            $options .= sprintf("<option value='%s'>%s</option>", $arr[$i]['ID'], $arr[$i]['title']);
        }
        $options .= '</select>';
        return $options;
    }
?>

我在寻找代码片段时遇到了这个问题。我最终编写了一个函数,它的调用以及与之相关的所有内容(尽管在这里我将CSS放在样式标记中,而不是CSS文件中)

你可以把这个放进去,它会工作的

            <?php
            function createOptionBox($boxName,$boxClass ,$productArray){

                $boxHtml = '<select name="'.$boxName.'" class="'.$boxClass.'">';

                for($x=0;$x<count($productArray);$x+=2){

                    $boxHtml .= '<option value="'.$productArray[$x].'">'.$productArray[$x+1].'</option>';

                }

                $boxHtml .= '</select>';

                return($boxHtml);
            }

            $productArray = array("1","ipad","2","phone");
            $boxName = "products";
            $boxClass = "productSelect";

            $productDropDown = createOptionBox($boxName,$boxClass ,$productArray);
            ?>
            <style>

                .productSelect{
                    font-size:12pt;
                    padding: 5px;
                }

            </style>

            <p><?php echo $productDropDown;?></p>

谢谢,这非常有效。现在我想将所选值(一旦选择一个)存储到变量
$number
中。我该怎么做?这需要您从
表单中发回数据(其中应包括服务器上的PHP脚本的
select
,然后由PHP脚本执行其余操作。不过,这需要您首先为每个
选项添加
属性,并为
select
提供
名称
属性(如前所述,使用包含
选择
表单
)(通过使用javascript将请求设置为AJAX,但这太高级了,在您的编码环境中可能没有任何意义。无论如何,如果您还有其他问题,请在单独的线程中提问,因为(我假设)您当前的问题已经得到了回答。选中标记您的答案,并在此处创建了新问题:我真的不认为在此上下文中定义全局变量是正确的。您甚至不会多次使用它。
<?php

    function createSelectBox($arr){

        $options= '<select>';
        for($i=0; $i <= count($arr); $i++){
            $options .= sprintf("<option value='%s'>%s</option>", $arr[$i]['ID'], $arr[$i]['title']);
        }
        $options .= '</select>';
        return $options;
    }
?>
            <?php
            function createOptionBox($boxName,$boxClass ,$productArray){

                $boxHtml = '<select name="'.$boxName.'" class="'.$boxClass.'">';

                for($x=0;$x<count($productArray);$x+=2){

                    $boxHtml .= '<option value="'.$productArray[$x].'">'.$productArray[$x+1].'</option>';

                }

                $boxHtml .= '</select>';

                return($boxHtml);
            }

            $productArray = array("1","ipad","2","phone");
            $boxName = "products";
            $boxClass = "productSelect";

            $productDropDown = createOptionBox($boxName,$boxClass ,$productArray);
            ?>
            <style>

                .productSelect{
                    font-size:12pt;
                    padding: 5px;
                }

            </style>

            <p><?php echo $productDropDown;?></p>