简体   繁体   English

如何使用下拉框创建一个for循环

[英]how to create a for loop with a drop down box

Hi I am trying to create a set of drop down boxes that will use an array of data from the values that you pick and then runs through a loop to post them to the screen at the moment the data that i want to use will just be local but i want to edit this later so that it will loop through the data from my database and post that to the screen. 嗨,我正在尝试创建一组下拉框,这些下拉框将使用您选择的值中的数据数组,然后通过循环运行以将它们发布到屏幕上,而此时我要使用的数据将只是本地,但我想稍后编辑它,以便它将遍历数据库中的数据并将其发布到屏幕上。 i have looked at other questions on this subject and just wondering how i would change it for my code i have looked at this link questions on stack overflow that I have looked at i have just got a couple questions that im wondering if anybody has seen this before or if they have seen any examples i have also looked at for loops and i understand the concept 我已经看过这个问题的其他问题,只是想知道我将如何为我的代码更改它,我已经看过我在堆栈溢出时看到的这个链接问题,我刚刚遇到了几个问题,我想知道是否有人看到过这个问题在他们看过任何示例之前或之前,我也查看过循环,并且我理解了这个概念

my questions to you are: 我对您的问题是:

1) how would I post the values from my drop down boxes into a php array 2) how would I then check the values against and array of data and then choose which are correct and post them to the screen. 1)我将如何将下拉框中的值发布到php数组中2)然后我将如何对照数据和数组检查值,然后选择正确的值并将其发布到屏幕上。 3)Would I need to use a second language like javascript or can it be done just in php 3)我需要使用第二种语言,例如javascript还是可以在php中完成

My drop down box code is 我的下拉框代码是

<div id="Content">

    <?php include "select.class.php"; ?>
    <form id="select_form">
        Choose a category:<br />
        <select id="category">
            <?php echo $opt->ShowCategory(); ?>
        </select>
        <br /><br />

        Choose a type:<br />
        <select id="type">
            <option value="%">any...</option>
        </select>
        <br /><br />

        Choose a principle:<br />
        <select id="principle">
            <option value="%">any...</option>
        </select>
        <br /><br />

        <input type="submit" value="confirm" />
    </form>
    <div id="result"></div>

    <!-- end of the Options -->

below is the select.class.php 下面是select.class.php

<?php
    class SelectList
    {
        protected $conn;

            public function __construct()
            {
                $this->DbConnect();
            }

            protected function DbConnect()
            {
                include "db_config.php";
                $this->conn = mysql_connect($host,$user,$password) OR die("Unable to connect to the database");
                mysql_select_db($db,$this->conn) OR die("can not select the database $db");
                return TRUE;
            }

            public function ShowCategory()
            {
                $sql = "SELECT * FROM subject";
                $res = mysql_query($sql,$this->conn);
                $category = '<option value="0">choose...</option>';
                while($row = mysql_fetch_array($res))
                {
                    $category .= '<option value="' . $row['subject_id'] . '">' . $row['description'] . '</option>';
                }
                return $category;
            }

            public function ShowType()
            {
                $sql = "SELECT * FROM section WHERE subject_id=$_POST[id]";
                $res = mysql_query($sql,$this->conn);
                $type = '<option value="0">choose...</option>';
                while($row = mysql_fetch_array($res))
                {
                    $type .= '<option value="' . $row['section_id'] . '">' . $row['description'] . '</option>';
                }
                return $type;
            }

             public function ShowPrinciple()
            {
                $sql = "SELECT * FROM principle WHERE section_id=$_POST[id]";
                $res = mysql_query($sql,$this->conn);
                $principle = '<option value="0">choose...</option>';
                while($row = mysql_fetch_array($res))
                {
                    $principle .= '<option value="' . $row['principle_id'] . '">' . $row['description'] . '</option>';
                }
                return $principle;
            }   
    }

    $opt = new SelectList();
?>

1) how would I post the values from my drop down boxes into a php array 1)我如何将下拉框中的值发布到php数组中

In the form tag add method="POST". 在表单标签中添加method =“ POST”。 Reference in PHP with $_POST array. 在PHP中使用$ _POST数组进行引用。 Make sure to validate and escape the data before writing to your DB. 在写入数据库之前,请确保验证并转义数据。

2) how would I then check the values against and array of data and then choose which are correct and post them to the screen. 2)然后我将如何对照数据值和数据数组检查值,然后选择正确的值并将其发布到屏幕上。

If you don't have millions of categories, you are better off sending them all as a JSON array and using Javascript. 如果您没有数百万个类别,则最好将它们全部作为JSON数组发送并使用Javascript。 Something like: 就像是:

<script>
var categories = <?php echo json_encode($opt->ShowCategory()); ?>;
</script>

json_encode may require some options to be set, depening on your character set. json_encode可能需要设置一些选项,具体取决于您的字符集。 More info here: http://php.net/manual/en/function.json-encode.php 此处提供更多信息: http : //php.net/manual/zh/function.json-encode.php

Making a new request each time someone changes a dropdown box will drive them crazy, I know I hate that. 每当有人更改下拉框时发出新请求都会使他们发疯,我知道我讨厌这样做。 If you have used jQuery before, this is very easy. 如果您以前使用过jQuery,这非常容易。 This isn't that difficult without it. 没有它,这并不困难。

3)Would I need to use a second language like javascript or can it be done just in php 3)我需要使用第二种语言,例如javascript还是可以在php中完成

For the sake of your users, use Javascript. 为了您的用户,请使用Javascript。

code for showCategory() showCategory()的代码

...
$categories = new array();
$category = '<option value="0">choose...</option>';
while($row = mysql_fetch_array($res))
            {
                $categories[$row['subject_id']] = $row['description'];
            }
$validCategories = $this->getValidCategories() // get the valid categories 
foreach($categories as $index=>$cat){
   // only choose the categories that are valid
   if(array_search($cat,$validCategories) !== FALSE)
       $category.= '<option value="'.$index.'">'.$cat.'</option>'; 

}
return $category;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM