CakePHP - passing multiple values from a view element to the controller - php

I want to pass some values to my controller from the view.
Actually I'm using the following code:
<?
echo $this->element('produtos-categoria', array(
'categoria_id' => $produtos['Produto']['categoriasproduto_id'],
'produto_id' => $produtos['Produto']['id']
));
?>
But I'm not able to get the second value in my controller, just the first value is coming:
public function list_categories($categoria_id = null, $produto_id = null ) {
pr($produto_id); exit; //empty
}
Anyone can help how to get the second value?

I donno what are you trying to achieve but to get the second variable you would need to set the second parameter to your method.
// If you want to set variable from a function and get it from an other function
public function an_other_function(){
$this->listacategorias(22, 333);
}
public function listacategorias($categoria_id = null, $produto_id = null ) {
var_dump($categoria_id);
var_dump($produto_id);
}
// If you want to set and get variable from url
host_or_domain_name/controller_name/listacategorias/22/33

Related

How Access Database Values Using Array In Laravel

I am creating a web site. So , I have stored data in the database. Now I want to view data from two different tables. Then I tried a method like below. But , it gives me this error -
Trying to get property 'firstname' of non-object (View: D:\wamp64\www\cheapfares\resources\views\invoices\des.blade.php)
But , clearly firstname is in the database table.
How can I Fix this ??
Controller page. ( InvoicesController.blade.php )
public function userinvoice($terms = '',$invoiceNo = '')
{
$invoice = Invoice::where('invoicereference', $invoiceNo)->get()->first();
$tr = DB::table('termsandconditions')
->where('topic', $terms)->get()->first();
$twoar = [];
$twoar['inv'] = $invoice;
$twoar['trms'] = $tr;
return view('invoices.des', ['twoar' => $twoar]);
}
View page. ( des.blade.php )
{{$twoar['inv']->firstname}}
{{$twoar['trms']->topic}}
Route.
Route::get('/invoice/adminuser-invoice/{invoiceno}', [
'uses' => 'InvoicesController#adminuserinvoice',
'as' => 'invoice.adminuser'
]);
Although casting the response to Array might be a suitable solution, the cause of your exception most likely lies in not having a valid entry in the database.
You can improve your code like this to mitigate that:
public function userinvoice($terms, $invoiceNo)
{
// Load invoice, or throw ModelNotFoundException/404 without valid entries.
$invoice = Invoice::where('invoicereference', $invoiceNo)->firstOrFail();
// load the terms.
$terms = DB::table('termsandconditions')
->where('topic', $terms)->first();
return view('invoices.des', compact('invoice', 'terms'));
}
In this example I made $terms and $invoiceNo obligated arguments in the route. To ensure the query will provide proper results. In addition an Invoice entry is now required with firstOrFail(), the terms is optional. Instead of assigning both variables to an array, I'm sending them both to the view so you can assert their value properly without cluttering using array key access.
Your view:
{{$invoice->firstname}}
{{$terms->topic}}
Try this below code:
public function userinvoice($terms = '',$invoiceNo = '')
{
$invoice = Invoice::where('invoicereference', $invoiceNo)->get()->first();
$tr = DB::table('termsandconditions')
->where('topic', $terms)->get()->first();
return view('invoices.des', ['tr'=>$tr,'invoice'=>$invoice]); //Directly pass the mulitple values into the view
}
And your view page like this:
{{$invoice->firstname}}
{{$tr->topic}}
Its may help for you friend.

session values not passing from one controller to another in code igniter

I have stored the session varible in one controller and getting the value in another controller but the value is not passing
here is one controller
function control1 {
$this->session->set_userdata(array(
'value1' => $this->input->post('value1'),
'value2' => $this->input->post('value2'),
);
echo $this->session->userdata('value1'); //it returns value
}
function control2 {
echo $this->session->userdata('value1'); //it returns empty value
}
What may be the reason for this
You should check if the session has been set in the second controller like this:
function control2 {
if (!isset ($this->session->userdata('value1'))){
redirect('control1');
} else {
echo $this->session->userdata('value1'); //it returns empty value
}
If you haven't run through the control1 first, there won't be any session set yet, you see.
$this->session->set_userdata( 'values', array(
'value1' => $this->input->post('value1'),
'value2' => $this->input->post('value2'),
) );
$a = $this->session->userdata('values'); //it returns value
print_r( $a );
Try this code.
How the way you go into control2()? I mean, did you call control1() first to set the sessions value before goes to control2()? If so then the sessions value should be passed.

Pass parameters in cakephp function

$this->requestAction("/survey/responsethereuser/qid:$qid,uid:$uid,sid:$sid");
In my controller
function responsethereuser($qid = null, $uid=null, $sid=null)
It is showing on taking value of qid
$qid=1,uid:3,sid:1
How to get all the parameter values correctly?
try this
$this->requestAction("/survey/responsethereuser/", array('qid'=>$qid, 'uid'=>$uid, 'sid'=>$sid));

Pass PHP Variable/Class to Separate File

I hardly know any OOP PHP so I don't know how to properly pass variables.
I have a function that passes my results to a .php file that has the HTML table to show the results. I want to pass a variable "$config" to that file so I make a if/else statement.
Here is the function that passes my results to the file, that I need to pass the "$config" variable to create my if/else.
public function outputBrowser($results)
{
// $rows = array();
// print_r($results);
// die();
$config = $this ->config
$this->EE->load->add_package_path($this->report_path);
return $this->EE->load->view('output_browser', array("results" => $results), TRUE);
}
The main objective is to pass the selected drop down item to my file so I can output various HTML tables based on that selection. That selected value is in my config class or $config variable.
As my comment seemed to have fixed it:
Just add the $config variable to the array you pass:
array("results" => $results, "config" => $config)

Joomla: Passing a variable into getInput()

I'm building a backend component (1.6 / 1.7 / 2.5) where I need to pass a variable from another view into a field in a new record. Variable passing is working fine.
My problem is using getInput().
To start with different doc pages have different amounts and formatting of parameters - confusing! For example:
http://docs.joomla.org/API16:JForm/getInput: getInput($name, $group= '_default', $formControl= '_default', $groupControl= '_default', $value=null)
vs
http://docs.joomla.org/JForm::getInput/1.6:
public function getInput (
$name
$group=null
$value=null
)
The problem:
I just need to pass a variable as a default value, something like:
echo $this->form->getInput('id', $value=$this->userID );?>
The above code makes the input field disappear. If I take out the $value=$this->userID the input field shows up though obviously doesn't have any default value. I've also tried:
$value=$this->userID;
echo $this->form->getInput('id', $value );
And same problem, the input field goes away. I tried a few other variations but basically if I try to put anything else within getInput() it doesn't work nor can I find any good working examples of how to use these other parameters.
What am I doing wrong?
Thanks!
According to the source, this is the correct API:
getInput($name, $group = null, $value = null)
And getInput() just calls getField():
getField($name, $group = null, $value = null)
Which means you should be doing this to set a default value:
echo $this->form->getInput('id', null, $this->userID ); // Returns the $field->input String
Or:
$field = $this->form->getField('id', null, $this->userID ); // Returns the JFormField object

Categories