Passing value from controller to layout - php

I have tried to pass a value from controller to layout using this but it does not work:
foreach ($user_info_details as $details):
$first_name = $details['first_name'];
endforeach;
Zend_Layout::getMvcInstance()->assign('first_name',$first_name);
and retrieve it using
<?php echo $this->layout()->first_name; ?>
but it shows blank in every case

To output values to your view easily, in your controller use:
$this->view->first_name = $first_name;
And in your view, access it like this:
echo $this->first_name;

Related

Display data from database using html encode(yii2)

I want to display the specific data from database using <?= Html::encode() ?>
Let say, i get the specific column of the model as below:
<?php $model = ExampleModule::find()->select('anycolumn')->all(); ?>
And then, what should i write to the <?= Html::encode(anystatement) ?>to display the values ?
you can encode the single column result and ->all() return a collection fo models
so first you should access to single model, eg: assuming you obtain your collection of models as an array
<?php $model = ExampleModule::find()->select('anycolumn')->asArray->()all(); ?>
you can encode the single column result for the first model this way
<?= Html::encode($model[0]['your_column']); ?>
Get all data first .
First step
<?php $model = ExampleModule::find()->select('anycolumn')->asArray()->all(); ?>
Second step
<?= Html::encode($model[0]['anycolumn']); ?>
First of all ExampleModule::find()->select('anycolumn')->all() returns an array of records.
If you want to get first found record you need to use
<?php
$model = ExampleModule::find()->select('anycolumn')->one();
?>
Then
<?= Html::encode($model->anycolumn) ?>
Or if you want to display all records:
<?php
foreach (ExampleModule::find()->select('anycolumn')->all() as $model) {
echo Html::encode($model->anycolumn) . '<br>';
}
?>

View included by $this->include does note recieve passed params

Is possible pass parameter to view when i use $this->include method in another view?
In example:
<?php
foreach ($destaques as $camping) {
$this->include('partial', ['camping' => $camping])
}
?>
But partial.php dont recieve $camping value.
When using $this->include you're making an echo of a view into an other one. So by default, the view you're loading will have acces to any data you gave to the parent view but not variables you declared into it.
A few options in your case :
Using the view method :
foreach ($destaques as $camping) {
echo view('partial', ['camping' => $camping]);
}
Moving your foreach loop in the partial view so you'll use $destaques into it.
<?php
// dont forget to echo
echo $this->include('partial')
?>
// or this way with short tags enabled
<?= $this->include('partial') ?>
And just embed your partial view in your previous foreach loop
foreach ($destaques as $camping) {
// whatever your partial view is
}
Hello I try using the parameter option. I test code and it s working. But note the include view layout method work like php default include function and it does not display until you echo it.
Here is my code which I use to test yours and it worked for me. Check it
$inc = '';
foreach ($destaques as $camping) {
$inc .= $this->include('partial', ['camping' => $camping])
}
echo $inc;
And this displayed and worked for me check it. if this is not what you were expecting, just call my attention

Passing variable to a controller in CodeIgniter

Hello I am displaying data in the view but each time that i display i want to store the Student_id in a variable the pass that variable to a controller but it is giving me errors
foreach($results as $row)
{
$this->load->helper('url');
echo '<a href="http://localhost/amref/mama/get_student?var1=<?php echo $row->student_id;?>" '.'<tr><td>'.$row->student_fname.' '.$row->student_lname.'</td><td>'.$row->gender.'</td><td>'. $row->res_district.' , '.$row->res_region.'</td><td>'.$row->collage_name.'</td><td>'.$row->course_name.'</td></tr>'.'</a>';
}
Your syntax is incorrect and I am pretty sure you want that anchor tag nested inside a <td> element, maybe like this:
echo '<tr><td>'.$row->student_fname.' '.$row->student_lname.'</td><td>'.$row->gender.'</td><td>'. $row->res_district.' , '.$row->res_region.'</td><td>'.$row->collage_name.'</td><td>'.$row->course_name.'</td></tr>';

Yii How to getState() and display the variable

