I am setting some vars from GET
$start = $_GET['start'];
$end = $_GET['end'];
From which I get:
start=1-11-2018&end=30-11-2018
And then I am doing:
if((!$start) && (!$end)) {
if (($dateFormat >= $start) && ($dateFormat <= $end)) {
} else {
echo "no dates";
}
And to close it
if((!$start) && (!$end)) {
}
}
But this isn't happening
if((!$start) && (!$end)) {
UPDATE
Now this is working but it doesn't go in else if no GET
if((!empty($_GET['start'])) && (!empty($_GET['end']))) {
if (($dateFormat >= $start) && ($dateFormat <= $end)) {
} else {
echo "No dates";
}
Check via isset()
if you are calling : http://example.com?start=1-11-2018&end=30-11-2018
1. The isset() is checking query string "start"/"end" is having or not.
2. The empty() is checking query string "start"/"end" is empty/blank or not
if( isset($_GET['start']) && isset($_GET['end']) ){ // check the GET method is set or not
if((!empty($_GET['start'])) && (!empty($_GET['end']))) {
if (($dateFormat >= $start) && ($dateFormat <= $end)) {
}
else {
echo "Empty dates";
}
}
else{
echo "Start / End date query string is missing...";
}
This is how I resolved it:
if((!empty($start)) && (!empty($end))) {
if (($dateFormat >= $start) && ($dateFormat <= $end)) {
}
// here the content which
// in case those vars are not empty,
// would get filtered by that logic.
if((empty($start)) && (empty($end))) {
// Close the condition for second if when not empty
} else {
echo "No dates";
}
Related
I have a for loop to cycle through and array, run a database query in relation to each element, then call a function that prints out something in relation to it. The array is 12 elements long but the for loop never gets past element 0. It doesn't error or fail it just doesn't do anything after the first element. I verified that by putting the echo $x; and echo $vendorsname[$x]; at the start of each loop cycle and sure enough it only ever echo's 0 out to the page.
$continuetill = count($vendorsname);
for ($x = 0; $x < $continuetill; $x++)
{
echo $x;
echo $vendorsname[$x];
$sql="SELECT low,mid,high,verlow,vermin,verhigh FROM vendors WHERE vendor = ".$x." ORDER BY id DESC LIMIT 1";
if ($result=mysqli_query($conn,$sql))
{
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
$low = $row[0];
$mid = $row[1];
$high = $row[2];
$verlow = $row[3];
$vermid = $row[4];
$verhigh = $row[5];
if(($low > $mid) && ($low > $high))
{
likely295Message($vendorsname[$x]);
}
elseif (($high > $low) && ($high > $mid) && ($high < 15))
{
possibly300Message($vendorsname[$x]);
}
elseif (($high > $low) && ($high > $mid) && ($high >= 15))
{
likely300Message($vendorsname[$x]);
}
elseif (($mid > $low) && ($mid > $high))
{
likely296Message($vendorsname[$x]);
}else
{
unknownMessage($vendorsname[$x]);
}
if(($verlow != 0) || ($vermid != 0) || ($verhigh != 0))
{
if(($verlow > $vermid) && ($verlow > $verhigh))
{
verified295Message($vendorsname[$x]);
changeBackgroundBack($vendorsname[$x]);
changeImage($vendorsname[$x]);
}
elseif (($verhigh > $verlow) && ($verhigh > $vermid))
{
verified300($vendorsname[$x]);
changeBackground($vendorsname[$x]);
changeImage($vendorsname[$x]);
}
elseif (($vermid > $verlow) && ($vermid > $verhigh))
{
verified296($vendorsname[$x]);
changeBackgroundBack($vendorsname[$x]);
changeImage($vendorsname[$x]);
}
}
}
mysqli_free_result($result);
}
}
Make sure you have error displaying turned on. Add at the beginning of your script:
ini_set('display_errors', 1);
to make sure you don't have any errors.
I'm having an issue with some code that someone else has previously worked on.
The goal is to iterate through a directory and push any files that are within a certain date range to an array (files are in mmddyyy.txt format).
The (terribly named, not by my own doing) variables in the code represent the following:
$aYear - A given year, read in from a text file. This variable changes during every iteration of the loop. The same goes for $aMonth and $aDay.
$sYear1 - Start year. $sMonth1 and $sDay1 are used in respect to $sYear1.
$sYear2 - End year. $sMonth2 and $sDay2 are used in respect to $sYear2.
$isGood - File will be added to the array.
$isGood = false;
if($aYear >= $sYear1 && $aYear <= $sYear2)
{
if($aYear == $sYear1)
{
if($aMonth == $sMonth1)
{
if($aDay >= $sDay1 && $aDay <= $sDay2)
{
$isGood = true;
}
}
else
{
if($aMonth >= $sMonth1 && $aMonth <= $sMonth2)
{
$isGood = true;
}
}
}
else if($aYear == $sYear2)
{
if($aMonth == $sMonth2)
{
if($aDay <= $sDay2)
{
$isGood = true;
}
}
else
{
if($aMonth <= $sMonth2)
{
$isGood = true;
}
}
}
else
{
$isGood = true;
}
}
if($isGood)
{
//echo "Found good article";
$a = $a . "===" . $file;
array_push($result, $a);
}
I'm not getting the results that I expected. I'm looking for some help as to how I can simplify this code and get it working properly. I do need to keep this solution in PHP.
Thank you in advance.
It seems to me Month statement if($aMonth >= $sMonth1 && $aMonth <= $sMonth2) needs work
eg start date- 03 Aug 2013 end date- 04 Sep 2016 and check date say 08 Nov 2013
would make isGood=false whereas it should be true.
Removing && $aDay <= $sDay2 and && $aMonth <= $sMonth2 should work.
As #Sandeep pointed out you're issues are with:
if ($aMonth >= $sMonth1 && $aMonth <= $sMonth2)
and
if ($aDay >= $sDay1 && $aDay <= $sDay2)
as you don't need to be comparing the date with end dates as well.
That being said you can clear up your code completely by doing something like:
$date = (new DateTime)->setDate($aYear, $aMonth, $aDay);
$start = (new DateTime)->setDate($sYear1, $sMonth1, $sDay1);
$end = (new DateTime)->setDate($sYear2, $sMonth2, $sDay2);
if ($start <= $date && $date <= $end) {
//echo "Found good article";
$a = $a . "===" . $file;
array_push($result, $a);
}
Hope this helps!
I have this code:
if ( ($oldTime < (time() - self::wait)) ) {
if ($this->setTime())
{
return true;
}
else
{
return false;
}
} else {
return false;
}
Can i replace it with:
if ( ($oTime < (time() - self::wait)) && $this->setTime() ) {
return true;
} else {
return false;
}
I need it to check if $this->setTime() returns true ONLY if $oTime < (time() - self::wait) is true.
return ($oTime < (time() - self::wait)) && $this->setTime()
yes you can use this
if ( ($oTime < (time() - self::wait)) && $this->setTime() ) {
return true;
} else {
return false;
}
if first condition in the if statement with && ( not || ) fails, it will go to the else branch automatically without verifying the second condition
This seems simple but I don't know why it doe snot work.
I need to write an if statement that
first, checks if it is numeric
second, if it is not between 1 and 10, issue errorA
third, if it is not between 20 and 30, issue errorB
fourth, it is not a number, issue errorC
If is not numeric and satisfies all the ranges, added to the database.
anyways, I am not sure about the if and while combination to satisfy this....
So far I have,
if numeric and satisfies ranges, add to database
else, issue errorC
How can I filter for error A and B?
if ( isset [some code...]) {
$a = ...;
$b = ...);
$c = ...;
if (preg_match('/^\d+$/',$b) && preg_match('/^\d+$/',$c) &&
((1 <= $b && 10 >= $b)) && ((20 <= $c && 30 >= $c))) {
$sql = "INSERT [some code...]
mysql_query($sql);
$_SESSION['success'] = $_POST['success'];
header('Location: index.php') ;
return;
} else {
$_SESSION['error'] = $_POST['error'];
header('Location: index.php') ;
return;
}
}
if (preg_match('/^\d+$/',$b) && preg_match('/^\d+$/',$c)) {
if (($b >= 1 && $b <= 10) && ($c >= 20 && $c <= 30)) {
echo "OK";
} else {
echo "not in range";
}
} else {
echo "not a number";
}
how can I check for numbers only from -10 negative to +10 positive?
This is what I have, but I think it's not safe:
if(isset($_POST['number']) && ctype_digit($_POST['number']) && $_POST['number']>=-10 && $_POST['number']<=10){
//do something
}
and the form:
Input a number between -10 and 10: <input type="text" name="number" size="5" />
if( isset($_POST['number'])) {
$num = intval($_POST['number']);
if( $num >= -10 && $num <= 10) {
// do something
}
}
There are other ways, but that one will work. Anything that can't be converted to a number will be treated as zero. If this is not desired behaviour, add:
&& "".$num == $_POST['number']
To that inner IF statement, to ensure that no non-numeric characters were removed from the input.
Check whether a variable is a number including zero and negative values
$x = '-22';
if (isNumber($x, ['zero','negative']))
echo 'Yes';
else
echo 'No';
isNumber($x, $includes=[])
{
if (is_int($x)) {
if ($x === 0) {
if (in_array('zero', $includes))
return true;
} elseif ($x < 0) {
if (in_array('negative', $includes))
return true;
} else
return true;
} elseif (is_string($x)) {
if ($x == '0') {
if (in_array('zero', $includes))
return true;
} elseif ($x[0] == '-') {
if (in_array('negative', $includes))
return ctype_digit(substr($x, 1));
} else
return ctype_digit($x);
}
}