I want to filter websites using xml depending on whether the values are contained in it or not. I have such a script and I don't know why, but it filters well once, not, can anyone advise me something?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "Note.dtd">
<filter>
<min_szerokosc>111</min_szerokosc>
<min_wysokosc>111</min_wysokosc>
<min_glebokosc>111</min_glebokosc>
<max_szerokosc>1111</max_szerokosc>
<max_wysokosc>1111</max_wysokosc>
<max_glebokosc>1111</max_glebokosc>
<typ>power&free</typ>
<myjnia_automat>1</myjnia_automat>
<kabina_automat>1</kabina_automat>
<transport_gorny>1</transport_gorny>
<extra></extra>
</filter>
<?php
$cat=new Kategoria;
$kat=array(205,233,234,206,204,203,202);
//$kat=array(206);
foreach($_POST as $k => $v){
if($v=="on"){
$_POST[$k]=1;
}
}
foreach($kat as $a => $b){
$result=$cat->getKategoria($b);
//print_r($_POST);
foreach($result as $res => $val){
if(preg_match("/tab_[0-9]{1,2}_filter/i",$res)>0){
//print_r($val);
$xml = simplexml_load_string($val);
if($xml){
if($_POST['typ'] == $xml->typ){
if($_POST['min_szerokosc'] <= $xml->min_szerokosc &&
$_POST['min_wysokosc'] <= $xml->min_wysokosc &&
$_POST['min_glebokosc'] <= $xml->min_glebokosc &&
$_POST['max_szerokosc'] <= $xml->max_szerokosc &&
$_POST['max_wysokosc'] <= $xml->max_wysokosc &&
$_POST['max_glebokosc'] <= $xml->max_glebokosc &&
$_POST['myjnia_automat'] == $xml->myjnia_automat ||
$_POST['myjnia_reczna'] == $xml->myjnia_reczna ||
$_POST['wanna'] == $xml->wanna &&
$_POST['kabina_automat'] == $xml->kabina_automat ||
$_POST['kabina_jedno_stanowiskowa'] == $xml->kabina_jedno_stanowiskowa ||
$_POST['kabina_dwu_stanowiskowa'] == $xml->kabina_dwu_stanowiskowa &&
$_POST['transport_gorny'] == $xml->transport_gorny ||
$_POST['transport_dolny'] == $xml->transport_dolny){
print "true:{$b}:{$res} \n";
}else{
//print "false\n";
//print("{$_POST['typ']} == $xml->typ");
}
}
}
//print($res."\n");
}
}
}
?>
How do I compose an if statement to be able to refer to each of the values of the xml file ?
How can I filter this simple xml?
I found a solution to my problem - and these are weights for the searched values - thanks to this I can precisely filter the list from the xml file
$resx=0;
if($_POST['typ'] == $xml->typ){
$resx=$resx+100;
}else{
$resx=$resx-100;
}
if($_POST['min_szerokosc'] <= $xml->max_szerokosc && $_POST['min_wysokosc'] <= $xml->max_wysokosc && $_POST['min_glebokosc'] <= $xml->max_glebokosc ){
$resx=$resx+600;
}else{
$resx=$resx-600;
}
if($_POST['max_szerokosc'] <= $xml->max_szerokosc && $_POST['max_wysokosc'] <= $xml->max_wysokosc && $_POST['max_glebokosc'] <= $xml->max_glebokosc ){
$resx=$resx+5000;
}else{
$resx=$resx-5000;
}
if($_POST['myjnia_automat'] == $xml->myjnia_automat || $_POST['myjnia_reczna'] == $xml->myjnia_reczna || $_POST['wanna'] == $xml->wanna){
$resx=$resx+40000;
}else{
$resx=$resx-40000;
}
if($_POST['kabina_automat'] == $xml->kabina_automat || $_POST['kabina_jedno_stanowiskowa'] == $xml->kabina_jedno_stanowiskowa || $_POST['kabina_dwu_stanowiskowa'] == $xml->kabina_dwu_stanowiskowa){
$resx=$resx+30000;
}else{
$resx=$resx-30000;
}
if($_POST['transport_gorny'] == $xml->transport_gorny || $_POST['transport_dolny'] == $xml->transport_dolny){
$resx=$resx+200;
}else{
$resx=$resx-200;
}
//print $resx."+";
if($resx >= 75900 || $_POST['czysc']==1){
//some code;
}
Related
I'm really confused, because I have an if/else statement what works at a first integration in the system, but in a second case I must rewrite the function. My opinion is, that both statements has the same logic? Or isn't it?
Statement 1: works as intended in first integration of code, but not at the second integration (always the variable ba_geschaeftszeichen has a string lenght of zero):
if (
(isset($_POST['ba_geschaeftszeichen']) && ($kostentraeger == "sozialamt")) ||
(isset($_POST['pk_vnr']) && ($kostentraeger == "pflegekasse"))
) {
if (isset($_POST['ba_geschaeftszeichen']) && (strlen($_POST['ba_geschaeftszeichen']) > 0)) {
$ba_geschaeftszeichen = $_POST['ba_geschaeftszeichen'];
} else if (isset($_POST['pk_vnr']) && (strlen($_POST['pk_vnr']) > 0)) {
$ba_geschaeftszeichen = $_POST['pk_vnr'];
} else {
$ba_geschaeftszeichen = "";
}
} else { $ba_geschaeftszeichen = ""; }
Statement 2: only this code works at the second integration:
if (
(isset($_POST['ba_geschaeftszeichen']) && ($kostentraeger == "sozialamt")) ||
(isset($_POST['pk_vnr']) && ($kostentraeger == "pflegekasse"))
) {
if (isset($_POST['ba_geschaeftszeichen']) && (strlen($_POST['ba_geschaeftszeichen']) > 0)) {
$ba_geschaeftszeichen = $_POST['ba_geschaeftszeichen'];
} else {
$ba_geschaeftszeichen = "";
}
if (isset($_POST['pk_vnr']) && (strlen($_POST['pk_vnr']) > 0)) {
$ba_geschaeftszeichen = $_POST['pk_vnr'];
} else {
$ba_geschaeftszeichen = "";
}
} else { $ba_geschaeftszeichen = ""; }
In statement 1, you reach
if (isset($_POST['pk_vnr']) && (strlen($_POST['pk_vnr']) > 0))
only if
if (isset($_POST['ba_geschaeftszeichen']) && (strlen($_POST['ba_geschaeftszeichen']) > 0))
is false.
In statement 2, you reach
if (isset($_POST['pk_vnr']) && (strlen($_POST['pk_vnr']) > 0))
regardless.
I trying to create a function that matches the first number in a string to a state. For example if the user inputs a number that starts with 3, and the state 'vic', then the form should not present any errors. However no matter what is entered the function always returns true. Any help would be appreciated.
$state = array('Please Select', 'VIC', 'NSW', 'QLD', 'NT', 'WA', 'SA', 'TAS', 'ACT'); //In the form this is a drop down menu
$selected_key = $_POST['state'];
$postcode = $_POST["postcode"]; //The full number entered by the user
$errMsg .= validatePS($postcode, $selected_key);
function validatePS($ps, $state) {
$errMsg ="";
$digit = $ps[0]; //Takes the first number from full postcode
$valid = false;
if (($digit == 3) or ($digit == 8) && ($state == 'vic'))
{
$post = true;
}
if (($digit == 1) or ($digit == 2) && ($state == 'nsw'))
{
$post = true;
}
if (($digit == 4) or ($digit == 9) && ($state == 'qld'))
{
$post = true;
}
if ($valid == false) {
$errMsg .= "<p>Match the correct postcode to state</p>";
}
return $errMsg;
}
if ($errMsg !=""){
echo "<p>Please correct the following errors...</p>"; //Prints out errors
echo "<p>$errMsg</p>";
}
You have two "flag" variables - $post which you are updating and $valid which you rely on. If you reduce them to one variable, you should get the behavior you want:
function validatePS($ps, $state) {
$errMsg ="";
$digit = $ps[0]; //Takes the first number from full postcode
$valid = false;
if (($digit == 3) or ($digit == 8) && ($state == 'vic'))
{
$valid = true;
}
if (($digit == 1) or ($digit == 2) && ($state == 'nsw'))
{
$valid = true;
}
if (($digit == 4) or ($digit == 9) && ($state == 'qld'))
{
$valid = true;
}
if ($valid == false) {
$errMsg .= "<p>Match the correct postcode to state</p>";
}
return $errMsg;
}
Note that you could clean up this code considerably by using logical operators instead of multiple if statements:
function validatePS($ps, $state) {
$errMsg ="";
$digit = $ps[0]; //Takes the first number from full postcode
$valid = false;
if ((($digit == 3) or ($digit == 8) && ($state == 'vic')) ||
(($digit == 1) or ($digit == 2) && ($state == 'nsw')) ||
(($digit == 4) or ($digit == 9) && ($state == 'qld'))) {
$errMsg .= "<p>Match the correct postcode to state</p>";
}
return $errMsg;
}
I am trying to add a second condition to existing code but it doesn't seem to be working.
The conditions are:
Compare two strings, from different arrays (working)
And check the value of a third string from a different array (not
working)
Here is the working code without the second condition: http://pastebin.com/bfpNb9zw
Here is my attempt:
Basically, the bit I am trying to get working is this part && ($ca = '') && ($ca = '0') && ($ca = '1') but it seems $ca is not able to be read outside the loop
if(!function_exists('lookup')){
function lookup($chain, $type) {
$cacount = count($chain['tbsCertificate']['extensions']);
for($j = 0; $j < $cacount; $j++) {
$count = count($chain['tbsCertificate'][$type]['rdnSequence']);
$exists = array('utf8String', 'printableString', 'teletexString', 'bmpString', 'universalString', 'ia5String');
$oid = array('id-at-commonName');
for($i = 0; $i < $count; $i++) {
foreach($exists as $field) {
if(
array_key_exists($field, $chain['tbsCertificate'][$type]['rdnSequence'][$i][0]['value']) &&
in_array($chain['tbsCertificate'][$type]['rdnSequence'][$i][0]['type'], $oid)
) {
$value = $chain['tbsCertificate'][$type]['rdnSequence'][$i][0]['value'][$field];
return $value;
$ca = '';
if(isset($chain['tbsCertificate']['extensions'][$j]['extnValue']['cA'])) {
$ca = $chain['tbsCertificate']['extensions'][$j]['extnValue']['cA'];
}
}
}
}
}
return null;
}
}
if (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca == '')) {
echo 'end entity';
}
elseif (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca == '0')) {
echo 'secondary ca';
}
elseif (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca == '1')) {
echo 'primary ca';
} else {
echo 'Root';
}
You are using =, which sets the value of $ca. You should be using === to check the value, instead.
Example:
if (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca === '')) {
echo 'end entity';
}
elseif (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca === '0')) {
echo 'secondary ca';
}
elseif (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca === '1')) {
echo 'primary ca';
} else {
echo 'Root';
}
I have the following code:
if ($Type != "DEA" and $VA != "Allowed" and $VolSess != 1) {
$max_rows = max($CMSReg_num_rows);
if ($max_rows == 0) {
mail($to, $subject, $body);
header('Location: '.bloginfo('home_url').'/profile');
}
}
The problem I have is that that an email is sent despite the if-statement being false, and only an email is sent. The rest of the code is not executed, i.e. no redirect. And when I comment out the mail() function, it does not send the email.
And when I add this code:
if ($VA == "Allowed") {
echo "VA = " . $VA;
}
if ($VolSess == 1) {
echo "VolSess = " . $VolSess;
}
I get this output:
VA = Allowed VolSess = 1
So I know that the condition in the if statement is false.
AND has a different order of precedence compared to &&. So your expression does not evaluate as you expect it to.
("$Type" != "DEA" and $VA != "Allowed" and $VolSess != 1)
should be
(("$Type" != "DEA") and ($VA != "Allowed") and ($VolSess != 1))
or
("$Type" != "DEA" && $VA != "Allowed" && $VolSess != 1)
for it to work as you expect it. This is one of those tiny mistakes/bugs that's easy to overlook.
try do an else after...
elseif($VA == "Allowed"){}
Try using the WordPress wp_mail().
die; after header() and also add 302 as a second argument to the header() function.
Enable error reporting with ini_set('display_errors', true); error_reporting(-1); on top of your PHP code.
Tell us what you see after making these changes.
Try:
if ($Type != 'DEA' && $VA != 'Allowed' && $VolSess != 1)
{
$max_rows = max($CMSReg_num_rows);
if ($max_rows === 0)
{
mail($to, $subject, $body);
header('Location: ' . bloginfo('home_url') . '/profile');
}
}
EDIT
The above works, but so does the oringal question code... The problem is elsewhere.
<?php
$Type = 'foo';
$VA = 'Allowed';
$VolSess = 1;
if ($Type != 'DEA' and $VA != 'Allowed' and $VolSess != 1)
{
$max_rows = 0;
if ($max_rows === 0)
{
echo 'Orig True';
}
}
else
{
echo 'fine?';
}
if ($Type != 'DEA' && $VA != 'Allowed' && $VolSess != 1)
{
$max_rows = 0;
if ($max_rows === 0)
{
echo 'Second True';
}
}
else
{
echo 'fine?';
}
?>
Both print 'fine?' Implying your error is elsewhere in your code.
I have this if statment here and its not working
if(
($division->id == 1 || $division->id == 2) &&
in_array('member1', $memberships) ||
in_array('member2', $memberships) ||
in_array('member3', $memberships) ||
in_array('member4', $memberships) ||
($division->id != 1 || $division->id != 2)
&& in_array('member5', $memberships))
{
return FALSE;
} else {
return TRUE;
}
What I am trying to do is say if $division is 1 or 2 and if member1, member2, member3, member4 are in the array $memberships return false, if $division is not 1 or 2 and member5 is in the array return false, everything else return true.
This is not working because member5 is in the array and $division is 1, which should return true, but it returns false.
PS - member1-5 are just names I am using for here as they actually are personal information in my array.
What Am i doing wrong?
I'd do this as a series of different 'if' statements, to avoid you going around the bend trying to work it out.
I've created this piece of code according to your statement...
What I am trying to do is say if $division is 1 or 2 and if member1,
member2, member3, member4 are in the array $memberships return false,
if $division is not 1 or 2 and member5 is in the array return false,
everything else return true.
I think I've got the logic right, I'm about to double check it:
if ($division->id == 1 || $division->id == 2)
{
if (in_array("member_2", $memberships) || in_array("member_3", $memberships) || in_array('member_3', $memberships) || in_array('member_4', $memberships))
{
return false;
}
else
{
return true;
}
}
else
{
if (in_array("member_5", $memberships))
{
return false;
}
else
{
return true;
}
}
These are the logical groupings, 1 per line. Use parenthesis to adjust how php evaluates it if this isnt what you want.
($division->id == 1 || $division->id == 2) && in_array('member1', $memberships)
|| in_array('member2', $memberships)
|| in_array('member3', $memberships)
|| in_array('member4', $memberships)
|| ($division->id != 1 || $division->id != 2) && in_array('member5', $memberships))
Try:
if(
(($division->id == 1 || $division->id == 2) &&
(in_array('member1', $memberships) ||
in_array('member2', $memberships) ||
in_array('member3', $memberships) ||
in_array('member4', $memberships))) ||
(($division->id != 1 || $division->id != 2)
&& in_array('member5', $memberships)))
{
return FALSE;
} else {
return TRUE;
}
Less lines of code does not always mean it's better, break it down and make it readable, for example:
$output = true;
if($division->id == 1 || $division->id == 2)
{
if(in_array("member_2", $memberships) || in_array("member_3", $memberships) || in_array('member_4', $memberships))
{
$output = false;
}
}
else
{
if (in_array("member_5", $memberships))
{
$output = false;
}
}
return $output;