global variable is not working in php5.6 - php-5.6

I am using PHP 5.6. When I try to print using $GLOBALS["var"] inside the function scope, is displaying nothing on browser window.
Here is my code---
<?php
$var=100;
function showGlobalVariable()
{
printf($GLOBALS["var"]);
}
?>

You have to call the function:
<?php
$var = 100;
function showGlobalVariable(){
printf("The value is %d",$GLOBALS['var']);
}
showGlobalVariable();

The printf function needs a format string as first argument. Try
printf("%d", $GLOBALS["var"]);

Related

Yii2 mpdf with barcode-generator

I'm using yii2 and installed two extension : mpdf , and yii2-barcode-generator-8-types.
Both of them were installed and configured properly and working well.
But what I can't do is to load barcode into pdf.
Here is my code :
Controller :
public function actionPdf()
{
$pdf = Yii::$app->pdf;
$pdf->content = $this->renderPartial('_pdf');
echo $this->renderPartial('_pdf');exit;
return $pdf->render();
}
View :
<div id="showBarcode"></div>
<?php
use barcode\barcode\BarcodeGenerator as BarcodeGenerator;
$optionsArray = array(
'elementId'=> 'showBarcode',
'value'=> '12345678',
'type'=>'code128',
);
echo BarcodeGenerator::widget($optionsArray);
?>
And it show something like this
but If I try to delete all code inside actionPdf() and just write
return $this->render("_pdf");
it show like this:
Please help!!!
I think your controller should be this
public function actionPdf()
{
$pdf = Yii::$app->pdf;
$pdf->content = $this->renderPartial('_pdf');
return $pdf->render();
}
The row with echo $this->renderPartial('_pdf');exit; don't must be used because it prevents the program to invoke correctly the render of the pdf page. If you use this instruction you displays only the render of "html like code" page like you see in result you posted moreover immediately after this instruction you exit form action without invoking the $pdf->render.

How to Add Wordpress Filter to add_filter to next_post_link

How can I add the PARAMETERS to this WP function next_post_link().
add_filter ('next_post_link', 'new_next_post_link');
function new_next_post_link ($args) {
in_same_cat ??????
return $args;
}
I need to change it via FILTER cause I cant change it in the Theme. I tried this.
add_filter ('next_post_link', 'new_next_post_link');
function new_next_post_link ($args) {
$args = "Hello World";
return $args;
}
This works, so I need to know how to change "Hello World" into next_post_link('%link', 'Next post in category', TRUE);
Cheers,
Denis
you don't need to. the third parameter is in_same_cat (bool). by default it is set as false.
<?php next_post_link($format, $link, true); ?>
http://codex.wordpress.org/Function_Reference/next_post_link

Problems in using global variables in functions

