Accessing variable from helpers file in views - Laravel 4.2 - php

I have made a helpers file in my /app folder which contains the following:
$constants = DB::table('constants')->get();
foreach ($constants as $constant) {
$C[$constant->type] = $constant->value;
}
echo $C['business_name'];
This works, but if I try
echo $C['business_name'];
In one of my views I get an error of $C undefined. I have added the helpers file to my start/global file and I know it works...
What steps should I take to use this variable in my views?

You need to pass data directly into the view via the second parameter of View::make or alternatively View::make('someBlade')->with(data);
So in your case it might be something like:
View::make('someBlade', $C);
If you really, really want globals, you can do this for views:
View::share('c', $C);
http://laravel.com/docs/4.2/responses

I think you need to create a function in helper and call that function in view.
it will automatically display value of this variable but you need change "echo" replacing with "return" in function last line.
function xyz()
{
$constants = DB::table('constants')->get();
foreach ($constants as $constant) {
$C[$constant->type] = $constant->value;
}
return $C['business_name'];
}
Call this function in your view like this. {{xyz()}}
if you are returning array {? $abc=xyz(); ?} make blade filter not echoing value pass this function to array variable and show like this {{$abc['business_name']}}

Related

how to get data into view from controller in cakephp

$this->loadModel('Siteconfig');
$data=$this->Siteconfig->find('all');
foreach($data as $data)
{
$email=$data['Siteconfig']['email'];
$companyname=$data['Siteconfig']['companyname'];
$cfa=$data['Siteconfig']['cfa'];
}
Above is Controller
How to get cfa data in view using CAKEPHP?
In your controller method, you can use this
$this->set('var_name_in_view', $cfa); //You will find the variable $cfa as $var_name_in_view in your view(You have to use $var_name_in_view in view)
You can also send multiple variables to your view at once
$this->set(compact('cfa', 'another_variable')); //You can acceess using $cfa and $another_variable
Compact makes an array keeping the variable name as key and variable value as the value for the array key.
Modify your script to
$this->loadModel('Siteconfig');
$data=$this->Siteconfig->find('all');
$cfa = '';
foreach($data as $datum)
{
$email=$datum['Siteconfig']['email'];
$companyname=$datum['Siteconfig']['companyname'];
$cfa=$datum['Siteconfig']['cfa'];
}
$this->set('view_cfa',$cfa);
This will fix it. Your foreach was reassigning $data.

How to set variable in routes - laravel

I have the following route in my routes.web.php. Looks like this...
Route::get('/spielerAuswahl/{somevar}', 'SpielplanController#getHeimGast');
In the variable {somevar} I have for example Nr1=111&Nr2=222. The route works fine in a get to SpielplanController...
public function getHeimGast(){
$var = $somevar;
return view('test')->with('variableControllerSomevar', $var);
}
In this function I want to put the Nr1=111&Nr2=222 in the $var and make then an easy output of this variable in the view. How to get that?
Though the answer come late, but you can put Nr1=111&Nr2=222 as uri with proper encoding. For javascript, encodeURIComponent can be used to escape certain characters to form a valid URI:
encodeURIComponent('Nr1=111&Nr2=222');
// output: Nr1%3D111%26Nr2%3D222
To request server, using this url:
http://example.com/spielerAuswahl/Nr1%3D111%26Nr2%3D222
The controller function then receives correct form of variable as follows:
public function getHeimGast ($somevar) {
$somevar // = Nr1=111&Nr2=222
}
Only URI segments can be map as route variables in Laravel, no query string variables.
You can simply fetch those like these:
public function getHeimGast(){
$vars = request()->all();
return view('test', $vars);
}
In your test blade, you can use those query vars, eg( Nr1=111&Nr2=222 ) as:
$Nr1, $Nr2 ... and so on...
NOTE: given a query like this: /spielerAuswahl?Nr1=111&Nr2=222

PHP Function named after the Value of a Variable

