I have a function in one of my views, and I want to access one of the variables available to the view through CodeIgniter's data array.
For example; in my controller I have this:
$this->load->view('someview', array(
'info' => 'some info'
));
Now, within my view, I have a function, and I want to be able to access the variable $info from within that function scope.
Is that possible?
in your controller:
$GLOBALS['var_available_in_function'] = $value;
Your function:
function get_var() {
global $var_available_in_function;
return $var_available_in_function;
}
I tried this but using globals in my view someview.php doesn't work..
function func_in_view(){
global $info;
print_r ($info); // NULL
}
You may need to pass this as a parameter instead to your function so it's available to it.
function func_in_view($info){
print_r ($info); // NULL
}
I read this method $this->load->vars($array)
in http://codeigniter.com/user_guide/libraries/loader.html
but it's purpose is just to make it available to any view file from any function for that controller. I tried my code above global $info; and it still doesn't work.
You may need to do the workaround by passing it as a parameter instead.
Try including this in your someview.php => print "<pre>";print_r($GLOBALS);print "</pre>"; and the variables passed through $this->load->view aren't included.
I solved this by creating a global variable within the view, then assigning the passed in $data value to the global variable. That way, the information can be read within the view's function without having to pass the variable to the function.
For example, in the controller, you have:
$data['variable1'] = "hello world";
$this->load->view('showpage',$data);
Then in the showpage.php view, you have:
global $local_variable = $variable1;
function view_function() {
global $local_variable;
echo $local_variable;
}
view_function();
Hope this helps!
Related
I'm having difficulty passing some variables from one function to another.
I've tried to make them global with little success.
The variables I would like to pass are the ones from send_email_notifications to send_email_notification_function.
function send_email_notifications($ID, $post) {
global $notificationTitle;
global $notificationPermalink;
$notificationTitle = $post->post_title;
$notificationPermalink = get_permalink($ID);
if(isset($_REQUEST['send_email_notification'])) {
date_default_timezone_set('Europe/London');
wp_schedule_single_event(time() + 10, 'send_email_notification_function_execute');
}
}
add_action('publish_Test_notifications', 'send_email_notifications', 10, 2);
function send_email_notification_function() {
global $notificationTitle;
global $notificationPermalink;
echo $notificationTitle;
echo $notificationPermalink;
$notificationEmail = 'test#test.com';
wp_mail($notificationEmail, $notificationSubject, $notificationContent);
}
add_action('send_email_notification_function_execute', 'send_email_notification_function');
It seems you are using wordpress. Your question might be better answered on https://wordpress.stackexchange.com/.
You should use an object instead of a function for the callable parameter of add_action. Your object can contain your global variables.
For example you can create a php class called EmailNotification and it can have two functions send_email_notification_function and send_email_notifications. This class can have two properties called $notificationTitle and $notificationPermalink.
You can then create an instance of this class and use it as the second argument to add_action.
How would I alter the function below to produce a new variable for use outside of the function?
PHP Function
function sizeShown ($size)
{
// *** Continental Adult Sizes ***
if (strpos($size, 'continental-')!== false)
{
$size = preg_replace("/\D+/", '', $size);
$searchsize = 'quantity_c_size_' . $size;
}
return $searchsize;
Example
<?php
sizeShown($size);
$searchsize;
?>
This currently produces a null value and Notice: undefined variable.
So the function takes one argument, a variable containing a string relating to size. It checks the variable for the string 'continental-', if found it trims the string of everything except the numbers. A new variable $searchsize is created which appends 'quantity_c_size_' to the value stored in $size.
So the result would be like so ... quantity_c_size_45
I want to be able to call $searchsize outside of the function within the same script.
Can anybody provide a solution?
Thanks.
Try using the global keyword, like so:
function test () {
global $test_var;
$test_var = 'Hello World!';
}
test();
echo $test_var;
However, this is usually not a good coding practice. So I would suggest the following:
function test () {
return 'Hello World!';
}
$test_var = test();
echo $test_var;
In the function 'sizeShown' you are just returning the function. You forgot to echo the function when you call your function.
echo sizeShown($size);
echo $searchsize;
?>
But the way you call $searchsize is not possible.
This is an old question, and I might not be understanding the OP's question properly, but why couldn't you just do this:
<?php
$searchsize = sizeShown($size);
?>
You're already returning $searchsize from the sizeShown method. So if you simply assign the result of the function to the $sizeShown variable, you should have what you want.
In codeigniter what is the correct method of passing variables to the callback function?
I have used this,
$var1 = 'some conditions';
$this->form_validation->set_rules("callback__is_value_unique[value, $var1]");
public function _is_value_unique($value, $var1){
echo $var1;
die;
}
This gave me output like shown below:-
value, some conditions
rather than,
some conditions
You must only set your value!
$var1 = 'some conditions';
$this->form_validation->set_rules("callback__is_value_unique[value, $var1]");
public function _is_value_unique($value, $var1){
echo $var1;
die;
}
From the docs..
If you need to receive an extra parameter in your callback function,
just add it normally after the function name between square brackets,
as in: "callback_foo[bar]", then it will be passed as the second
argument of your callback function.
Sounds like you could only pass one extra argument. Which should be a string. If you want to pass more arguments, you could store them somewhere else and just pass an argument, which stores the location of the additional param.
$index = count($this->arguments);
$this->arguments[$index] = array('value', 'some conditions'/*, ...*/);
$this->form_validation->set_rules("callback__is_value_unique[$index]");
public function _is_value_unique($value, $index){
$args = $this->argumts[$index];
echo $args[1];
die;
}
i want to use different objects inside the template. different objects are created on different section of the site.
currently my code is
public function notify ($template, $info)
{
ob_start();
include $template;
$content = ob_get_clean();
//... more further code
}
as you see $info parameter. i dont want to use $info inside templates, but i was to use $photo, $admin or whatever is passed to it.
which i use like
// for feed
$user->notify('email_template_feed.php', $feed);
// for new photo - i would also like to use $user inside templates
$user->notify('email_template_photo.php', $photo);
how can i do that? cant use global, because is inside function and function is getting called dynamically on different locations/sections of the site, which can further extend.
You can't.
Solution 1
Instead you could use an array and extract its values:
public function notify ($__template, array $info)
{
ob_start();
extract($info);
include $__template;
$content = ob_get_clean();
//... more further code
}
Example 1
if you call it with:
$user->notify('email_template_feed.php', array('feed' => $feed));
and inside the template email_template_feed.php:
...
<?=$feed?>
...
it will print:
...
FEED
...
Solution 2
You could also pass the name of the variable as third parameter:
public function notify ($template, $info, $name)
{
ob_start();
$$name = $info;
unset($info);
include $template;
$content = ob_get_clean();
//... more further code
}
Example 2
You could then call it via:
$user->notify('email_template_feed.php', $feed, 'feed');
Can anyone tell me why this works (it echos out "poo"):
$input = "wee";
$val = "poo";
${$input} = $val;
echo $wee;
But this doesn't:
function bodily($input) {
$val = "poo";
${$input} = $val;
}
bodily("wee");
echo $wee;
I want to use this sort of method to play with some $_POST vars. Please tell me if I can explain more... Cheers!
Your variable $wee gets only defined inside the scope of your function bodily(). It is not defined outside this function.
You could make it global, anyway this is not a useful pattern for a real life application:
function bodily($input) {
$val = "poo";
global ${$input}; // make your $wee defined in the global scope
${$input} = $val;
}
bodily("wee");
echo $wee;
outputs
poo
Because the variable is defined locally inside of the function. Let the function return the value and assign it to a variable outside of the function.
Because the variables inside a function are not accessible from outside unless inside the function you use "global $var" or pass it by reference like function (&$var) ...
in order for your code to work you need
<?php
function bodily($input) {
$val = "poo";
${$input} = $val;
echo $wee;
}
bodily("wee");