Convert two inputs to a single array to input into db codeigniter

Im busy creating a webpage that issues drawings to a client and creates a receipt for sending to a client. The part that I'm having trouble with is to select the drawings that I want to add to the receipt and to change the revision.

The information for the drawings are split over two tables. The first contains the drawing number, title, drawn by etc and the second contains only the revision history. The tables are linked by the drawing id.

To issue a drawings the user must tick the checkboxes and change the revision of the drawings he wants to issue.

I want to input this into the database, but I'm not sure how to create the array. Currently my controller takes gets all the dropdown inputs and only the ticked checkboxes. I want only the dropdown inputs associated with the selected checkboxes to be updated by the database.

This is my view.

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

My controller

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



}

The issue_dwg() function gets populates the dropdown with the drawing numbers and populates the view with the db results.

The issue() function needs to process the posted information and convert it into a array to pass to the db.

(I wanted to add an image of the view page, but I don't have enough reputation to do it)

Answers


When you parse your form, you need to build your inputs so that, when you submit the form, you end up with associative arrays:

currently you have the following:

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

when submitted, it will send the following post array:

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

SOLUTION:

structure your form like this:

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

this way, youll end up with an array as such:

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

Now you can iterate with a foreach in your controller with ease.


Need Your Help

Python: Reading individual elements of a file

python python-2.7 file-io

I am attempting to read in individual elements of a file. In this example, the first element of each line is to be the key of a dictionary. The next five elements will be a corresponding value for ...

VB not stopping at exceptions

vb.net exception

I like VB.Net, but there is something that is driving me nuts. Too many times when an exception occurs, it simply continues somewhere else, usually by exiting the sub or function, but otherwise ke...