passing array to controller function in codeigniter - arrays

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"));?>

Related

cakephp getLastInsertId not working

Here I have used cakephp js helper to send data,After insert One data I need this last id for farther work.Here I have tried bellow code in Addcontroller
if ($this->Patient->save($this->request->data)) {
$lastid=$this->Patient->getLastInsertId();
$patient=$this->Patient->find('all',array(
'conditions'=>array('Patient.id'=>$lastid ),
'recursive' => -1
));
$this->set('patient', $patient);
}
In add.ctp I have tried bellow code but I haven't get last id here.
<?php foreach ($patient as $patient): ?>
<?php echo h($patient['Patient']['id']); ?>
<?php endforeach; ?>
Method getLastInsertId() return id of just saved records.
If you need this id in your view just after save, you must first set that variable in your controller like $this->set(compact('lastid','patient'); and then use in view <?php echo $lastid; ?>
use
if ($this->Patient->save($this->request->data)) {
$id = $this->Patient->id;
$patient=$this->Patient->find('all',array('conditions'=>array('Patient.id'=>$id),'recursive' => -1));
$this->set->('patient', $patient);
//If you are saving the record with ajax which it looks like you
//might be from your question you will need the following instead
//of $this->set->('patient', $patient); try:
return json_encode($patient);
You will then also need to update your js ajax call, you will have a json array to decode so parse it with jquery and append it back into your view.
Cake will always give you the id of record you have just saved, by simply adding $id = $this->MyModel->id; You can use the id to query for the record.
Try below code:
In controller:
$lastid=$this->Patient->getLastInsertId();
$this->set(compact('lastid','patient');
Then use $lastid in View file.
In controller:
$lastid=$this->Patient->getLastInsertId();
$patient['Patient']['last_id'] = $lastid;
then use $patient['Patient']['last_id'] in your view file.

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.

how to access a function defined in app_controller from a ctp

I have function in app_controller.php.The function is like:
function globalSum($Var1,$Var2)
{
$Var3 = $Var1 + $Var2;
return $Var3;
}
Now I want to access this function from any CTP file to get the value after sum.when I call this function the arments will be send from the ctp file.
So,anybody can tell me how to call this function with arguments from the ctp file??
Thanks in advance..
The way you're trying to do this probably isn't the best, seeing as it's working against the MVC architecture that CakePHP uses.
In MVC, the ctp file is your view and should only act as a template, to the greatest extent possible, with any values that you need in the view should be passed to it from the controller.
You have a number of simple solutions to your problem.
One is simply to do the addition in the view:
index.ctp
<?php
echo $var1 + $var2
?>
For such a simple operation, why bother with a separate function?
If your function is more complicated, you can put it in the AppController and then set the view variable in the controller that the action belongs to. For example:
app_controller.php
<?php
function globalSum($Var1,$Var2) {
$Var3 = $Var1 + $Var2;
return $Var3;
}
?>
posts_controller.php
<?php
function index() {
$this->set('var3', $this->globalSum($var1,$var2));
}
?>
index.ctp
<?php
echo $var3;
?>
Hope that helps.

CakePHP question: How can i call a view of one controller from another controller?

This is posts/index.php =>
<?php foreach ($allposts as $post) {
echo '<tr class="class_row">';
echo '<td>';
echo $this->Html->link($post['Post']['title'],
array('controller'=>'posts','action'=>'view',$post['Post']['id']),
array('id'=>'id_anchor_title','class'=>'class_anchor_title') );
echo '<tr>';
echo '<td>';
}
?>
I want to call this posts/index.ctp from products/index.ctp => It will be a generic/common index.ctp for all controller. How can i do this ?
In posts/index.ctp the $allposts is used. It's set in posts/index action. But when i will call posts/index.ctp from products/index action different variable is set there. Suppose $this->set('allproducts',$allproducts); is set in products/index action. Now how can i use that allproducts variable in posts/index.ctp ?
As #Vins stated, you can use $this->render('view_name'); at the end of your controller action to render a different view (In your case it should be $this->render('/posts/index');)
In terms of using the variable you want, there are a couple things you can do. One would be to change your set function in each controller to use a common name. For example the posts controller could have $this->set('results',$allposts); and the products controller could have $this->set('results',$allproducts); Doing this, you can always reference $results in your view file. You might also want to set another variable, $pageModel. $this->set('pageModel','Product'); in your products controller for example. Then your posts/index.php file could do something like this:
<?php foreach ($results as $result) {
echo '<tr class="class_row">';
echo '<td>';
echo $this->Html->link($result[$pageModel]['title'],
array('controller'=>$this->controller,'action'=>'view',$result[$pageModel]['id']),
array('id'=>'id_anchor_title','class'=>'class_anchor_title') );
echo '<tr>';
echo '<td>';
}
?>
notice that I replaced 'controller' => 'posts' with 'controller' => $this->controller This will make your view dynamic so the links will always point to the view action of the correct controller.
I hope this helps!
We can use $this->render('view_name'); to use the another view for some other action. I'm not sure how exactly you're going to achieve your goal.
if you want to render posts/index.ctp instead of products/index.ctp, use $this->render('/posts/index');
Or you may want to put that in an element (that's the same idea of generic/common index.ctp).

how to store an array in the controller and use in the view file in cakephp

In my Users controller, I find the number of forms that the user has created by querying the 'Forms' table and store it in an array. But how to use the array variables in the view file.
Normally, I would set the variable in the controller using set function and use it in the view file as such. But how to set an array in the controller? I get the values in the controller(did an echo).
My controller code is:
function users(){
$this->set('users',$this->User->find('all'));
$users=$this->User->find('all');
foreach($users as $user):
$id=$user['User']['id'];
$userFormCount[$id]=$this->Form->find('count',array('conditions'=>array('Form.created_by'=>$user['User']['id'])));
$userFormCount[$id]=$this->Form->find('count',array('conditions'=>array('Form.created_by'=>$user['User']['id'])));
echo "users : ".$id." ".$userFormCount[$id];
endforeach;
}
My view file:
<?php foreach($users as $user):?>
<tr>
<td class="people_profile">
<?php echo $user['User']['name'];?>
</td>
<td>
<?php
$id=$user['User']['id'];
echo $userFormCount[$id]." ";?>forms, //need the array value here.
0 reports,
0 badges
</td>
</tr>
<?php endforeach;?>
Since I have stored the value in a varaible $userFormCount in the controller, I do not get the value in the view file. but how to set and get an array value?
First of all you should avoid repeating your function calls.
First two lines of your function can be replaced by:
$users=$this->User->find('all');
$this->set('users', $users);
Next you can alter your object before passing it to view and add the property you are calculating to that object.
function users(){
$users=$this->User->find('all');
// notice the & here
foreach($users as & $user):
// you are repeating the same line here as well, that's not necessary
$user['User']['custom_field'] = $this->Form->find('count',
array('conditions'=>array('Form.created_by'=>$user['User']['id'])));
endforeach;
$this->set('users', $users);
}
and you can then use it in your view like any other field that you get from the database. To improve it even firther you may want to consider moving this code to a afterFind callback in your User model, at least if this custom values are used more then once.
You already know how to set an array value :-)
In this code you are setting the array 'users'
$this->set('users',$this->User->find('all'));
So you can do the same for the other array:
$this->set('userFormCount',$userFormCount);
...and read it in the same way as your users variable.
But please look at RaYell's answer, because it explains how to make your current code better.

Resources