1

テーブルFRUITSがあるとします。

mysql> select * from FRUITS;
+---------+-----------+
| fruitid | fruitname |
+---------+-----------+
|       1 | Orange    |
|       2 | Apple     |
|       3 | Pear      |
|       4 | Banana    |
+---------+-----------+
4 rows in set (0.02 sec)

フォームを含むビューファイルがあります。

ここに、ユーザーが欲しい食べ物を選択するリストがあるので、ユーザーが「果物」を選択した場合、テーブルFRUITSからデータを取得し、コンボボックスに入力します。

ユーザーがフォームに入力すると、一部のテーブルが更新されます。プロセス中に、FRUITSテーブルのデータを参照し、ユーザーが食品のリストで果物を選択したときにコンボボックスに入力する必要があります。

Codeigniterを使用していなかった場合は、

<?php
 $conn = mysql_connect('localhost','yourUsernameHere','yourPasswordHere');
 mysql_select_db('testdb',$conn);
 $query = "select fruitid,fruitname from FRUITS order by fruitname";
 $result = mysql_query($query,$conn);
 $selectbox='<select name=\'fruit\'>';
 while ($row = mysql_fetch_assoc($result)) {
     $selectbox.='<option value=\"' . $row['fruitid'] . '\">' . $row['fruitname'] . '</option>';
 }

 $selectbox.='</select>';
 mysql_free_result($result);
 echo $selectbox;
?>

フォーム内のコンボボックスに入力し、その値をキャプチャして送信し、他のテーブルにデータを挿入します。

クエリはコントローラーファイルで実行する必要があるため、Codeigniterでこれを行うにはどうすればよいですか...?

アップデート

私が持っているとしましょう

モデル

class Fruits extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }

    function what_fruits()
    {
        $this->db->select('fruitname');
        $this->db->from('FRUITS');
        $q = $this->db->get('');
        if($q->num_rows() > 0) 
        {
            $data = array();
            foreach($q->result() as $row) 
            {
                $data=$row;
            }
            return $data;
        }
    }
}

コントローラ

class main_form_view extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->view('main_form_view');
    }

    public function result()
    {
        $this->load->model('Fruits');
        $data['fruitname'] = $this->Fruits->what_fruits(); 

    }
}

ビューmain_form_view

<html>
    <head></head>
    <body>
    <form method="post">
        <select name="food" id="food" onchange="fill_cb_food(this.value);" >
          <option value="0">fruits</option>
          <option value="1">other1</option>
          <option value="2">other2</option>
        </select>

        //response of ajax call display here.???
       <div class="selectbox" id="id_fruits">
          <select name="fruits" id="fruits">
             <option value="">--Choose fruit--</option>
          </select>
       </div>



    </form>
    </body>
</html>



    function fill_cb_food(id)
    {
       $.ajax({
                type: "POST",
                url: "main_form_view/result.php,
                data: "fruit=" + fruitname,
                success: function(html){
                    $("#id_fruits").html(html);
                }
            });
    }

だから私はコンボボックスを手に入れたい

 Orange    
  Apple    
  Pear     
  Banana

ユーザーがフォームでFruitsオプションを選択したとき...

4

2 に答える 2

2

ajax post を使用してランタイム データを取得し、コンボ ボックスに入力できます。

         $.ajax({
                url: 'http://url_to_fetch_fruit_table_data/',
                type: "POST",
                data:data,
                success: function(response) {
                   $("select").html(response);
                }
            });

または、foraeachを使用してループして塗りつぶすこともできます。

于 2013-03-07T07:46:57.413 に答える
1

モデルでは、「*」を使用して任意の列を選択します。

ビューファイルにデータを渡す必要があります。

class main_form_view extends CI_Controller
{    
    public function index()
    {
        $this->load->model('Fruits');
        $data['fruits'] = $this->Fruits->what_fruits(); 
        $this->load->view('main_form_view', $data);
    }
}

次に、ビューファイルで次のことができます。

<?php foreach($fruits as $fruit): ?>
    <option value="<?php print  $fruit['fruitid'] ?>"><?php print $fruit['fruitname']</option>
<?php endforeach; ?>
于 2016-06-07T04:10:56.147 に答える