Getting a value from an array in controller - arrays

I have a function in controller which contains an array
function get_art(){
$data['rec']=$this->article_model->get_article();
$this->load->view('first',$data);}
I need the section_id from $data to pass it to another function in model.How can I get that?
Thanks from your help.

try this
function get_art(){
$data['rec']=$this->article_model->get_article();
foreach ($data['rec'] as $a) {
print_r($a->section_id); // $this->article_model->anotherfun($a->section_id);
}
$this->load->view('first',$data);
}

In View you can simply access it like below.
<?php echo $rec->section_id; ?>
But without viewing the model function I cannot determine whether you return an array or single object from the db read.

Related

controller logic for included view files in cakephp

As I am including elements/header.ctp file in another main.ctp file..
<?php echo $this->element('header'); ?>
I have included header in main.ctp it is displaying quite good. but when I am writing the query in ElementsController in is throwing an error undefined varible..
Below code for Elements Controller header() is the included file function.
public function header()
{
$this->set('marquee',$this->Newsmaster->find('all',array('order'=>array('Newsmaster.priority DESC'),'limit' =>20)));
$userid = $this->Session->read('user');
}
can anyone help me in solving this please... thank You
Elements Controller is not needed to set "marquee" varaible , you need to code in your usual controller for eg. for pages/index you will need to set variable in PagesController's index function not in under ElementsController

passing array to controller function in codeigniter

I have array of id's. I am getting id's in string in $user_id.
$array=explode(',', $user_id);
echo http_build_query($array);
It outputs 0=3&1=4 but when I Pass $array as a parameter to controller function via url like this..
<?php echo base_url()."index.php/requesthandler/cancelRequest/$payment_id/$booking_id/$array";?>
And when I print_r($array) in controller it gives me output only string that is 'Array'.So how do I Pass that array to controller function via url??
$booking_id and $payment_id are other parameters for that function.
You should convet array to query string, please use http_build_query functio with url, try this
<?php echo base_url()."index.php/requesthandler/cancelRequest
/$payment_id/$booking_id?".http_build_query($array);?>
OR
You can also pass same string with single key
<?php echo base_url()."index.php/requesthandler/cancelRequest
/$payment_id/$booking_id?user_ids={$user_id}"; ?>
And parse on user like this
<?php print_r(explode(',', $this->input->get("user_ids"));?>

How to use find('all') in Views - CakePHP

I searched a lot but I couldn't find on How to use the find('all') in Views as used in Rails, but here I'm getting the error "Undefined property: View::$Menu [APP\Lib\Cake\View\View.php, line 804]"
'Menu' is the model which I'm using to fetch data from the menus table.
I'm using the below code in views:
$this->set('test',$this->Menu->find('all'));
print_r($test);
Inside your Menu model create a method, something like getMenu(). In this method do your find() and get the results you want. Modify the results as you need and like to within the getMenu() method and return the data.
If you need that menu on every page in AppController::beforeFilter() or beforeRender() simply do
$this->set('menu', ClassRegistry::init('Menu')->getMenu());
If you do not need it everywhere you might go better with using requestAction getting the data using this method from the Menus controller that will call getMenu() from the model and return the data. Setting it where you need it would be still better, if you use requestAction you also want to cache it very likely.
TRY TO NOT RETRIEVE DATA WITHIN VIEW FILE. VIOLATION OF MVC RULE
try this in view file:
$menu = ClassRegistry::init('Menu');
pr($menu->find('all'));
In AppHelper ,
Make a below function
function getMenu()
{
App::import('Model', 'Menu');
$this->Menu= &new Menu();
$test = array();
$test = $this->Menu->find('all');
return $test;
}
Use above function in view like :
<?php
$menu = $html->getMenu();
print_r($menu);
?>
Cakephp not allow this .
First create the reference(object) of your model using ClassRegistry::init('Model');
And then call find function from using object
$obj = ClassRegistry::init('Menu');
$test = $obj->find('all');
echo ""; print_r($test); `
This will work.

CakePHP : How to access $this->set(array('fruit' => 'orange', 'vegetable' => 'kale')); in controller

I am a new in CakePHP and I am looking for some code, I have been downloaded function which looks like below:
$this->set(array('fruit' => 'orange', 'vegetable' => 'kale'));
In the code, the array variables are accessed in another controller function using this method:
$varsSet = $this->viewVars;
echo $varsSet['vegetable'];
What I would like to do access the array variables in the same function in the controller where the $this-set() statement is made, and it seems like I should be able to do so with just one line of code. I have tried all of the following:
echo $fruit;
echo $this->field('fruit');
echo $this->MyModel->$fruit;
echo $this->MyModel->field('fruit');
And all of these throw parse, undefined variable, or variable not found errors. What would be the simplest/most proper way to access the variable within the same function in the controller?
Thanks,
Jonathan
Function $this->set() in the controller is used to pass variables from the controller to the view.
I.e.
If you have:
$this->set('fruit' => array('orange', 'vegetable' => 'kale'));
Then in the associated view you can access the array directly as
print_r($fruit);
If you want to use fruit variable in the controller, then you have to assign it to the var i.e.:
$fruits = array('orange', 'vegetable' => 'kale');
$this->set('fruit', $fruits);
But your question is not very clear what do you want to achieve with this.

Cakephp: how to access to model "variables" (mapped from db) from the controller after a set?

Ok i have this controller:
class ExampleController extends AppController {
var $name = 'Example';
public function test_me () {
$this->Example->Create();
$this->Example->set( 'variable_from_db_1' => 'random_value_1',
'variable_from_db_2' => 'random_value_2' );
//here, how can i access to variable_from_db_1 and 2 in $this->Example?
//???? i've tried $this->data and $this->Example->data but nothing to do
}
}
Do you have some hints for me?
you can explore the data with:
debug( $this->Example );
the data is it's own array:
$this->Example->data['variable_from_db_1'];
I don't think you can do that.
You can assign your model data to a $this->data array like this:
$this->data['variable_from_db_1'] = $value;
$this->set('variable_from_db_1', $value);
So know you can access $this->data within the controller.
I think if you want to save data to your actual Model, you might have to implement the getter / setter method in your model...
In the related view you can access it like this:
echo $variable_from_db_1.'<br />';
echo $variable_from_db_2.'<br />';
In the controller call
debug($this->data);
I think you cannot do this in controller, normally set calls are moved to the end area of actions and all the operations on the such variables must be performed before 'set'ing it for later access in views.

Resources