I\'m working on Cake PHP. Please give me a solution to solve this:
Controller.php:
public function test() {
$this->set(\'users_l
Your function should look like this:
public function test() {
$this->set('users_list', $this->User->find('all', array('fields' => array('User.id', 'User.fullname'))));
}
Then in your view you should do a foreach loop to output the values like this:
foreach($users_list as $ul){
echo 'This is the id: '.$ul[0]['User']['id'];
echo ' This is the full name: '.$ul[0]['User']['fullname'];
}
that would be the way i'd do it but i don't know if its the best one
if you want something easier you can print an array with:
print_r($users_list);
test.ctp:
<?php echo "<pre>";print_r($users_list);echo "</pre>"; ?>
as the result of find will be an array so the above line will print all the elements of $user_list array, now you have to use this array according to your logic, for example you can use foreach() to get individual elements of this array.
Hope the above explanation is enough for your understanding!
You can use the String class. It has a very cool method called toList(), and it's used to print an array with an 'and' before the last element.
App::uses('String', 'Utility');
echo String::toList($users_list);
Check out the documentation: http://book.cakephp.org/2.0/en/core-utility-libraries/string.html
this Worked For me. Hope it will also work for you Here is my ctp file code
$this->Form->input('answertype',array('div'=>false,'label'=>false,'type'=>'select','multiple'> => 'checkbox', 'options' => $arrayOptions, 'class' => 'form-control dropdown-menu-item '));
When i Debug in controller i got Result like This
array(
'PackageDescription' => array(
'name' => 'Special Di scout Package',
'question' => '20',
'revision' => '32',
'experttype' => '100',
'responsetime' => '6',
'answertype' => array(
(int) 0 => 'image',
(int) 1 => 'audio'
),
'support' => 'email&sms',
'status' => 'public'
)
)
and i make Some Changes in My Model to Store Any Array as String
public function beforeSave($options = array()) {
if(!empty($this->data[$this->alias]['answertype']) && $this->data[$this->alias]['answertype'] != ''){
$subArr = $this->data[$this->alias]['answertype'];
$this->data[$this->alias]['answertype'] = implode(',', $this->data[$this->alias]['answertype']);
$my_model = ClassRegistry::init('Answer');
$AnswerInfo = $my_model->find('all', array('conditions' => array('Answer.name' => $subArr), 'fields' => array('Answer.name', 'Answer.name')));
if(!empty($AnswerInfo)){
$answertype = array();
foreach($AnswerInfo as $info){
$answertype[] = $info['Answer']['name'];
}
$this->data[$this->alias]['answertype'] = implode(',', $answertype);
}
}
if (!empty($this->data[$this->alias]['name']) && empty($this->data[$this->alias]['slug'])) {
if(!isset($this->data[$this->alias]['id'])){
$this->data[$this->alias]['slug']=$this->stringToSlug($this->data[$this->alias]['name']);
}else{
$this->data[$this->alias]['slug']=$this->stringToSlug($this->data[$this->alias]['name'],$this->data[$this->alias]['id']);
}
}
return true;
}