how to make php expressions and execute from database? - php

I have a database table that stores various type of operators and values that make expressions like
if(mydBVal1 mydBExpression mydBval2)
{
// do something....!
}
Here is my code thats shows and example of what I want to say and the help I require
e.g:
$data['myValue'] = 100;
$data['operator'] = "<";
$data['comparison_value'] = 150
if( $data['myValue'] . $data['operator'] . $data['comparison_value'] )
{
///do something......
}
I want that if condition to be read as if(100 < 150){}, but the if condition expression is not working properly!
any one here know how I can make it work?

I think you want to use the eval() function.
Be very careful about sanitising the data from the database before evaling it though as you could allow users to execute PHP code that you don't want them to.
$data['myValue']=100;
$data['operator']="<";
$data['comparison_value']= 150;
$eval = sprintf("return(%d %s %d);", $data['myValue'], $data['operator'], $data['comparison_value']);
if(eval($eval))
{

Also you can take a look into php assert
php.net/assert
<?php
var_dump(assert("1 == 1"));
var_dump(assert("1 === null"));
?>
Sample code I used related to my project:
$assert_statement =
(($typecriteria != 'IS_NULL' || $typecriteria != 'NOT_NULL' ) ? "'".$value."'" : '' )
. " " . $typecriteria . " '" . $criteriavalue."'";
// Active assert and make it quiet
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_BAIL, 0);
assert_options(ASSERT_QUIET_EVAL, 1);
if ( $debug >= 1 ) {
print __METHOD__." assert debug ".$assert_statement."<br>";
var_dump(assert( $assert_statement ));
}
if (assert( $assert_statement ) === true )
{
return true;
}

Related

Fatal error: Cannot use isset() on the result of an expression

When coding with isset i am getting an fatal error.I have searched stackoverflow but results are not satisfactory.
I am getting
Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
My codes are
if (!isset( $size || $color )) {
$style = '';
}else{
$style = 'font-size : ' . $size . ';color:' . $color;
}
As mentioned in the comments (and the error message), you cannot pass the result of an expression to isset.
You can use multiple isset calls, or reverse the logic of your if/else block and pass multiple parameters to isset, which i think is the cleanest solution:
//true if both are set
if(isset($size, $color)) {
$style = 'font-size : ' . $size . ';color:' . $color;
}else{
$style = '';
}
You can clean this up a little further by setting the default value first, thus avoiding the need for an else section:
$style = '';
if(isset($size, $color)) {
$style = 'font-size : ' . $size . ';color:' . $color;
}
You could even use a ternary, though some people find them harder to read:
$style = isset($size, $color) ? 'font-size : ' . $size . ';color:' . $color : '';
you should use this way
if (!isset( $size ) || !isset( $color )) {
Your expression always return either true or false => In theory isset always return true so PHP not allow this
Change
if (!isset( $size || $color )) {
To
if (!isset($size) || !isset($color)) {

Change '-' to variable and use this variable to call exact value

For example:
$k = "+";
$q = 8;
echo $array[$q+1];
But I want the following:
echo $array[$q$k1];
So it basically says "call the value of array which is 8+1 so 9." and if I want to call 7 I can do $k = "-".
In PHP, you can not treat operators as variables.
Still, there two basic ways you can achieve the same effect.
You can use a conditional and specify the values accordingly:
$r = 1; //the value you're adding; moved to a variable for clarity
$op = '+'; //+ means add; anything else means subtract
echo $array[$q + ($op === '+' ? $r : -$r)];
//or
if($op === '+') {
echo $array[$q + $r];
} else {
echo $array[$q - $r];
}
Or you can change the operation into a multiplication:
echo $array[$q + (($op === '+' ? 1 : -1) * $r)];
Either form will work; it's just a matter of what is most convenient for your code.

PHP - Incorrect use of static variable?

I'm trying to make a simple code in PHP in order to decide when I can make a query.
My code looks like this :
$status = shell_exec("/usr/local/bin/gpio -g read 17");
static $status_lpv = 0;
if ($status == 1 )
{
if($status_lpv == 0)
{
$status_lpv = 1;
echo " do the job ";
}
}
if($status == 0 )
{
if($status_lpv == 1 )
{
$status_lpv = 0;
echo "do another job ";
}
}
My variable $status_lpv is always 0. What I'm doing wrong ?
Try to wrap the code into function (if not there yet) and call the function
You cannot store status between http calls in a (static) variable.
You need persistent storage, locking etc. (eg. try a database)

Wordpress QR Code Widget

Basically, I've been trying to make a simple Wordpress widget that displays a QR code with the URL of the current page. I'm using a modififed version of the simple text widget that parses PHP too.
function the_qrcode($permalink = '', $title = '') {
if($permalink && $title == '') {
$permalink = 'http://eternityofgamers.com/forums';
$title = 'Forums';
}
echo '<img src="http://api.qrserver.com/v1/create-qr-code/?data=' .$permalink. '" alt="QR: ' .$title. '"/>;
}
Can someone tell me what's wrong with this? I get a 500 error when I add it to functions.php.
You will need to use the urlencode() function. Generally as a rule of thumb all querystring values should be url encoded.
function the_qrcode( $permalink = '' ) {
if($permalink == '') {
$permalink = 'http://eternityofgamers.com/forums';
}
echo '<img src="http://api.qrserver.com/v1/create-qr-code/?data='.urlencode($permalink);
}
Now you can create your QR code with:
the_qrcode(the_permalink());
Also, you had a very bad missing equals sign. It is very important to understand the difference between = and ==. If you don't, no matter the context = and == mean two different things. = assigns the right hand side to the left. == returns true or false whether the left and right hand side are loosely equal (loosely because casting will be used if the sides are not of the same type).
Look at this example (Codepad demo):
$a = 5;
$b = 10;
if($a = 6) {
echo "This always appears because when you assign a truthy (all non-zero numbers are true) to a variable, true is returned.\n";
echo "Also a should now equal six instead of five: " . $a . "\n";
}
if($b == 10) {
echo "This will work as expected because == is a comparison not an assignment.\n";
echo "And b should still be 10: " . $b;
}
Try with:
<?php
function the_permalink( $permalink ) {
if ($permalink == '') {
echo '<img src="http://api.qrserver.com/v1/create-qr-code/?data=http://eternityofgamers.com/forums" alt="QR Code">';
} else {
echo '<img src="http://api.qrserver.com/v1/create-qr-code/?data='.$permalink;
}
}
?>
(I've corrected a bunch of syntax errors)

Boolean PHP MySQL And OR Not Search

Can anybody help, i'm trying to build a search using php that searches a text field in mysql.
I would like users to be able to enter a must have criteria, an or criteria and a not criteria, as well as being able to search for strings, so for example:
("This phrase" OR "That phrase") AND word
At present i'm using the example below to generate a search string:
$all = $row['and_search'] ;
$any = $row['or_search'] ;
$none = $row['not_search'];
if((!$all) || ($all == "")) { $all = ""; } else { $all = "$all"; }
if((!$any) || ($any == "")) { $any = ""; }
if((!$none) || ($none == "")) { $none = ""; } else { $none = "$none"; }
The above works brilliantly for only single words, but not searches such as the example above.
Any ideas how I can change achieve this?
Strip $all, $any and $none by whitespace into arrays. Than join them like
$newAll = '("' . join('" OR "', split(' ', $all)) . '")';

Categories