JSONPath PHP gives false - php

I've been using JSONPath in my Codeigniter 3 Project.
I want to find the result of an expression.
Now, coming to the issue. The Same expression works on an online json evaluator. But the same expression used during the code results into false.
Here's the expression
$.slots[?((('2035-05-16 08:00' >= #.slot_datetime_from && '2035-05-16 08:00' < #.slot_datetime_to) || ('2035-05-16 12:30' > #.slot_datetime_from && '2035-05-16 12:30' <= #.slot_datetime_to) ) && #.venue_id == '1' && #.court_id == '1')]
I'm using one JSON. Here's the link
Here's the code which I'm using.
$existingEntries = new JsonObject($existingSlotAndCourtEntries);
$result = $existingEntries->{$exp};

Here's the answer. The author of the library answered it himself.
Github link

Related

String to int in PHP [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 4 years ago.
I wrote a program in python in my raspberry pi which use the gpio 17. My goal is to read the status of this gpio separably to this program, to run a "if" and to display the result in a local website. For this, I use apache2 and PHP (version 7), I'm beginner in this language. This is the program I use :
<?php
$read = shell-exec ('gpio read 0');
$status = intval($read);
if ($status = 1) {
print ("oui");
}
else {
print ("non");
}
?>
This program don't work because if I understand, the value of $read I obtain is a string and I need an Int to use it in my "If". For it, I tried to change this String to an Int thanks to the function intval() (like you can see in the program in the top) but it didn't work. I tried to use also the ord() and the (int) function. The result is always the same. It display "oui".
Is my problem come from the intval() function or is it maybe come from shell-exec() ?
Thanks for your help ;)
I tried to be the clearest possible in my explanation
You have an error at this line, since = is not a comparison operator:
if ($status = 1) {
It should be:
if ($status == 1) {
If you also want to check that 1 and $status are of the same type, use the operator === instead. Here is the PHP documentation for comparison operator.
You will need to use a comparison operator. I have modified your if condition to compare if the values are identical.
<?php
$read = shell-exec ('gpio read 0');
$status = intval($read);
if ($status === 1) { //Use === to check if they are the same type and value
print ("oui");
}
else {
print ("non");
}
?>

PHP Laravel get millions record using MYSQL

I have been using laravel + mysql for my project but and it was working perfectly fine until now. The records keep on increasing and now they have reached almost a million record. Problem is when i try to fetch sms from my database using this query
$smsHistory = SmsLog::where('created_at', '>=', $startDate)->where('created_at', '<=', $endDate)->whereNotNull('gateway_statuscode')->get();
It gives a 500 error without showing anything in error log. What i assume is that as i decreased the time period it gives me record so the problem is the bulk it can not handle. What could be the possible solution as i have to give it today.
I am not worried about the error log.. i want to get a count of those million record but i want to apply some algorithm on it before doing it
This is the check which i have to perform afterwards to see how many sms are in sms log
foreach ($smsHistory as $sms) {
$sms_content = SmsService::_getSmsContent($sms);
if ($sms_content->business_id && array_key_exists($sms_content->business_id, $smsCredits )) {
if (floor(strlen($sms_content->content) / 160) == 0) {
$smsCredits[$sms_content->business_id]['count'] += 1;
}
if (floor(strlen($sms_content->content) / 160) == 1 && floor(strlen($sms_content->content) / 306) == 0) {
$smsCredits[$sms_content->business_id]['count'] += 2;
}
if (floor(strlen($sms_content->content) / 306) == 1 && floor(strlen($sms_content->content) / 459) == 0) {
$smsCredits[$sms_content->business_id]['count'] += 3;
}
if (floor(strlen($sms_content->content) / 459) == 1 && floor(strlen($sms_content->content) / 621) == 0) {
$smsCredits[$sms_content->business_id]['count'] += 4;
}
if (floor(strlen($sms_content->content) / 621) == 1 && floor(strlen($sms_content->content) / 774) == 0) {
$smsCredits[$sms_content->business_id]['count'] += 5;
}
if (floor(strlen($sms_content->content) / 774) == 1 && floor(strlen($sms_content->content) / 927) == 0) {
$smsCredits[$sms_content->business_id]['count'] += 6;
}
}
this is the database field
content is the sms that i have to get and count
Regarding the 500 error:
If you are getting a 500 error, there should hopefully be some clue in the actual server error log (your laravel application error log may not have caught it depending on the error handlers, etc and what the cause was). php_info() should show you the location of the physical error log with the error_log setting:
<?php phpinfo(); ?>
If I had to guess, possibly something memory related that is causing it to choke. But that is just a guess.
As a side question, why are you trying to retrieve so many at once? Can you split them up somehow?
Edit based on updated question:
You may need to use some raw-expressions here to get what you really want: https://www.laravel.com/docs/4.2/queries#raw-expressions
MySQL for example provides the ability to get a column length:
FLOOR(CHAR_LENGTH(content) / 160) as len1, FLOOR(CHAR_LENGTH(content) / 306) as len2, FLOOR(CHAR_LENGTH(content) / 459) as len3
So probably with some magic we could take your query and let the database system do it all for you. And there are probably more efficient ways to do it, if I knew more about the significance of those numbers, but I am trying to help you at least get on one possible path.
You should setup index for mysql, or implement a search engine like elastic search

Check that an array includes at least a set of values in PHP

How can I check that the $courseAreas array includes at least "ayrshire"
and "fife"?
I've tried the following code, but it does not work:
$courseAreas = array('ayrshire', 'fife', 'cheshire', 'lanarkshire');
$includesAyrshireAndFife = (count(array_intersect(array('ayrshire', 'fife'), $courseAreas)) >= 2 ? true : false);
Try putting the ? true : false outside the braces
$courseAreas = array('ayrshire', 'fife', 'cheshire', 'lanarkshire');
$includesAyrshireAndFife = (count(array_intersect(array('ayrshire', 'fife'), $courseAreas)) >= 2) ? true : false;
var_dump($includesAyrshireAndFife);
$courseAreas = array('ayrshire', 'stirlingshire', 'cheshire', 'lanarkshire');
$includesAyrshireAndFife = (count(array_intersect(array('ayrshire', 'fife'), $courseAreas)) >= 2) ? true : false;
var_dump($includesAyrshireAndFife);
Seems to work
But your original also seems to work perfectly well.... in what circumstances do you find that it fails?
$courseAreas = array('ayrshire', 'fife', 'cheshire', 'lanarkshire');
$includesAyrshireAndFife = count(array_intersect(array('ayrshire', 'fife'), $courseAreas)) > 1;
You don't even need tenary operator because with > it's already boolean expression.
Edit:
I've noticed that your code works too. I've just shorten it.
You can use in_array()
See :- http://www.w3schools.com/php/func_array_in_array.asp

WP-Polls set IP limit at 2

I'm using wp-polls and need to limit the number of votes per IP. Although this is usually set at 1 by default, I need to limit the number of votes per IP to 2.
I've been reading documentation, playing with the plugin code, and looking around on google and SO and can't seem to find the proper method.
I've considered using cookies, but these are harder to catch since I could just as well empty my cookies and voilĂ , it's done.
I need a limit because I don't want people to vote endlessly
Looking at the database, I imagine this has something to do with the pollip_ip field (from the (prefix)_pollsip) table, but not being familiar with editing WP Plugins, this is as far I as I got to go.
As a reference, here is some failed code
wp-polls.php, line 1323
// original code
// if($check_voted == 0) {
// proposed by #birgire
if( $check_voted == 0 || ( is_array( $check_voted ) && 2 >= count( $check_voted ) ) ) {
wp-polls.php, line 1323
// original code
// if($check_voted == 0) {
// alternative of code proposed by #birgire
if( ($check_voted == 0) || count($check_voted) <= 2)
I couldn't see any available filters for this, when I skimmed through the plugin source.
I wonder if it would work, if you replace line #1323 of the wp-polls.php file:
if($check_voted == 0) {
with:
if( $check_voted == 0
|| ( is_array( $check_voted ) && 2 >= count( $check_voted ) ) ) {
to limit the number of votes per IP to 2.
Additionally:
Replace line #140 with:
if( !is_array($check_voted) && intval($check_voted) > 0
|| (is_array($check_voted) && sizeof($check_voted) > 1)
|| ($poll_active == 0 && $poll_close == 1)) {
But I don't recommend in general to modify plugin files, since it will be restored in the next plugin update.
Sidenote: The plugin is not using the recommended $wpdb->prepare() when preparing the SQL for $wpdb->query().

PHP Constant string parameters token

In a system we will be using, there is a function called "uses". If you are familiar with pascal, the uses clause is where you tell your program what dependencies it has (similar to C and PHP includes).
This function is being used in order to further control file inclusion other than include(_once) or require(_once).
As part of testing procedures, I need to write a dependency visualization tool for statically loaded files.
Statically Loaded Example: uses('core/core.php','core/security.php');
Dynamically Loaded Example: uses('exts/database.'.$driver.'.php');
I need to filter out dynamic load cases because the code is tested statically, not while running.
This is the code I'm using at this time:
$inuses=false; // whether currently in uses function or not
$uses=array(); // holds dependencies (line=>file)
$tknbuf=array(); // last token
foreach(token_get_all(file_get_contents($file)) as $token){
// detect uses function
if(!$inuses && is_array($token) && $token[0]==T_STRING && $token[1]=='uses')$inuses=true;
// detect uses argument (dependency file)
if($inuses && is_array($token) && $token[0]==T_CONSTANT_ENCAPSED_STRING)$tknbuf=$token;
// detect the end of uses function
if($inuses && is_string($token) && $token==')'){
$inuses=false;
isset($uses[$tknbuf[2]])
? $uses[$tknbuf[2]][]=$tknbuf[1]
: $uses[$tknbuf[2]]=array($tknbuf[1]);
}
// a new argument (dependency) is found
if($inuses && is_string($token) && $token==',')
isset($uses[$tknbuf[2]])
? $uses[$tknbuf[2]][]=$tknbuf[1]
: $uses[$tknbuf[2]]=array($tknbuf[1]);
}
Note: It may help to know that I'm using a state engine to detect the arguments.
My issue? Since there are all sorts of arguments that can go in the function, it is very difficult getting it right.
Maybe I'm not using the right approach, however, I'm pretty sure using token_get_all is the best in this case. So maybe the issue is my state engine which really isn't that good.
I might be missing the easy way out, thought I'd get some peer review off it.
Edit: I took the approach of explaining what I'm doing this time, but not exactly what I want.
Put in simple words, I need to get an array of the arguments being passed to a function named "uses". The thing is I'm a bit specific about the arguments; I only need an array of straight strings, no dynamic code at all (constants, variables, function calls...).
Using regular expressions:
<?php
preg_match_all('/uses\s*\((.+)\s*\)/',
file_get_contents('uses.php'), $matches, PREG_SET_ORDER);
foreach ($matches as $set) {
list($full, $match) = $set;
echo "$full\n";
// try to remove function arguments
$new = $match;
do {
$match = $new;
$new = preg_replace('/\([^()]*\)/', '', $match);
} while ($new != $match);
// iterate over each of the uses() args
foreach (explode(',', $match) as $arg) {
$arg = trim($arg);
if (($arg[0] == "'" || $arg[0] == '"') && substr($arg,-1) == $arg[0])
echo " ".substr($arg,1,-1)."\n";
}
}
?>
Running against:
uses('bar.php', 'test.php', $foo->bar());
uses(bar('test.php'), 'file.php');
uses(bar(foo('a','b','c')), zed());
Yields:
uses('bar.php', 'test.php', $foo->bar())
bar.php
test.php
uses(bar('test.php'), 'file.php')
file.php
uses(bar(foo('a','b','c')), zed())
Obviously it has limitations and assumptions, but if you know how the code is called, it could be sufficient.
OK I got it working. Just some minor fixes to the state engine. In short, argument tokens are buffered instead of put in the uses array directly. Next, at each ',' or ')' I check if the token is valid or not and add it to the uses array.
$inuses=false; // whether currently in uses function or not
$uses=array(); // holds dependencies (line=>file)
$tknbuf=array(); // last token
$tknbad=false; // whether last token is good or not
foreach(token_get_all(file_get_contents($file)) as $token){
// detect uses function
if(!$inuses && is_array($token) && $token[0]==T_STRING && $token[1]=='uses')$inuses=true;
// token found, put it in buffer
if($inuses && is_array($token) && $token[0]==T_CONSTANT_ENCAPSED_STRING)$tknbuf=$token;
// end-of-function found check buffer and throw into $uses
if($inuses && is_string($token) && $token==')'){
$inuses=false;
if(count($tknbuf)==3 && !$tknbad)isset($GLOBALS['uses'][$file][$tknbuf[2]])
? $GLOBALS['uses'][$file][$tknbuf[2]][]=$tknbuf[1]
: $GLOBALS['uses'][$file][$tknbuf[2]]=array($tknbuf[1]);
$tknbuf=array(); $tknbad=false;
}
// end-of-argument check token and add to $uses
if($inuses && is_string($token) && $token==','){
if(count($tknbuf)==3 && !$tknbad)isset($GLOBALS['uses'][$file][$tknbuf[2]])
? $GLOBALS['uses'][$file][$tknbuf[2]][]=$tknbuf[1]
: $GLOBALS['uses'][$file][$tknbuf[2]]=array($tknbuf[1]);
$tknbuf=array(); $tknbad=false;
}
// if current token is not an a simple string, flag all tokens as bad
if($inuses && is_array($token) && $token[0]!=T_CONSTANT_ENCAPSED_STRING)$tknbad=true;
}
Edit: Actually it is still faulty (a different issue though). But the new idea I've had ought to work out nicely.

Categories