variable sphinx filter with PHP - php

i m sorry for my english, i m french, i need your help for one thing please :
It s about filtering in php sphinx
this my code :
$filtres= array();
if(isset($_POST['Pharmacie']) and $_POST['Pharmacie'] ="1" ){ $filtres[]= 1;}
if(isset($_POST['Autres']) and $_POST['Autres'] ="8" ){ $filtres[] = 8;}
$varfiltres = 'array('.implode(" , ",$filtres).')';
if($filtres != array()){
$sphinx->SetFilter('Type', array(implode(",",$filtres)));
}
i have error :
Warning: assert(): Assertion failed in \sphinxapi.php on line 850
in case only one variable isset (pharmacie or Autres) that work!
also if i do $sphinx->SetFilter('Type', array(1,8)) it work!
thanks for help.

SetFilter, takes an array directly. With:
$sphinx->SetFilter('Type', array(implode(",",$filtres)));
you are first converting the Array to a String, and then putting it in an new array. Don't do that :)
This is all that is needed:
$sphinx->SetFilter('Type', $filtres);

Related

Problem on check condition on laravel php

I have a problem on my laravel code. I want to check condition and show up using dd my code here.
$ca = floatval($sw->terminal_ca); // $sw->terminal_ca from postgresql type number(8,6) = 0.003480
$max_ca = $this->terminal_ca*(1+($this->tolerance/100)); // $this->terminal_ca from postgresql type number(16,4) = 0.0029
// $this->tolerance from postgresql type number(16,4) = 20.0000
dd(
$ca,
$max_ca,
($ca <= $max_ca));
but my result here
not acceptable.
it should be true.
What's wrong on my code?
How can I solve it?
I have given static values in the controller and it's working fine.
Kindly check your type in the database from where you are fetching data dynamically.
I round it before comparison is ok now. Thank #El_Vanja
$ca = round($ca, 6);
$min_ca = round($min_ca, 6);

Plugin preg_match_all Error after Updating from very oldto PHP 7.4 [duplicate]

