How to pass simple data from controller to view in cakePHP - cakephp

I get an int value to the controller and I would like to send it to the according view, but i am either passing or accesing it wrong. Here is what I am trying to do:
Controller:
function send($int = NULL) {
$this->set('integer', $int);
}
View:
echo $integer['int']
Your help is much appreciated!

Change
echo $integer['int']
To:
echo $integer;
The first parameter in $this->set is used as the variable name in your view, the second parameter is the value assigned to that variable.

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

Getting a value from an array in controller

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.

call action from action in one controller cakephp like controller/action/action

i have a controller called "Movies" so the link is "localhost/Movies" and have some actions in this controller like "localhost/Movies/view/[id]" , "localhost/Movies/view/category/[id]" if i want to make a path by the category name and sub category name like "localhost/Movies/English/new" .
how can i do something like this in cakephp 2. my project now like "localhost/English/new" but i want to put Movies in this path, to make it more fixable, if i want to make a new category just add a column in my database .
thanks
If i understand, maybe by creating a custom route for your action:
In app/config/routes.php:
Router::connect('/movies/:category/:subcategory', array('controller'=>'movies','action' => 'index'));
And you can retrieve the value in your controller with:
echo $this->params['category'];
echo $this->params['subcategory'];
You can also read about it in the cookbook http://book.cakephp.org/1.3/view/945/Routes-Configuration
The default routing in CakePHP is as follows:
mydomain/controller/action/param1/param2/param3/param4/...
If you want to add filter options to an action, just add optional parameters to the action.
function index($category = null, $subcategory = null) {
if(isset($subcategory)){
//will execute if you pass both arguments
}else if(isset($category)){
//will execute if you pass one argument
}else{
//will execute if you pass no arguments
}
}
EDIT
In this case, $category is param1, and $subcategory is param2. The overloaded function can receive either 1, 2, or no arguments. If this is in, for example, the ObjectsController, all these are valid URLs:
localhost/objects/index/ //$category==null, $subcategory==null
localhost/objects/index/foods/ //$category=='foods', $subcategory==null
localhost/objects/index/foods/green/ //$category=='foods', $subcategory=='green'
This allows you to control many options in the same action.

send parameter url

I am sending the values to a controller doing this
echo $html->link('Do this? ',"/item/view/{$form->value('table.id')}");
on the controller end cake just pickups the id like this
function view($id = null){
....
}
Now in addition to table.id, I want to send another value called table2.source ...how would I do that so the controller also gets it
function view($id = null, $source=null) ...something along those lines but not sure how will they get to the controller in first place
Can you not just change the link url to something like this:
echo $html->link('Do this? ',
"/item/view/{$form->value('table.id')}/{$form->value('table2.source')}");
(After changing the view function to accept the second parameter...)

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.

Resources