I’m sending 2 datas with same name at the front side like this
<h1>First Address</h1> <input name="address[1][street]" value="Hauptstr" /> <input name="address[1][city]" value="Berlin" /> <h2>Second Address</h2> <input name="address[2][street]" value="Wallstreet" /> <input name="address[2][city]" value="New York" />
And then I’m trying to store that value in my database through my model methods in codeigniter 3
This is what I was doing before with just 1 data
$streetinfo['street'] = $this->input->post('street'); $streetinfo['city'] = $this->input->post('city'); $newAddress= json_encode($streetinfo); $entry = array( "id" => "1", "address" => $newAddress, ); $result= $this->address->insert_entry($entry);
But now that im dealing with more input data i dont know how to handle it im storing the data like this now
$streetinfo = $this->input->post('street');
and when i do the var_dump for $streetinfo i get this
array(2) { [1]=> array(2) { ["street"]=> string(1) "1" ["city"]=> string(1) "1" } [2]=> array(2) { ["street"]=> string(1) "3" ["city"]=> string(1) "2" }
}
And then now, I don’t know how to get each value from those keys and make each insert in the db
Advertisement
Answer
Since you’re now passing the data as an array, you’re supposed to use indexes to access each variable. First, store the input data in a variable, then access the different indexes and their respective keys/values.
$data = $this->input->post('address'); $street1 = $data[1]['street']; $city1 = $data[1]['city']; $street2 = $data[2]['street']; $city2 = $data[2]['city'];
If you then want to save them to the database, make sure you have the required fields/columns in your database and continue saving the data as usual.
Is this what you’re looking for?