I have a PHP function that I need to create that executes PHP or HTML Code that is stored in a variable. That's one problem. Another problem is the fact that the function will be created multiple times and will need a unique name so I've named it after $title but I'm not sure if this will create the function from the value of $title or if it will create a function called $title or if it just won't work. This is my code:
$title = $_POST['sogo_aso_name'];
$output = $_POST['sogo_aso_output'];
Some Code Here...
function $title ($output)
{
//Some Code Here to execute $output...
}
So... That's my problem. I'm not sure what will execute the $output cleanly so it can be shown on a page. And I don't know if the function will be named after the value of $title
Thanks for any help!
Ethan Brouwer
You can use call_user_func like this:
call_user_func($title, $output);
But it's really strange to change name of one function.
What you're trying to do is unfortunately not possible. You could create namespaces if you need a common name to prefix your functions with.
namespace fArr {
function func1 (args) {}
function func2 (args) {}
}
To call them you can use:
namespace {
fArr\func1($title, $output);
}
Generally you want to avoid creating totally anonymous functions. Maybe create a function that handles the title and output for all requests. Alternatively create a new object with the func name as a property and output assigned to it.
$title = $_POST['sogo_aso_name'];
$output = $_POST['sogo_aso_output'];
// Store data in an object named after the value of $title
// Note that constant use of this can get very messy, and very confusing
$$title = (object) array('output' => $output);

PHP MVC loop in the view

I consider myself as a php beginner, so it may be possible that this question is too easy for someone, but I got really confused on how to solve it. I am trying to loop something from the database in my views. So, in a quick way I solved it like this:
I've created a function in my model that does the loop and in the same time is creating the html and saves it in a variable. Then, I get that variable from my controller and I pass it in my view. But, it seems that this is not a good way to solve it, since if I want to change my html I need to enter my model function instead some of the view files.
Then, I've created another function in my model that looks like this:
function displayUsers() {
$sql = $this->pdo->prepare('select * from user');
$sql->execute();
while($row = $sql->fetch())
$results[] = $row;
return $results;
}
Now... I take the result in my controller, and send it in the view, but then... I don't know how to extract the results from my variable. I have done something like this:
while($output) {
foreach($output[$i] as $key => $value)
$data[$key] = $value;
echo $data['email'];
$i++;
}
But then, in the end it says to me undefined offset, which means I am referring to an array key that doesn't exist. Can anyone help me on how to solve this issue?
Proper MVC shouldn't have any output in the model or the controller.
Ideally you would have a model that just gets the raw data and returns it in the controller. The controller can then build up an array of values that we'll call data. For example:
Controller
$data['users'] = $this->MyModel->getusers(); // Getting the users from your model
$data['posts'] = $this->MyModel->getposts(); // Getting the posts from your model
$this->getTemplate('userdisplay', $data); // Get the template userdisplay and pass data
This gets the data from the model, and then assigns it to a key within the "data" variable. You can then pass the data variable into the template. You'll then have two variables to work with in the template, $users and $posts.
You'll need a "getTemplate" function that properly maps the data array to individual variables for use in the template, but all of the display should be located in the template.
To answer your specific question at the end, something like this should work in the template:
if (count($users) > 0) {
foreach ($users as $person) {
echo $person['email'];
}
}
You should be able to do this:
foreach($output as $row) {
echo $row['email'];
}

codeigniter loading one view in to another getting the variables

When loading one view in to another with jquery i do that.
In file view 5
load("<?php echo site_url("controller/name")?>");
I'm loading the name function that contains the view.But how to get the variables from that name function that is loading a view in to that view ?
function name(){
$a['aa'] = 1;
$this->load->view("file",$a);
}
And i wan to load the $aa variable where i'm loading the function with jquery , this is the loaded function - name, the default view is different and with different function in the controller.
Why don't you pass the $aa through the controller like this?
function name($aa = 1) {
$this->load->view('file', $aa);
}
Then you can do load('<?php echo site_url('controller/name/1') ?>');
You might want to sanitize $aa as well.

Categories