$this->layout='defaultall';
$id = $this->Auth->user('idUser');
$data= $this->Contact->find('all',array(
'conditions' => array('Contact.User_id' => $id)));
$this->set('contacts',$data);
$this->loadModel('Calllog');
/* for($i=1; $i < 2; $i++)
{
$mobile =$data[$i]['Contact']["mobileNo"];
$work =$data[$i]['Contact']["workNo"];
$home =$data[$i]['Contact']["homeNo"];*/
for($data as $datas){
$mobile= $datas['Contact']['mobileNo'];
$home= $datas['Contact']['homeNo'];
$work= $datas['Contact']['workNo'];
$recent=$this->Calllog->find('all',
array('order'=>'Calllog.idCallLog DESC',
'limit' => 1,
'conditions' => array('Calllog.mobileNo' => array($mobile,$work,$home ),
'Calllog.User_id' => $id
)));
//here array want to declare
$this->set('a',$recent);
//here i want to declare an another array which saves the data of every iteration..because i dont want override the data
sorry i am week in arrays.. and do tell me how can i then print the array ...if i want to display the 1st iteration result in my view page
If you just want to add new element to the array without deleting its content you can do that just like that:
$recent[] = 'new_string1';
or if you have more elements:
array_push($recent, 'new_string1', 'new_string2');
But in the future, please read manual: http://php.net/manual/en/function.array-push.php
Related
I'm trying to loop through an array and insert my values into the database. My table looks like this:
hours_day
hours_open
hours_close
My form enables the user to select a day, an opening time and a closing time. There is a plus button which adds another day below it, so the user can choose whichever days they wish. This means that my select box names are arrays, i.e. hours_day[]
My Model looks like this:
$hours = array(
'hours_day' => $this->input->post('venue_hours_day'),
'hours_opening' => $this->input->post('venue_hours_open'),
'hours_closing' => $this->input->post('venue_hours_close'),
);
So I have an array ($hours) with arrays inside of it (hours_day, hours_opening, hours_closing). How do I loop through this an add it to my database?
You can use :
$post_day = $this->input->post('venue_hours_day');
$post_opening = $this->input->post('venue_hours_open');
$post_closing = $this->input->post('venue_hours_closing');
$count = count($post_day);
$results = array();
for ($i = 0; $i < $count; $i++)
{
$results []= array(
'hours_day' => $post_day[$i],
'hours_opening' => $post_opening[$i],
'hours_closing' => $post_closing[$i]
);
}
$this->db->insert_batch('your_table', $results);
I'm using cakephp 2.3.0. I have a scenario where I need to insert data in a table, but I need to do multiple inserts, based on the number of users that need the inserted data. If I have one value in the $userIds array, or more than 1 value in the $userIds array, then only one row is inserted in the table. i.e. if I had two values in the array, I would expect two rows to be inserted in my table.
I can't figure out how to correctly build my $message array with the different values from the $userIds array. In the example output below, the value of 5 gets overwritten by 3, and 3 gets overwritten by the value of 4.
When I have 2 or more values in the $userIds array, I confirm that the for loop is correctly looping through each time in the array, by the debug statement.
Below is the output of debug statements:
\app\Controller\ActivitiesController.php (line 134)
'5'
\app\Controller\ActivitiesController.php (line 134)
'3'
\app\Controller\ActivitiesController.php (line 134)
'4'
\app\Controller\ActivitiesController.php (line 136)
array(
'Message' => array(
'message' => 'The activity has been deleted.',
'user_id' => '4'
)
)
Below is my code snippet from my controller:
$this->Activity->Message->create();
foreach ($userIds as $ids) {
$message = array(
'Message' => array(
'message' => 'The activity has been deleted.',
'user_id' => $ids
)
);
debug($ids);
}
debug($message);
$this->Activity->Message->saveMany($message);
You reset your array each time instead of adding one element to it. So try changing your foreach like this
$message = array();
foreach ($userIds as $ids) {
$message[] = array(
'Message' => array(
'message' => 'The activity has been deleted.',
'user_id' => $ids
)
);
debug($ids);
}
I'm trying to build an array and update a few fields in a loop.
This is my request->data
Array
(
[list] => Array
(
[4] => null
[2] => null
[3] => null
[5] => 3
)
)
It's the return value from a jquery serialized list. The key being the row id and the value being the rows parent_id.
So I loop through the array in the controller:
foreach ($this->request->data['list'] as $key => $value) {
(!isset($orders[$value])) ? $orders[$value]=0 : $orders[$value]++;
$data = array('id' => $key, 'parent_id' => (int)$value, 'display_order' => $orders[$value]);
$this->Category->save($data);
}
The $data array that I create in the loop is correct but the sql logs only shows SELECT COUNT(*) etc. for each row and no UPDATE commands.
I have tried all manner of ways to save this: using set() method, using $this->Category->id = $key; instead of directly adding the key in the data array.
Any ideas why this is not saving? I'm sure it is something simple...
i think u forgot the cake convention in the save method, you have to put all the field values inside an array with the Model name FirstCap too, something like this:
Array
(
[ModelName] => Array
(
[fieldname1] => 'value'
[fieldname2] => 'value'
)
)
Else you can use set for each and every value, and also youre forgetting to create one row for every insert youre making so try something like this:
foreach ($this->request->data['list'] as $key => $value) {
(!isset($orders[$value])) ? $orders[$value]=0 : $orders[$value]++;
$data = array( 'Category' => array('id' => $key, 'parent_id' => (int)$value, 'display_order' => $orders[$value]));
$this->Category->save($data);
}
Also you can set each and every id and then iterates over the values
foreach ($this->request->data['list'] as $key => $value) {
(!isset($orders[$value])) ? $orders[$value]=0 : $orders[$value]++;
$this->Category->id = $key;
$this->Category->set(array('parent_id' => (int)$value,
'display_order' => $orders[$value]
));
$this->Category->save();
}
try each and every answer but i think the last one will fit better at your problem.
From the CakePHP book.
When calling save in a loop, don’t forget to call create().
echo $data and match it with save() method's syntax.also do not slip to match data types of fields in table.
I am still new to PHP, and even newer to Drupal's Form API. I'm simply trying to populate a drop down select from the database. I think the issue stems from my lack of a deep understanding of how to work with multidimensional arrays. Any help is sincerely appreciated.
My code is currently:
//Query DB for Rows
$query = db_select('hp_education_years');
$query->fields('hp_education_years', array('id', 'years',));
$query->orderBy('years', 'ASC');
$results = $query->execute();
//define rows
$options = array();
foreach ($results as $result) {
$options[$result->id] = array(
$result->years,
);
}
$form['education']['year'] = array(
'#type' => 'select',
'#title' => t('Year'),
'#options' => $options,
'#states' => array(
'visible' => array(
':input[name="data_set"]' => array('value' => 'education'),
),
),
);
This returned a populated list, but displays the year's ID in bold as well as the year (2008 for example).
How can I get the dropdown just to display the year, not the year ID in bold, and then the year. It seems like $option is just a level higher than I want it to be? if that makes sense?
Many thanks in advance.
Try changing
//define rows
$options = array();
foreach ($results as $result) {
$options[$result->id] = array(
$result->years,
);
}
to
//define rows
$options = array();
foreach ($results as $result) {
$options[$result->id] = $result->years;
}
If you look at the example from Drupal's Form API, you'll see that the Options values should be just the string value and not an array.
I have a table with data from the database, I would like it to have a Pager, I have all the code from an example (of buildamodule.com) among other sites, and my table get's rendered, but it doesn't generate a pager, although I have more rows then the limit:
the function:
function get_loyaltycodes(){
$headers = array(
array(
'data' => t('Code')
),
array(
'data' => t('Info')
),
array(
'data' => t('Points')
),
array(
'data' => t('Consumed by')
),
);
$limit = variable_get('codes_per_page',5);
$query = db_select('loyalty_codes','lc');
$query -> fields('lc',array('code','info','points','uid'))
-> orderBy('lc.info','ASC')
-> extend('PagerDefault')
-> limit($limit);
//to see all codes for a certain amount of points, just append the number of points to the URL
$arg = arg(2);
if($arg != '' && is_numeric($arg))
{
$query->condition('points', $arg);
}
// Fetch the result set.
$result = $query->execute();
$rows = array();
// Loop through each item and add to the $rows array.
foreach ($result as $row) {
$rows[] = array(
$row->code,
$row->info,
$row->points,
$row->uid,
);
}
// Format output.
$output = theme('table', array('header' => $headers, 'rows' => $rows)) . theme('pager');
return $output;
the $limit variable is set to 5 in the settings form, and it is 5 in the database too.
Anybody who has any idea in why the pager is not showing? perhaps something in the formatting off the output?
Help is very much appreciated!
apparently I can't log in right know because I'm behind a firewall, anyway I seem to have fixed it, stupid mistake though:
the ‘->extend(‘PagerDefault’) extension in the query had to be the first function in the daisy chain. If not there is no error but the function seems to not be called.
$query = db_select('loyalty_codes','lc')
->extend('PagerDefault')
-> fields('lc',array('code','info','points','uid'))
-> orderBy('lc.info','ASC')
-> limit(5);//$limit);