将两个输入转换为单个数组以输入到db codeigniter

时间:2015-07-05 14:01:07

标签: mysql arrays codeigniter user-input

我正忙于创建一个向客户发布图纸并创建收据以发送给客户的网页。我遇到问题的部分是选择我想要添加到收据中的图纸并更改修订。

图纸的信息分为两个表格。第一个包含图纸编号,标题,由等绘制,第二个包含修订历史。表格由图纸ID链接。

要发出图纸,用户必须勾选复选框并更改他想要发布的图纸的修订版。

我想将其输入数据库,但我不确定如何创建数组。目前我的控制器获取所有下拉输入和仅勾选的复选框。我只希望数据库更新与所选复选框关联的下拉输入。

这是我的观点。

<h1>Issue drawing</h1>
<br>

<div id="body">
<div class="row">
        <div class="form-group-sm"><lable class="col-sm-2 control-label">Project number:</lable>
    <?php
        $js = 'onchange="this.form.submit()" class="form-control" id="focusInput"';
        echo form_open('dwg_issue/issue_dwg');
        echo "<div class=\"col-xs-2\">" . form_dropdown('project_no',$proj_num, $this->input->post('project_no'), $js)."</div>";
        echo form_error('project_no', '<div class="col-xs-4"><div class="alert alert-danger fade in"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>','</div></div>');
    ?>
        </div>
        </div>

        <?php
            echo "<noscript>".form_submit('submit','Submit')."</noscript>";
            echo form_close();
        ?>
    <form action="<?php echo base_url() . 'index.php/dwg_issue/issue'; ?>" method="post" accept-charset="utf-8" id="issue">

        <table title="Client information" class="table table-hover">
            <caption><b>List of drawings</b></caption>
        <thead>
            <tr><th>Project number</th><th>Drawing number</th><th>Client drawing number</th>
                <th>Title</th><th>Drawn by</th><th>Revision</th><th>Drawn Date</th><th>Select</th>
        </thead>    
        <tbody> &nbsp;
        <?php

//          var_dump($client_info);
            if(!empty($result))
            {
                foreach($result as $row)
                { 
                    echo "<tr>";
                    echo "<td>" . $row->project_no . "</td>";
                    echo "<td>" . $row->sws_dwg_no . "</td>";
                    echo "<td>" . $row->client_dwg_no . "</td>";
                    echo "<td>" . $row->dwg_title . "</td>";
                    echo "<td>" . $row->dwg_by . "</td>";
                    $rev = array('0' => $row->dwg_rev);
                    $rev_change = array(
                        'B' => 'B',
                        'C' => 'C',
                        'D' => 'D',
                        'E' => 'E'                  
                        );
                    $dropdown = array_merge($rev,$rev_change);

                    echo "<td>" . form_dropdown('dwg_rev['.$row->dwg_id.']',$dropdown) . "</td>";
                    echo "<td>" . date('Y/m/d', strtotime($row->dwg_date)) . "</td>"; 
                    echo "<td>" . form_checkbox('select['.$row->dwg_id.']',$row->dwg_id) . "</td>"; 
                    echo "</tr>";
                }
            }

        ?>
        </tbody>
       </table>

       <div class="row">
       <div class="col-xs-12 col-md-10">Please select client distribution here with other options</div>
        <?php
        echo "<div class=\"col-xs-6 col-md-2\"><button class=\"btn btn-md btn-primary btn-block\" type=\"submit\" form=\"issue\">Issue drawings</button></div>";
        ?>
        </div>
        <br>


        <?php
        echo form_close();
        ?>

</div>

我的控制器

public function issue_dwg()
{
    $data['proj_num'] = $this->model_proj->proj_num_all();
    if ($this->input->post('project_no') != '0')
    {
        $data['result'] = $this->model_issue->list_dwg($this->input->post('project_no'));
    }

    $data['main_content'] = 'issue_view';
    $this->load->view('includes/template.php', $data);


}

//This function takes the selected drawings and create and issue slip
public function issue()
{
    $rows = array();

    $num_results = count($this->input->post('select'));
    for($i = 0; $i < $num_results; $i++)
    {
        $rows[$i]['select'] = $_POST['select'][$i];
        $rows[$i]['dwg_rev'] = $_POST['dwg_rev'][$i];
    }



}

issue_dwg()函数使用图纸编号填充下拉列表,并使用db结果填充视图。

issue()函数需要处理发布的信息并将其转换为数组以传递给数据库。

(我想添加一个视图页面的图片,但我没有足够的声誉去做)

1 个答案:

答案 0 :(得分:1)

解析表单时,需要构建输入,以便在提交表单时最终得到关联数组:

目前您有以下内容:

//...
echo form_dropdown('dwg_rev['.$row->dwg_id.']',$dropdown);
echo form_checkbox('select['.$row->dwg_id.']',$row->dwg_id);
//...

提交时,它将发送以下帖子数组:

"select" (array)[
   0 => "some select value",
   1 => "some select value"
],
"dwg_rev" (array)[
   0 => "some dwg_rev value",
   1 => "some dwg_rev value"
]

解决方案:

像这样构建你的表单:

//...
echo form_dropdown('result['.$row->dwg_id.'][dwg_rev]',$dropdown);
echo form_checkbox('result['.$row->dwg_id.'][select]',$row->dwg_id);
//...

这样,你最终得到一个数组:

"result"[
   0 => [
      "select" => "some select value",
      "dwg_rev" => "some dwg_rev value"
    ]
   1 => [
      "select" => "some select value",
      "dwg_rev" => "some dwg_rev value"
    ]
]

现在,您可以轻松地在控制器中使用foreach进行迭代。