I'm using a function to generate some HTML in the view. I can not access the data that send form controller within that function.Following is the simplified code.
Controller
$data["var"] = "something";
$this->load->view("the_view",$data);
View
function some(){
global $var;
echo $var;
}
some(); //not working
echo $var; //working
I can move this function to the controller. Generate HTML in controller and send the generated HTML to view. But I like to keep HTML stuf in the view. How can I do this ?
You can rewrite your function like this
function some($var){
echo $var;
}
some($var);
Hope it will work
You have created an array named as data and var stored into that, so you need to this
function some($data){
echo $data["var"];
}
some($data);
And in case your data array is global, then
function some(){
global $data
echo $data["var"];
}
some();
Related
I am really very surprised to see my function comes out to be undefined on AJAX request. I am wondering does function order matters in AJAX file? Following code will show you my problem -
This is the ajax.php file which is called by jquery/ajax request from index.php. It is supposed to simply print the name -
<?php
if(isset($_POST))
{
$name = 'admin';
echo display_name($name);
function display_name($name)
{
return $name;
}
}
?>
But when this file is called, i get -
Fatal error: Call to undefined function display_name()
But when i change the order of code i.e. function like this -
<?php
if(isset($_POST))
{
$name = 'admin';
function display_name($name)
{
return $name;
}
echo display_name($name);
}
?>
then it displays -
admin
How strange it is!
Thus if really function availability order matters then how the following code works -
It is simple file and it is NOT called by AJAX request. I am simply loading the file and doesn't matter where the function is written. It is working by all using any order of line code -
<?php
$name = 'admin';
echo display_name($name);
function display_name($name)
{
return $name;
}
?>
The following snippet is also working -
<?php
$name = 'admin';
function display_name($name)
{
return $name;
}
echo display_name($name);
?>
So please tell my the reason behind this difference. In page loading code works and on ajax request it doesn't. Why the function display_name() is undefined if it exists?
This has nothing to do with Ajax.
From the PHP manual:
Functions need not be defined before they are referenced, except when a function is conditionally defined
The order matters in your first example because the function is inside an if statement.
I think this issue is not related to calling file as Ajax request. This is more related to PHP's function declaration scope.
Consider the following situation:
if(true)
{
function bar()
{
}
$functions = get_defined_functions();
print_r($functions["user"]);
}
function foo()
{
}
this code will give bar and foo as defined functions.
But the following situation produces just foo function:
if(true)
{
$functions = get_defined_functions();
print_r($functions["user"]);
function bar()
{
}
}
function foo()
{
}
From this we see that all the function in a file are defined and available imediately when the file is loaded, but if blocks are interpreted as the execution passes step by step.
Hope this helps.
I just want to ask if its possible to call variables on class to another page of the site. I have tried calling the function's name and inside the parenthesis. I included the variable found inside that function e.g:
<?php
$loadconv -> loadmsg($msgReturn);
echo $loadconv;
?>
But it didn't work.
Do you want something like this?
class Load
{
public $msgReturn;
__construct()
{
}
public function loadMsg($param)
{
$this->msgReturn = $param;
}
}
Then you could do
$loadConv = new Load();
$loadConv->loadMsg('just a string');
echo $loadConv->msgReturn; // 'just a string'
I'm writing now an app, which is supposed to be as simple as possible. My controllers implement the render($Layout) method which just does the following:
public function render($Layout) {
$this->Layout = $Layout;
include( ... .php)
}
I don't really understand the following problem: in my controller, I define a variable:
public function somethingAction() {
$someVariable = "something";
$this->render('someLayout');
}
in the php view file (same I have included in the render function) I try to echo the variable, but there is nothing. However, if I declare my action method like this:
public function somethingAction() {
global $someVariable;
$someVariable = "something";
$this->render('someLayout');
}
and in my view like this:
global $someVariable;
echo $someVariable;
it does work. Still it is annoying to write the word global each time.
How can I do this? I'd rather not use the $_GLOBAL arrays. In Cake PHP, for example, there is a method like $this->set('varName', $value). Then I can just access the variable in the view as $varName. How is it done?
From the PHP Manual on include:
When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.
When you do
public function somethingAction() {
$someVariable = "something";
$this->render('someLayout');
}
the $someVariable variable is limited to the somethingAction() scope. Calling your render() method will not magically make the variable available in render(), because the render() method has it's own variable scope. A possible solution would be to do
public function somethingAction() {
$this->render(
'someLayout',
array(
'someVariable' => 'something'
)
);
}
and then change render() to
public function render($Layout, array $viewData) {
$this->Layout = $Layout;
include( ... .php)
}
You will then have access to $viewData in the included file, given that you are not trying to use it in some other function or method, e.g. if your included file looks like this:
<h1><?php echo $viewData['someVariable']; ?></h1>
it will work, but if it is looks like this:
function foo() {
return $viewData['someVariable'];
}
echo foo();
it will not work, because foo() has it's own variable scope.
However, a controller's sole responsibility is to handle input. Rendering is the responsibility of the View. Thus, your controller should not have a render() method at all. Consider moving the method to your View class and then do
public function somethingAction() {
$view = new View('someLayout');
$view->setData('someVariable', 'something');
$view->render();
}
The render() method of your View object could then be implemented like this:
class View
…
$private $viewData = array();
public function setData($key, $value)
{
$this->viewData[$key] = $data;
}
public function render()
{
extract($this->viewData, EXTR_SKIP);
include sprintf('/path/to/layouts/%s.php', $this->layout);
}
The extract function will import the values of an array in the current scope using their keys as the name. This will allow you to use data in the viewData as $someVariable instead of $this->viewData['someVariable']. Make sure you understand the security implications of extract before using it though.
Note that this is just one possible alternative to your current way of doing things. You could also move out the View completely from the controller.
Using global you're implicitly using the $_GLOBALS array.
A not recommended example, because globals are never a good thing:
function something() {
global $var;
$var = 'data';
}
// now these lines are the same result:
echo $_GLOBALS['var'];
global $var; echo $var;
Why don't you use simply the $this->set function?
public function render($Layout) {
$this->Layout = $Layout;
include( ... .php)
}
public function somethingAction() {
$this->set('someVariable', "something");
$this->render('someLayout');
}
// in a framework's managed view:
echo $someVariable;
I'm sorry not to know your framework in detail, but that makes perfectly sense.
Actually how it's done: There is a native extract function, that loads an associative array into the current symbol table:
array( 'var0' => 'val0', 'var1' => 'val1')
becomes
echo $var0; // val0
echo $var1; // val1
That's most likely 'what happens'.
I have a question. Ive made some mvc'ish structure for my website.
I want to be able to use my variables inside my view without assigning them.
Let me explain by an example, here's a small part of my controller :
class members extends controller{
public function _construct()
{
parent::__construct();
}
public function index()
{
$test = 'test variable';
$data['test2'] = 'test variable 2';
view::setTemplate('header');
view::setTemplate('homepage', $data);
view::setTemplate('footer');
}
}
in the setTemplate() function of my view class, I use extract($data) and then I include the view file. This way I can echo $test2 and get 'test variable 2' as output, as expected.
However, is there a way to make the view 'remember' the first $test variable without having to add it to the setTemplate function ?
DISCLAIMER: THIS CODE HAS NOT BEEN TESTED
You can use variable variables to define the variables within the setTemplate method and include the view directly from there.
<?php
function setTemplate($view, $data) {
if(is_array($data)){
foreach($data as $var => $val){
$$var = $val;
}
}
include('views/'.$view.'.php');
}
?>
as per title above, when I trigger the controller with a hyperlink, it does runs the controller function but I couldn't get the value of SESSION after redirect from controller. The code is as follows...
function langpref($lang){
$this->load->helper('url');
redirect(ABSOLUTE_PATH, 'location');
$this->session->set_userdata('cur_lang', 'xxx');
}
*Note: ABSOLUTE_PATH is a constant of the hyperlink, and I already load the SESSION library in the autoload file.
In my view file, I written the code as follows...
<?php echo $this->session->userdata('cur_lang');?>
and it doesn't print out the SESSION value.
First Approach: You cannot access session variables like that
<?php $ci =& get_instance(); ?>
<div>
<?php echo $ci->session->userdata('cur_lang') ?>
</div>
Second Approach: Another way you can do this is pass the session data to the view
On your controller
$data['userdata'] = $this->session->userdata;
$this->load->view('your/view', $data);
On your view
echo $userdata['cur_lang'];
Shouldn't that be:
function langpref($lang){
$this->load->helper('url');
$this->session->set_userdata('cur_lang', 'xxx');
redirect(ABSOLUTE_PATH, 'location');
}
And in your view:
<?php echo $this->session->userdata("cur_lang"); ?>
session is global variable. if you want to use it in class or function. you need to access it by passing session variable as function argument. or you need to use global command. such as;
class XXX{
public function processSession($_SESSION){
return $_SESSION['xx'];
}
}
or you could use global directive
class XXX{
public function processSession(){
global $_SESSION;
return $_SESSION['xx'];
}
}
other way is starting session in function
class XXX{
public function processSession(){
session_start();
$_SESSION['xx'] = 'aaaa';
return $_SESSION['xx'];
}
}
other way, you cannot access session variable in function or class function