Im currently doing login and result are worked fine. However , I wish to get the firstname and lastname of the user from DB.
As I know getState() able to get the variable data from the DB.
Following bellow is the code for login:
$username = $_POST['username'];
$userpass = $_POST['userpass'];
$record=Games::model()->findByAttributes(array('email'=>$username));
if($record===null){
//somethings
}else if($this->checkPassword($record->password,$userpass)){
//somethings
}else
{
$this->_id=$record->id;
$this->_email=$record->email;
Yii::app()->user->setState('id', $record->id);
Yii::app()->user->setState('email', $record->email);
Yii::app()->user->setState('firstname', $record->firstname);
Yii::app()->user->setState('lastname', $record->lastname);
//go to somethings
}
In View
<?php
$username_first = Yii::app()->user->getState('firstname');
$username_last = Yii::app()->user->getState('lastname');
?>
<a href="#" ><?php echo $username_first.' '.$username_last; ?></a>
What is the problem of my code in view ? Any better suggestion to getState() the data I need ?
Updated :
I tried print out in controller ... it worked ... but why view cant ?
print_r(Yii::app()->user->getState('firstname'));
getState() is not dedicated to get variable from database. As Yii's official document defines it:
Returns the value of a variable that is stored in user session.
By setting state you store your variable's value into user session and you can get that value via getState().
As a suggestion, when you use getState(), pass a default value into the second parameter like below:
$email=Yii::app()->user->getState('email',NULL);
if(!is_null($email)) //do something
It is even better to check state before getting it by hasState() like below:
if(Yii::app()->user->hasState('email')){
$email=Yii::app()->getState('email',NULL);
}
Another note is that, it is better to get stored values in Controller and pass them to the view, not getting them in view. Take a look:
Controller
$email=Yii::app()->user->getState('email'); //it is better to check it via has state, and also passing a default value
$this->render('view',array(
'userEmail'=>$email
));
View
<h2><?php echo $email; ?></h2>
UPDATE
There is a condition that you may need to just get stored value into session (by setState()), So you can just do like below in view:
if(Yii::app()->user->hasState('firstname')) { echo Yii::app()->user->getState('firstname'); } //All done

HREF to call a PHP function and pass a variable?

Is it possible to create an HREF link that calls a PHP function and passes a variable along with it?
<?php
function sample(){
foreach ($json_output->object ){
$name = "{$object->title}";
$id = "{$object->id}";
print "<a href='search($id)' >$name</a>";
}
}
function search($id){
//run a search via the id provide by the clicking of that particular name link
}
?>
You can do this easily without using a framework. By default, anything that comes after a ? in a URL is a GET variable.
So for example, www.google.com/search.html?term=blah
Would go to www.google.com/search.html, and would pass the GET variable "term" with the value "blah".
Multiple variables can be separated with a &
So for example, www.google.com/search.html?term=blah&term2=cool
The GET method is independent of PHP, and is part of the HTTP specification.
PHP handles GET requests easily by automatically creating the superglobal variable $_GET[], where each array index is a GET variable name and the value of the array index is the value of the variable.
Here is some demo code to show how this works:
<?php
//check if the get variable exists
if (isset($_GET['search']))
{
search($_GET['search']);
}
function Search($res)
{
//real search code goes here
echo $res;
}
?>
Search
which will print out 15 because it is the value of search and my search dummy function just prints out any result it gets
The HTML output needs to look like
anchor text
Your function will need to output this information within that format.
No, you cannot do it directly. You can only link to a URL.
In this case, you can pass the function name and parameter in the query string and then handle it in PHP as shown below:
print "<a href='yourphpscript.php?fn=search&id=$id' >$name</a>";
And, in the PHP code :
if ($_GET['fn'] == "search")
if (!empty($_GET['id']))
search($id);
Make sure that you sanitize the GET parameters.
No, at least not directly.
You can link to a URL
You can include data in the query string of that URL (<a href="myProgram.php?foo=bar">)
That URL can be handled by a PHP program
That PHP program can call a function as the only thing it does
You can pass data from $_GET['foo'] to that function
Yes, you can do it. Example:
From your view:
<p>Edit
Where 1 is a parameter you want to send. It can be a data taken from an object too.
From your controller:
function test($id){
#code...
}
Simply do this
<?php
function sample(){
foreach ($json_output->object ){
$name = "{$object->title}";
$id = "{$object->id}";
print "<a href='?search=" . $id . "' > " . $name . "</a>";
}
}
if (isset($_REQUEST['search'])) {
search($_REQUEST['search']);
}
function search($id){
//run a search via the id provide by the clicking of that particular name link
}
?>
Also make sure that your $json_output is accessible with is the sample() function. You can do it either way
<?php
function sample(){
global $json_output;
// rest of the code
}
?>
or
<?php
function sample($json_output){
// rest of the code
}
?>
Set query string in your link's href with the value and access it with $_GET or $_REQUEST
<?php
if ( isset($_REQUEST['search']) ) {
search( $_REQUEST['search'] );
}
function Search($res) {
// search here
}
echo "<a href='?search='" . $id . "'>" . $name . "</a>";
?>
Yes, this is possible, but you need an MVC type structure, and .htaccess URL rewriting turned on as well.
Here's some reading material to get you started in understanding what MVC is all about.
http://www.phpro.org/tutorials/Model-View-Controller-MVC.html
And if you want to choose a sweet framework, instead of reinventing the MVC wheel, I highly suggest, LARAVEL 4

Categories