I try to use a global variable in my function, see below. I can use the global variable in my func1 to store a value, for Ex. 77, ok. In func2 I try to use the current value that is stored in the global variable but the result is undefined.
Any advice how to get this working?
doGet() {
...
var buttonValue;
...
func1(e,buttonValue) {
...
buttonValue = size;
throw buttonValue;
--> buttonValue ok!
...
}
func2(e,buttonValue) {
...
throw buttonValue;
--> buttonValue undefined!
}
You cannot do that. The value of global variables cannot be changed in handler functions etc.
Use CacheService to store values that have a global scope
Here is a code example:
function func1(){
CacheService.getPrivateCache().put('var_name', 'var_value');
}
function func2(){
var var_value = CacheService.getPrivateCache().get('var_name');
}

How to create custom popup based on success insert or failure mySQL?

I would like to have a customer popup window appear on a page after the insert to mySQL is completed.
I have the "header" going to a particular page, but I would also like to have a custom popup window appear after the page loads.
Here is my current php script. Everything works, but I need to add a popup window based on a success or failure.
<?
.........
if ($result) {
header("location: inv_fc.php"); //NEED TO ADD A CUSTOM POPUP FOR SUCCESS
}
else {
header("location: inv_fc.php"); //NEED TO ADD A CUSTOM POPUP FOR FAILURE
}
?>
Use session variables.
Start the session with session_start(), and then set the session variables using the $_SESSION array. Then in inv_fc.php, check for the existence of the session variable (you must also call session_start() in this file).
So, something like
session_start();
$_SESSION['success'] = ($result) ? TRUE : FALSE;
header('location: inv_fc.php');
// inside inv_fc.php
session_start();
if ($_SESSION['success'] == TRUE) {
// do success stuff
} else {
// do failure stuff
}
where we've used the ternary operator.
header("location: inv_fc.php?success=" . ($result ? 'y' : 'n'));
would be the easiest, then just look for that 'sucess' query parameter in the new page:
if ($_GET['success'] == 'y') {
... success ...
} else {
... epic fail ...
}

CakePHP - get last query run

I want to get the last query CakePHP ran. I can't turn debug on in core.php and I can't run the code locally. I need a way to get the last sql query and log it to the error log without effecting the live site. This query is failing but is being run.
something like this would be great:
$this->log($this->ModelName->lastQuery);
Thanks in advance.
For Cake 2.0, the query log is protected so this will work
function getLastQuery() {
$dbo = $this->getDatasource();
$logs = $dbo->getLog();
$lastLog = end($logs['log']);
return $lastLog['query'];
}
Tested in CakePHP v2.3.2
$log = $this->Model->getDataSource()->getLog(false, false);
debug($log);
In CakePHP 1.x, the data you want is accessible in DataSource::_queriesLog. Cake doesn't really provide a getter method for this member, but the underlying language being PHP, nothing stops you from doing the following:
In app/app_model.php:
function getLastQuery()
{
$dbo = $this->getDatasource();
$logs = $dbo->_queriesLog;
return end($logs);
}
You can use this inline.
$dbo = $this->Model->getDatasource();
// store old state
$oldStateFullDebug = $dbo->fullDebug;
// turn fullDebug on
$dbo->fullDebug = true;
// Your code here! eg.
$this->Model->find('all');
// write to logfile
// use print_r with second argument to return a dump of the array
Debugger::log(print_r($dbo->_queriesLog, true));
// restore fullDebug
$dbo->fullDebug = $oldStateFullDebug;
Simple you can use showLog() function
var_dump($this->YourModel->getDataSource()->showLog());
This is a very late answer, i know, but for whoever needs this in the future, you can always restrict setting debug to your IP, For example:
Configure::write('debug', 0);
if($_SERVER["REMOTE_ADDR"] == '192.168.0.100'){
Configure::write('debug', 2); //Enables debugging only for your IP.
}
Combination of Matt's and blavia's solution (works when debug is not 2):
$dbo = $this->Model->getDatasource();
$oldStateFullDebug = $dbo->fullDebug;
$dbo->fullDebug = true;
// find or whatever...
$this->Model->find("all");
$logs = $dbo->getLog();
$lastLog = end($logs['log']);
CakeLog::write("DBLog", $lastLog['query']);
$dbo->fullDebug = $oldStateFullDebug;
Having a quick skim of the book, cakephp api getLog you could turn on logTransaction. Although having not used it, I'm not sure how it will perform.
Otherwise you could experiment with FirePHP and here is the a guide for it,
You might try DebugKit, although off the top of my head I think you do still need debug 2 to get it to work.
Hopefully something might give you a lead. :)
You can use this:
$log = $this->Model->getDataSource()->getLog(false, false);
pr($log);die;
There are two methods to view the query in CakePHP.
Both methods you have to add the below one line in app/Config/core.php
Configure::write('debug', 2);
First methods :
debug($this->getLastQuery());
where you want to get the query add above line and call this function getLastQuery() on same controller using below code
public function getLastQuery() {
$dbo = $this->TModel->getDatasource(); //Here TModel is a model.what table you want to print
$logs = $dbo->getLog();
$lastLog = end($logs['log']);
return $lastLog['query'];
}
second method :
add the below line in the any Elements files.
<?php echo $this->element('sql_dump'); ?>
This will help you.
echo '<pre>';
$log = $this->YOUR_MODEL->getDataSource();
print_r($log);
exit;