This question already has an answer here:
PCRE2 Regex error escape sequence is invalid in character class
(1 answer)
Closed 2 years ago.
Our website with error turned on (plugin activated):
Example URLs with error:
https://www.live-karikaturen.ch/downloads/1-august-fahnen-schwingen-nationalfeiertag-schweiz/
https://www.live-karikaturen.ch/webshop-gratisbilder-free-cartoon-comic-images/
https://www.live-karikaturen.ch/downloads/auto-suv-autofan-abgase-umweltverschmutzung/
UPDATE INCOMPATIBILITY: I updated from a very old PHP version to 7.4
I use this EXTREMLY important plugin, which is horrible outdated (4 years last Update):
https://wordpress.org/support/plugin/creative-commons-configurator-1/
Now I get this error and I think it's maybe very easy to solve, but I know almost nothing about PHP, but I try to learn it a bit, so thank you very much in advance.
ERROR:
*
Warning: preg_match_all(): Compilation failed: escape sequence is
invalid in character class at offset 18 in
/home/clients/f9abb707fe4ac1215fe0f0a9c0b0ae21/www.live-karikaturen-ch/wp-content/plugins/creative-commons-configurator-1/creative-commons-configurator-1.php
on line 821
I copy the code from the .php file here
CODE:This is line 821:
if ( ! preg_match_all( $pattern_images_no_caption, $post_content, $matches ) ) {
return $post_content;
}
CODE From line 803 - 828 (don't know who to copy the line numbers):
function bccl_separate_licensing_on_images_without_caption( $post_content ) {
// Plugin options
$options = get_option('cc_settings');
if ( $options === false || $options['cc_enable_individual_media_licensing'] == '0' ) {
return $post_content;
}
$post = get_queried_object();
if ( apply_filters('bccl_exclude_license', false, $post) ) {
return $post_content;
}
// Pattern that matches images without caption
//$pattern_images_no_caption = '#<p>[\s\R]*(<img [^>]+>)#';
$pattern_images_no_caption = '#<(?:p|h[\d]+)>[\s\R]*(<img [^>]+>)#';
$pattern_images_no_caption = apply_filters('bccl_pattern_match_images_without_caption' , $pattern_images_no_caption);
**if ( ! preg_match_all( $pattern_images_no_caption, $post_content, $matches ) ) {
return $post_content;
}**
//var_dump($matches[1]);
// Iterate over the found images and add licensing metadata
foreach ( $matches[1] as $image_html ) {
Is there a simple way how to fix this error?
THANK YOU VERY MUCH FOR YOUR ANSWER & HELP!
THAAAANK you very much, you are AMAZING! :) I tried it out and it works, at least there is no error anymore.
FIRST
I am just not sure which version of the corrected code I should take. I guess Nr. 1 is ok, without []. That mean, as Toto wrote above I replaced [\s\R] with a simple \s.
Nr. 1.) without []
//$pattern_images_no_caption = '#\s*(<img [^>]+>)#';
$pattern_images_no_caption = '#<(?:p|h[\d]+)>\s*(<img [^>]+>)#';
Nr. 2.) with []
//$pattern_images_no_caption = '#<p>[\s]*(<img [^>]+>)#';
$pattern_images_no_caption = '#<(?:p|h[\d]+)>[\s]*(<img [^>]+>)#';
SECOND
I searched the whole creative-commons-configurator.php plugin code for \R and found further in line 979 this:
$parts = preg_split('#\R#u', $attach`enter code here`ment_data);
All lines together of this part are this:
// Attachment ID
// Unfortunately, WP does not provide enough information in order to determine the attachment ID
// So, first collect all the audio/video media file URLs in $attachments_urls
$attachments_data = get_post_meta( $post->ID, 'enclosure', false );
//var_dump($attachments_data);
$attachments_urls = array();
foreach ( $attachments_data as $attachment_data ) {
$parts = preg_split('#\R#u', $attachment_data);
$attachments_urls[] = $parts[0];
}
//var_dump($attachments_urls);
// Then check which media file URL exists in the $atts array so as to
// determine which attachment we are processing currently.
$atts_values = array_values($atts);
//var_dump($atts_values);
// Find the URL of the attachment we are processing
$current_attachment_url = '';
foreach ( $attachments_urls as $attachment_url) {
if ( in_array($attachment_url, $atts_values) ) {
$current_attachment_url = $attachment_url;
break;
}
}
QUESTION: In the moment I don't get any error, but I wonder if this will produce one and I have to delete or replace the "evil" \R?

Why does my code produce a notice about an undefined offset?

