I have a form for a hotel site, where I want to update its services, and the client wants to update multiple services at a time. However I'm lost as how to save it in the database with the model.
I already built my controller, it looks something like this:
$items = array(
array(
'id' => $this->input->post('id1', true),
'hotel_id' => $hotel_id,
'des_servicio' => $this->input->post('des_servicio1', true),
'est_activo' => $this->input->post('est_activo1', true)
),
array(
'id' => $this->input->post('id2', true),
'hotel_id' => $hotel_id,
'des_servicio' => $this->input->post('des_servicio2', true),
'est_activo' => $this->input->post('est_activo2', true)
),
array(
'id' => $this->input->post('id3', true),
'hotel_id' => $hotel_id,
'des_servicio' => $this->input->post('des_servicio3', true),
'est_activo' => $this->input->post('est_activo3', true)
)
);
$this->hotel_model->save_multiple($items);
[UPDATE]
this is how my new model looks like:
function save_multiple($items = array())
{
$this->db->insert_batch($this->__tabla, $items);
return $this->db->affected_rows();
}
My issue is that now it creates 10 rows (my original form has 10 fields) even if I only populate 3 fields. So in my database 3 services get stored, and also 7 blank rows. How can change this?
foreach $items //I get an error here
as $item
should be
foreach ( $items as $item )
Remember that input->post() returns false if the value is not set. So check to see if the value is set be for putting it in the array. Then when the model receives it. It saves them all. Or the other option is to create a new array in the model from the array that is passed in and then pass the new array to the insert_batch() function.
$items = array();
$id = $this->input->post('id1', true);
if( $id != false ) {
$items[] = array(
'id' => $id, true),
'hotel_id' => $hotel_id,
'des_servicio' => $this->input->post('des_servicio1', true),
'est_activo' => $this->input->post('est_activo1', true)
);
}
$id = $this->input->post('id2', true);
if( $id != false ) {
$items[] = array(
'id' => $id, true),
'hotel_id' => $hotel_id,
'des_servicio' => $this->input->post('des_servicio2', true),
'est_activo' => $this->input->post('est_activo2', true)
);
}
....
Related
i want to loop all the data i call but that runs only one data
I use Codeigniter framework
Model:
$this->db->where('group_user', 'upt_perpus')->order_by('idmodul', 'DESC')->get('menu')->result_array();
Controller:
$data = array(
'about' => array(
'sejarah' => 'Sejarah',
'visi_misi' => 'Visi, Misi & Tujuan',
'struktur' => 'Struktur Organisasi'
),
'services' => array(),
'peraturan' => array(
'umum' => 'Umum',
'khusus' => 'Khusus',
'pelayanan_perpus' => 'Pelayanan Perpustakaan'
)
);
$konten = $this->kontens->getPerpus();
foreach ($konten as $key) {
$data['services'] = array(
$key['slug'] => $key['judul']
);
}
return $data;
If you want an associative array in $data['services'], you need to assign to a key of the array.
foreach ($konten as $key) {
$data['services'][$key['slug']] = $key['judul'];
}
I have an array of arrays, as such
$statuses = array(
[0] => array('id'=>10, 'status' => 'active'),
[1] => array('id'=>11, 'status' => 'closed'),
[2] => array('id'=>12, 'status' => 'active'),
[3] => array('id'=>13, 'status' => 'stopped'),
)
I want to be able to make a new array of arrays and each of those sub arrays would contain the elements based on if they had the same status.
The trick here is, I do not want to do a case check based on hard coded status names as they can be random. I want to basically do a dynamic comparison, and say "if you are unique, then create a new array and stick yourself in there, if an array already exists with the same status than stick me in there instead". A sample result could look something like this.
Ive really had a challenge with this because the only way I can think to do it is check every single element against every other single element, and if unique than create a new array. This gets out of control fast if the original array is larger than 100. There must be some built in functions that can make this efficient.
<?php
$sortedArray = array(
['active'] => array(
array(
'id' => 10,
'status' => 'active'
),
array(
'id' => 12,
'status' => 'active'
)
),
['closed'] => array(
array(
'id' => 11,
'status' => 'active'
)
),
['stopped'] => array(
array(
'id' => 13,
'status' => 'active'
)
),
)
$SortedArray = array();
$SortedArray['active'] = array();
$SortedArray['closed'] = array();
$SortedArray['stopped'] = array();
foreach($statuses as $Curr) {
if ($Curr['status'] == 'active') { $SortedArray['active'][] = $Curr; }
if ($Curr['status'] == 'closed') { $SortedArray['closed'][] = $Curr; }
if ($Curr['status'] == 'stopped') { $SortedArray['stopped'][] = $Curr; }
}
You can also do it with functional way though it's pretty the same like Marc said.
$sorted = array_reduce($statuses, function($carry, $status) {
$carry[$status['status']][] = $status;
return $carry;
}, []);
I am struggling to get values added into the array at specific points.
What I am looking for is to create and empty array, take the resultset from the database and depending on whether they are global or local create the PHP array listed below. The problem is that no matter what I try I can't get the array to look like below.
Either values are being replaced, or arrays being replaced or values being added into wrong places
This is my empty array and I am trying to loop each resultset into the array and then how many sold in the day.
$test = array();
foreach($globalDatabase as $value)
{
$test['global'][] = array('name'=>$value->type, 'data' => array($value->day,$value->quantity) );
}
foreach($localDatabase as $value)
{
$test['local'][] = array('name'=>$value->type, 'data' => array($value->day,$value->quantity) );
}
This is what I need it to look like so that it ends up looking like this, which is the output format I need it to be in so it is compatible with highcharts
$test = array(
'global'=>array(
array(
'name'=>'CARS',
'data' => array(
array('mon',1),
array('tue',1)
)
),
array(
'name'=>'BIKES',
'data' => array(
array('mon',1),
array('tue',1)
)
),
array(
'name'=>'BOATS',
'data' => array(
array('mon',1),
array('tue',1)
)
),
),
'local'=>array(
array(
'name'=>'CARS',
'data' => array(
array('mon',1),
array('tue',1)
)
),
array(
'name'=>'BIKES',
'data' => array(
array('mon',1),
array('tue',1)
)
),
)
);
I take it that your data rows pulled from the table are 'flat', meaning each row has only a single value each for 'type', 'day', and 'quantity', with no nesting.
Going on that assumption, you may just need to group your data by common attributes similar to the following:
$databases = array('global'=>$globalDatabase, 'local'=>$localDatabase);
$workData = array('global'=>array(), 'local'=>array());
foreach($databases as $dbType=>$workDb)
{
foreach ($workDb as $value) {
if(!array_key_exists($value->type, $workData[$dbType])) {
$workData[$dbType][$value->type] = array();
}
$workData[$dbType][$value->type][] = array($value->day, $value->quantity);
}
}
$test = array();
foreach ($workData as $type=>$workRow) {
foreach ($workRow as $dataKey=>$dataRow) {
$test[$type][] = array('name'=>$dataKey, 'data' => $dataRow );
}
}
Here is a piece of my code:
if($all_pages)
foreach ($all_pages as $page)
{
$all_hokms = $mysqli->query("SELECT * FROM qm_hokm WHERE page_id = ".$page['page_id']."");
if($all_hokms)
foreach ($all_hokms as $hokm)
{
$one_page_ahkams[] = array(
'hokm_id_inPage' => $hokm['hokm_id_inPage'],
'type' => $page['type'],
);
}
else
$one_page_ahkams[] = array();
$all_pages_data[] = array(
'page_id' => $page['page_id'],
'done' => $page['done'],
'checked' => $page['checked'],
'ahkams' => array($one_page_ahkams), //Here Is the PROBLEM : (((
);
}
else
$all_pages_data[] = array();
echo json_encode($all_pages_data);
As you see, I want to send multidimensial array() in a json format (from server to client), but how can correct the insertion of an array in an other, I mean that I need to add $one_page_ahkams array in $all_pages_data one, any ideas
$one_page_ahkams = array();
$all_pages_data[] = array(
'page_id' => $page['page_id'],
'done' => $page['done'],
'checked' => $page['checked'],
'ahkams' => array($one_page_ahkams);//this comma (,) causes the problem i guess**
);
An other way to do it :
$all_pages_data[] = array(
'page_id' => $page['page_id'],
'done' => $page['done'],
'checked' => $page['checked'],
'ahkams' => array(['one_page_ahkams']=>$one_page_ahkams);
);
Just put that: 'ahkams' => array($one_page_ahkams), but check the content of $one_page_ahkams
Official PHP documentation states that filter_var_array() supports array filtering in the following format:
$data = array(
'testarray' => array('2', '23', '10', '12')
);
$args = array(
'testarray' => array('filter' => FILTER_VALIDATE_INT,
'flags' => FILTER_FORCE_ARRAY
)
);
$myinputs = filter_var_array($data, $args);
However, if the array in question is multi-dimensional and requires different filters for different parts, how would you approach defining filtering options?
As an example:
$data = array(
'testhash' => array('level1'=>'email',
'level2'=> array('23', '10', '12'))
);
Idea 1
Consider using FILTER_CALLBACK. In this way, you can write a callback function that itself uses the filter extension, thus providing a recursive ability.
function validate_array($args) {
return function ($data) use ($args) {
return filter_input_array($data, $args);
};
}
This will generate the callback functions.
$args = array(
'user' => array(
'filter' => FILTER_CALLBACK,
'options' => validate_array(array(
'age' => array('filter' => FILTER_INPUT_INT),
'email' => array('filter' => FILTER_INPUT_EMAIL)
))
)
);
This is what the config array would then look like.
Idea 2
Do not hesitate to pat me on the back for this one because I am quite proud of it.
Take an arg array that looks like this. Slashes indicate depth.
$args = array(
'user/age' => array('filter' => FILTER_INPUT_INT),
'user/email' => array('filter' => FILTER_INPUT_EMAIL),
'user/parent/age' => array('filter' => FILTER_INPUT_INT),
'foo' => array('filter' => FILTER_INPUT_INT)
);
Assume your data looks something like this.
$data = array(
'user' => array(
'age' => 15,
'email' => 'foo#gmail.com',
'parent' => array(
'age' => 38
)
),
'foo' => 5
);
Then, you can generate an array of references that map keys such as 'user/age' to $data['user']['age']. In final production, you get something like this:
function my_filter_array($data, $args) {
$ref_map = array();
foreach ($args as $key => $a) {
$parts = explode('/', $key);
$ref =& $data;
foreach ($parts as $p) $ref =& $ref[$p];
$ref_map[$key] =& $ref;
}
return filter_var_array($ref_map, $args);
}
var_dump(my_filter_array($data, $args));
Now the only question is how you deal with the mismatch between the validation record and the original data set. This I cannot answer without knowing how you need to use them.