I'm successfully updating the innerHTML of a DIV through xajax (when I click a link) but only when I assign HTML in the function itself, not when I call it from a different function.
To explain
// For the sake of testing
$output=rand(20,40);
// Add new HTML to container through $output
$ajax_resp->assign('div_container','innerHTML', $output);
return $ajax_resp;
This works fine. When I call this function through clicking the link, the container updates with a random number. However when I change $output to
$output=$compile->show('text');
Which is (simplified)
function show($var) { echo $var; }
It does not show it. The function show works perfectly outside of xajax. Any suggestions?
function show($var) { echo $var; }
This function doesn't return anything, it echo's the value to the screen. However, most functions you probably want to return a value, so they can work with it. Basically, when you do
$output=$compile->show('text');
It will get no input from the show method, because show doesn't return any value. I think if you change it to the following:
function show($var) { return $var; }
It will work.
Have you tried to change function show($var) { echo $var; } to function show($var) { return $var; }. Should solve your problem
Related
I have this WordPress function:
function content_before_after($content) {
return 'something goes here';
}
add_filter('the_content', 'content_before_after');
...which works fine except for one small problem: If there is more than one instance of the_content() on any page template, the returned text will appear for each of them.
For reasons I can't go into I won't be able to modify the templates.
But the problem I need to be solved is: how can I change this function so that the returned text is only output into the first instance of the_content() of my page template?
Here static variable will help you:
function content_before_after($content) {
// define variable as static
static $content_shown;
// if variable has __no__ value
// it means we run function for the first time
if (!$content_shown) {
// change value and return required string
$content_shown = true;
return 'something goes here';
}
// empty string will be returned in second and other function calls
return '';
}
I have one function call remove_certificate_packages($certificate_id, array_keys($package_id)) this will invoke the below function
function remove_certificate_packages($certificate_id, $package_id)
{
if (is_numeric($package_id)) // so this is list of package id:s
$package_id = array($package_id);
if (!$package_id) return true;
**notify_package_unlinked($certificate_id,array_keys($package_id));**//one more func call
return true;
}
in this function, I have one more function call "notify_package_unlinked" I need to pass the "$package_id". It will call the appropriate function but the problem is, in the "notify_package_unlinked" function the value is showing "Array". What is the problem? Could you please help
function notify_package_unlinked($certificate_id,$package_id)
{
$query="select id,filename,version from packages where id =$package_id";
$res = db_query($query);
$package= db_fetch_object($res);
$packid=$package->id;
$packname=$package->filename;
$packversion=$package->version;
print "$packid"; // here it is printing the value"Array"
}
I got my output using foreach loop .
foreach($package_id as $id){$pack=$id;}
I have basically 2 queries related this:
Consider the following PHP function in a class say xyz.php
function sendResponse(){
$car="Lambo";
echo $car;
}
function sendResponseTwo(){
$car="Lambo";
echo $car;
return $car;
}
function getResponse(){
//case 1:
$carName=$this->sendResponse();
//ABOVE WON'T WORK AS THE FUNCTION RETURNS NOTHING.
//case 2:
$carName=$this->sendResponseTwo();
//THIS WILL PRINT THE NAME OF CAR
}
In case 1, is there any way to get the echo values by calling the function in another function but without using return statement?
In case2, is there any way to stop the values printed by echo statement (I want only returned value)?
Answer to your both the question lies in output buffer(ob), Hope this will help you out in understanding. Here we are using three function ob_start() will start output buffer and ob_end_clean() will clean the output of buffer and ob_get_contents will give you output as string which is echoed till now. This will help you better understand ob_get_contents
Try this code snippet here
<?php
class x
{
function sendResponse()
{
$car = "Lambo1";
echo $car;
}
function sendResponseTwo()
{
$car = "Lambo2";
echo $car;
return $car;
}
function getResponse()
{
//case 1:
$carName = $this->sendResponse();
//ABOVE WON'T WORK AS THE FUNCTION RETURNS NOTHING.
//case 2:
$carName = $this->sendResponseTwo();
//THIS WILL PRINT THE NAME OF CAR
}
}
ob_start();//this will start output buffer
$obj= new x();
$obj->getResponse();
$string=ob_get_contents();//this will gather complete content of ob and store that in $string
ob_end_clean();//this will clean the output buffer
echo $string;
?>
You need to use output buffering:
ob_start();
$foo->sendResponse();
$response = ob_get_clean();
That's why it isn't a practical design in the first place. If you make the function always return the value it's trivial to do both things to your liking:
$response = $foo->sendResponse();
echo $foo->sendResponse();
<?=$foo->sendResponse()?>
(This last option is shared for illustration purposes and is not intended to open a flame war about short open tags.)
I've been racking my brains over this one but I'll do my best to describe the problem as best as possible. I have a custom function written within template.php, with a bunch of conditionals. When a condition is true, I would like to assign a value to a variable, and then pass that variable intro a node preprocess function that allows that variables to be rendered on a node template.
The function containing the condition:
function _mytheme_date_repeat_string($vars) {
$exdate_pos = strpos($rrule['WKST'], 'EXDATE:');
if($exdate_pos > 0) {
$vars['testvar'] = 'abc123';
}
}
The preprocess function that I would like to render the variable in for node template use:
function mytheme_preprocess_node(&$vars, $hook) {
$vars['new_variable'] = $testvar;
}
Intended usage in node.tpl.php:
<?php print $new_variable; ?>
I'm not great with PHP, but I know enough about programming to know that variable scope might be an issue here. What would be the best way to implement this? Any guidance is greatly appreciated.
Thanks,
Mark.
If it is not called, your _mytheme_date_repeat_string() function will never be executed. Preprocess functions (ie. any function starting mytheme_preprocess_, are called automatically by Drupal's theme system.
What you need is either move the code of _mytheme_date_repeat_string() in mytheme_preprocess_node() or refactor it and call it.
function _mytheme_date_repeat_string($rrule) {
$exdate_pos = strpos($rrule['WKST'], 'EXDATE:');
if($exdate_pos > 0) {
return 'abc123';
}
else {
return NULL;
}
}
/**
* Prepares variables for node templates.
*/
function mytheme_preprocess_node(&$variables, $hook) {
// Get $rrule from somewhere
$rrule = ... ;
$testvar = _mytheme_date_repeat_string($rrule);
if ($testvar) {
$variables['new_variable'] = $testvar;
}
}
You code does not show where the $rrule calue comes from. I assume you would get it for $variables['node'].
<?php if(isset($staff['FirstName'])){echo encodeToUtf8($staff['FirstName'];} ?>
code above is the code source of my costume function, I create a function to make my code more shorter than the previous one.
this is my function:
function ifset($table_name, $field_name){
if(isset($table_name[$field_name])){
encodeToUtf8($table_name[$field_name]);
}
return $table_name;
return $field_name;
}
so that I can use the code must shorter like this <?php echo ifset($staff, 'IDNumber'); ?>
but it's not working, it give error : Undefined variable if field has no value and not what a expected.
have someone any idea about this case.
You can't return two statements in a single function. You could return as an array.
function ifset($table_name, $field_name){
if(isset($table_name[$field_name])){
encodeToUtf8($table_name[$field_name]);
}
return array($table_name,$field_name);
}
You can then access it using a list.
list($table, $field) = ifset($staff, 'IDNumber');
The return statement is wrong. Here is the fix:
function ifset($table_name, $field_name)
{
if(isset($table_name[$field_name]))
{
return encodeToUtf8($table_name[$field_name]);
}
return '';
}