I have this notice:
Notice: Undefined offset: 1 in D:\serveurs\data\localweb\alexa
traffic.php on line 14
the line 14 is this:
$usrank = ($rankus[1]) ? $rankus[1] : 0;
How do I fix this?
Here is my code:
<?php
$source = file_get_contents('http://data.alexa.com/data?cli=10&dat=snbamz&url=linuxplained.com');
//Alexa Rank
preg_match('/\<popularity url\="(.*?)" text\="([0-9]+)" source\="panel"\/\>/si', $source, $matches);
$aresult = ($matches[2]) ? $matches[2] : 0;
//Alexa Sites Linking in
preg_match('/\<linksin num\="([0-9]+)"\/\>/si', $source, $asli);
$alinksin = ($asli[1]) ? $asli[1] : 0;
//Alexa US Rank
preg_match('/\<country code\="US" name\="United States" rank\="([0-9]+)"\/\>/si', $source, $rankus);
$usrank = ($rankus[1]) ? $rankus[1] : 0;
//Alexa Reach Rank
preg_match('/\<reach rank\="([0-9]+)"\/\>/si', $source, $reachr);
$areach = ($reachr[1]) ? $reachr[1] : 0;
?>
Use isset() to check to see if that value exists:
$usrank = (isset($rankus[1])) ? $rankus[1] : 0;
This error occurs because the array $rankus doesn't have a value at index 1. The easiest fix is by using isset to check whether the index exists before attempting to use it. So then your could would be:
$usrank = (isset($rankus[1])) ? $rankus[1] : 0;
This uses the ternary operator, which is equivalent to the following (easier to understand) code:
$usrank;
if (isset($rankus[1]) {
$usrank = $rankus[1];
} else {
$usrank = 0;
}
Hopefully you now understand why the problem occurs and how to fix this.
There are more problems in your code though. When you create the variables $aresult, $alinksin and $areach, you don't check whether the needed indexes exist either. You should probably do this in order to avoid more errors like the one you get at the moment.
Finally, I noticed you are trying to parse an XML document using regular expressions. That can go wrong in a lot of ways, and it's better to use a 'real' XML parser. Take a look at SimpleXML or one of the other XML libraries for PHP!

How can i solve this error: llegal string offset 'text'?

I have this code :
if(strlen($userdata->yim['text']) > 2 && !isset($_POST['step1']) ){
$GLOBALS['error'] = 1;
$GLOBALS['error_type'] = "tip";
$GLOBALS['error_msg'] = $userdata->yim['text'];}
I read about this error on this site but i have no ideea how to apply the fix on my particular code. I`m sorry if i repost the problem.
Try doing: var_dump($userdata->yim); to verify if yim does in fact exist and contains the key 'text'.
Or even just var_dump($userdata);
$userdata->yim['text'] is not set, so you should check for that first:
if(isset($userdata->yim['text']) && strlen($userdata->yim['text']) > 2 && !isset($_POST['step1']) ){
However, if that section of code relies on that value, something is wrong before that and this will just hide that problem.
In order to debug this you will need to utilize print_r() so that you can see the contents of your object or array:
// see what $userdata contains - update your question with the results of the line below
echo '<div style="background-color:white; padding:15px;"><pre>'.print_r($userdata, true).'</pre></div>';
// see what $userdata->yim contains - update your question with the results of the line below as well
echo '<div style="background-color:white; padding:15px;"><pre>'.print_r($userdata->yim, true).'</pre></div>';
if(strlen($userdata->yim['text']) > 2 && !isset($_POST['step1']))
{
$GLOBALS['error'] = 1;
$GLOBALS['error_type'] = "tip";
$GLOBALS['error_msg'] = $userdata->yim['text'];
}

String comparison fails because of value passed by datatable, codeigniter

I am facing a really strange problem which i am debugging from past 2 hours but unable to find the solution. Before explaining the problem, let me show the code
My Controller Function is
$this->load->library('datatables');
$actionLinkBar = $this->load->view("content/updates/dt_files/action_bar", array(), TRUE);
$this->datatables
->select("id, name, status")
->where('id', $this->session_data['user_id'])
->from("t_user")
->add_column("action", $actionLinkBar, 'id, name, status');
echo $this->datatables->generate();
And the code in my action_bar view is
<?php
$status_rec = '$3';
var_dump($status_rec); // STRANGE OUTPUT - string(2) "1"
?>
<div class="action_bar" data-update-id="<?php echo '$1'; ?>">
<?php if ($status_rec == '1') { ?> // HENCE COMPARISON ALWAYS FAILS
<span>Present</span>
<?php }else { ?>
<span>Absent</span>
<?php } ?>
</div>
Now explaining the problem.. I am using Datatables with Codeigniter. I have a view template action_bar which will be displayed in one of the columns of datable in front end. The view has if/else condition based on value of status field from DB. If status feild value = 1 = Present. Else it is Absent. But though the $status_rec has value as '1' it still fails in comparison. Strange thing is on var_dumping $status_rec, i found that though it has proper value, the length is weird(2) though its single int. I even tried trimming etc but still no effect. Maybe that's why the comparison is failing. Your help is really needed :/
P.S - The DB feild that holds this value is int with length 1
I also had a similar problem solved it a bit by changing the library:
Datatables.php with if condition
changes in the 194, 196 and 440 strings
How to use:
$if = array('0' => array('if_condition'=>'$3', 'if_condition_eqv'=>'1', 'if_true'=>'<span>Present</span></div>', 'if_false'=>'<span>Absent</span></div>'));
$this->datatables->add_column("action", '<div class="action_bar" data-update-id="$1">', 'id, name, status');
BUT you have to change: ".=" On "=" (strings 497 and 504)
Should work.
I would really appreciate if someone will correct code
P.S. Sorry for my english

Categories