1

I am using multiple select from a select drop down and I want to store it in database.

I have heard about implode explode, but I have no idea how to use them. Please give me an idea how to implode and explode in codeigniter.

My HTML form:

<div class="form-group">
    <label>
        Please Select Proof Of ID Deposit
    </label>
    <select multiple="" name="oids" class="js-example-basic-multiple js-states form-control">
        <optgroup>
            <option value="Passport">Passport</option>
            <option value="National ID Card">National ID Card</option>
            <option value="Driving Licence">Driving Licence</option>
            <option value="Univesity ID">Univesity ID</option>
        </optgroup>
    </select>
</div>

My controller:

public function customer_insert(){
    $data = array(
        'id'=>$this->input->post('up_id'),
        'Other_id'=>$this->input->post('oids') //here I want to implode my values
    )
    $this->db->insert('customer', $data);
}

View file:

<table>
    <thead>
        <tr><td>ID</td></tr>
        <tr><td>Other ID</td></tr>
    </thead>
<?php foreach ($query as $customer){?>
    <tr><td><?php echo $customer->id?></td></tr>
    <tr><td><?php echo $customer->Other_id?></td></tr> // I want to show my imploded value here. 
<?php}?>
</table>
2
  • You should store the ID - OID combinations in a separate table. That way you can search and filter in the future. Commented Apr 6, 2015 at 15:58
  • ok i can save that but my main problem is i want to save multiple value in one column in my database and show them in view... how can i achieve that?? Commented Apr 6, 2015 at 16:02

4 Answers 4

3

Firstly within your HTML FORM you need to change the name attr value with an array unless it will not produce an array of selected values

<select multiple="" name="oids" class="js-example-basic-multiple js-states form-control">

must be like

<select multiple="" name="oids[]" class="js-example-basic-multiple js-states form-control">

And within your controller function

public function customer_insert() {

    $oidVal = implode(",", $this->input->post('oids'));// Converted array into comma separated value using implode

    $data = array(
        'id'=> $this->input->post('up_id'),
        'Other_id'=> $oidVal
    )
    $this->db->insert('customer', $data);
}

and within your View File

<table>
    <thead>
        <tr><td>ID</td></tr>
        <tr><td>Other ID</td></tr>
    </thead>
<?php foreach ($query as $key => $value) { ?>
    <tr><td><?php echo $value['id']; ?></td></tr>
    <tr><td><?php echo explode(',',$value['Other_id']);?></td></tr> // exploded value
<?php } ?>
</table>
Sign up to request clarification or add additional context in comments.

1 Comment

Then accept it as an answer so this must be considered as closed issue.
0

You can use implode function to join array elements with a string and explode function to split a string by string. In this case you can use them like this.

public function customer_insert() {
    // Convert array to comma separated value 
    $oidString = implode(",", $this->input->post('oids'));
    $data = array(
        'id'=> $this->input->post('up_id'),
        'Other_id'=> $oidString //here I want to implode my values
    )
    $this->db->insert('customer', $data);
}

If you want to get back array from comma separated value, you can use explode.

// Convert comma separated value to array
$otherId = explode(",", $customer->Other_id);

Hope it will be useful for you.

Comments

0
 public function insert()
 {
//Insert second stage details for employer into database.
$Specilized_category = $this->input->post('spec_cat');
$data=array(
'Specilized_category'=>json_encode(implode(",", $Specilized_category)),
 );
$this->db->insert('tbl_employer', $data);

Comments

0

Firstly you need to change the name attr value with an array unless it will not produce an array of selected values

<div class="col-xxl-3 col-md-3">
  <label for="validationCustom03" class="form-label">Supplier  *</label>
  <select class="form-control supplier_select" name="supplier[]" multiple="multiple" required>
    <?php 
      $supplier_array_data=explode(",", $user_data->supplier);
      foreach($supplier_data as $supplier) {
    ?>
      <option value="<?php echo $supplier->supplier_id;?>" ><?php echo $supplier->supplier_name;?></option>
   <?php } ?>
  </select>
  <div class="invalid-feedback">
    Supplier is required.
  </div>
</div>

<?php
  //Controller function
  public function supplier ()
  {
    //implode
    $supplier = implode(",", $this->input->post('supplier'));
    print_r($supplier);
 
    //Explode
    $explode_supplier =explode(",", $supplier);
    print_r($explode_supplier);
  }
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.