Conditional statement not giving desired output in PHP 7.4 - php

I have a PHP script that queries the database and produces results based on the condition. In PHP 7.1 all worked well. But the server the website is hosted has requested I change the server PHP language to 7.4.
After the change I noticed the issue of the desired results not appearing as it should.
Below are my codes.
<?Php if (($checkday != "Sunday")&&($phdate == "")) {
$davsquery = "SELECT davsID, davs, davsslots FROM dailyavailableslotsus WHERE YEAR(davs)=? AND MONTH(davs)=? AND DAY(davs)=?";
$davsstmt = $connQlife->prepare($davsquery);
$davsstmt->bind_param('sss', $year, $month, $day);
$davsstmt->execute();
$davsstmt->store_result();
$davsstmt->bind_result($davsID, $davs, $davsslots);
$davsstmt->fetch();
$davsstmt->close();
if (($davsslots!="")&&($totalnumrows!="")) {
$availableslots = $davsslots - $totalnumrows;
}
if (($davsslots!="")&&($totalnumrows=="")) {
$availableslots = $davsslots;
}
if (($totalnumrows=="")&&($davsslots=="")) {
$availableslots = $defaultusimslots;
}
if (($checkday == "Saturday")&&($davsslots=="")) {
$availableslots = $usimslots20 - $totalnumrows;
}
if (($checkday == "Saturday")&&($davsslots!="")) {
$availableslots = $davsslots - $totalnumrows;
}
?>
<div class="calendarheadercont">
<div class="calendarday"><?Php echo date("l", mktime(0, 0, 0,$month,$day,$year)); ?></div>
</div>
<div class="clear_1"></div>
<div class="calendarsubcont">
<div class="calendardatecont">
<div class="calendarmonth"><?Php echo date("M", mktime(0,0,0, $month,$day,$year)); ?></div>
<div class="calendardate"><?Php echo date("j", mktime(0,0,0,$month,$day,$year)); ?></div>
</div>
<?Php if ($day > $curday){ ?>
<div class="calendartextcont">
<?Php if ($availableslots > 5){ ?>
<div class="calendartextg">Available slots: <?Php echo $availableslots; ?></div>
<?php } ?>
<?Php if (($availableslots >= 1)&&($availableslots <= 5)) { ?>
<div class="calendartexto">Available slots: <?Php echo $availableslots; ?></div>
<?php } ?>
<?Php if ($availableslots <= 0){ ?>
<div class="calendartextr">No Slots Available!</div>
<?php } ?>
</div>
<?Php if ($availableslots >= 1){ ?>
<label class="calendarcheckbox">Select
<input type="checkbox" name="screening" id="screening" value="<?Php if ($screeningdate=="") {echo date('Y-m-d', mktime(0,0,0,$month,$day,$year)); }else{ echo $screeningdate; } ?>">
<span class="checkmark"></span>
</label>
<?php } ?>
<?php } else { ?>
<div class="calendartextcont">
<div class="calendartextr">Booking Closed!</div>
</div>
<?php } ?>
</div>
<?Php } ?>
<?Php if ($checkday == "Sunday") { ?>
<div class="calendarheadercont">
<div class="calendarday"><?Php echo date("l", mktime(0, 0, 0,$month,$day,$year)); ?></div>
</div>
<div class="clear_1"></div>
<div class="calendarsubcont">
<div class="calendardatecont">
<div class="calendarmonth"><?Php echo date("M", mktime(0,0,0, $month,$day,$year)); ?></div>
<div class="calendardate"><?Php echo date("j", mktime(0,0,0,$month,$day,$year)); ?></div>
</div>
<div class="calendartextcont">
<div class="calendartextr">Closed On Sundays!</div>
</div>
</div>
<?Php } ?>
<?Php if ($phdate != ""){ ?>
<div class="calendarheadercont">
<div class="calendarday"><?Php echo date("l", mktime(0, 0, 0,$month,$day,$year)); ?></div>
</div>
<div class="clear_1"></div>
<div class="calendarsubcont">
<div class="calendardatecont">
<div class="calendarmonth"><?Php echo date("M", mktime(0,0,0, $month,$day,$year)); ?></div>
<div class="calendardate"><?Php echo date("j", mktime(0,0,0,$month,$day,$year)); ?></div>
</div>
<div class="calendartextcont">
<div class="calendartextr"><?Php echo $phdetail; ?></div>
</div>
</div>
<?Php } ?>
What changed in PHP 7.4 that may be affecting the script? Or is there a better way to rewrite this that produces the same results but in code practice favorable to PHP 7.4
To put this into context, this is a subset of a larger script that loops through the days of each month and shows how many slots are available for someone who wants to make a booking for that day.
There is a default value of 30 slots per day. There is also a table that takes entries of a particular number of slots for any particular day specified. There is also a table that takes public holidays. The script is supposed to check if there is a manual entry of slots for a particular day and if there is, check the number of slots available for that day and subtract from the manual entry to determine how many slots are left. If not manual entry, the default slots is then used for that day.
On PHP 7.4, if I specify a manual entry of say 10 slots for a particular day, the output when the entire script is run is that all days of the current month now shows 10 as the slots available rather than 30 and only show 10 for the particular day specified.
The other issue I noticed is that, if I specify a public holiday, all days after that day also show as the public holiday.
But when I switch back to PHP 7.1 all works perfectly.

Related

Displaying number of records for each day in a month in a for loop to display the days of the month

I have a for loop that displays all the days in a month. I also have a table that has various dates stored in it. I want to be able to query that table to select total count of all records of each date in that month and display it on each date in the results of the for loop.
Don't know how to get it done.
I think the problem is in the query construction but i can't figure out the best way to go about it to achieve the aim.
For example, for day one of month June, i want to see 5 slots. for day two, 10 slots, etc
$ucimslots = "25";
$curyear = date("Y");
$month = $_POST['month'];
$day = date('j');
$daysinmonth = date('t',mktime(0,0,0,$month,1,$curyear));
$query = "SELECT uciID, bookingdate, paymentstatus FROM ucibooking WHERE bookingdate=?";
$stmt = $connQlife->prepare($query);
$stmt->bind_param('s', $day);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($uciID, $bookingdate, $paymentstatus);
$stmt->fetch();
$totalnumrows = $stmt->num_rows;
$stmt->close();
if ($totalnumrows!=0){
$availableslots = $ucimslots - $totalnumrows;
}else if ($totalnumrows==0){
$availableslots = $ucimslots;
}
<?Php for($day = 1; $day <= $daysinmonth; $day++){?>
<div class="calendarcont">
<div class="calendarheadercont">
<div class="calendarday"><?Php echo date("l", mktime(0, 0, 0,$month,$day,$curyear)); ?></div>
</div>
<div class="clear_1"></div>
<div class="calendarsubcont">
<div class="calendardatecont">
<div class="calendarmonth"><?Php echo date("M", mktime(0,0,0, $month,$day,$curyear)); ?></div>
<div class="calendardate"><?Php echo date("j", mktime(0,0,0,$month,$day,$curyear)); ?></div>
</div>
<div class="calendartextcont">
<div class="calendartextr"><?Php echo $availableslots; ?></div>
</div>
</div>
</div>
<?Php } ?>
I solved the issue. So in case anyone else in the future encounters this problem, what i did was i amended the query to the database. The issue was with the query and its call position. It was outside the for loop. So i included it in the for loop like below:
<?Php for($day = 1; $day <= $daysinmonth; $day++){
$query = "SELECT uciID, bookingdate, paymentstatus FROM ucibooking WHERE YEAR(bookingdate)=? AND MONTH(bookingdate)=? AND DAY(bookingdate)=?";
$stmt = $connQlife->prepare($query);
$stmt->bind_param('sss', $curyear, $month, $day);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($uciID, $bookingdate, $paymentstatus);
$stmt->fetch();
$totalnumrows = $stmt->num_rows;
$stmt->close();?>
<div class="calendarcont">
<div class="calendarheadercont">
<div class="calendarday"><?Php echo date("l", mktime(0, 0, 0,$month,$day,$curyear)); ?></div>
</div>
<div class="clear_1"></div>
<div class="calendarsubcont">
<div class="calendardatecont">
<div class="calendarmonth"><?Php echo date("M", mktime(0,0,0, $month,$day,$curyear)); ?></div>
<div class="calendardate"><?Php echo date("j", mktime(0,0,0,$month,$day,$curyear)); ?></div>
</div>
<div class="calendartextcont">
<div class="calendartextr"><?Php echo $availableslots; ?></div>
</div>
</div>
</div>
<?Php } ?>

Echo something every 24th iteration

I have just stumbled across an issue. I have been trying to make sure that whenever the id reaches a number based on 24 (24, 48, 72, etc) it would echo a certain output. Does anyone have any clue on how to do this?
Best wishes,
$mysql_connection = mysqli_query($mysqli, "SELECT * FROM data") or die(mysql_error());
while($bar_data = mysqli_fetch_assoc($mysql_connection)) { ?>
<div class="item-wrapper">
<div class="item">
<div class="brand">
<p class="brand-content"><?php echo $bar_data["brand"]; ?></p>
</div>
<div class="type">
<p class="type-content"><?php echo $bar_data["type"]; ?></p>
</div>
<div class="data">
<p class="data-content"><?php echo $bar_data["content"]; ?>, <?php echo $bar_data["color"]; ?></p>
</div>
<div class="price">
<p class="price-content"><?php echo $bar_data["price"]; ?></p>
</div>
<div class="img">
<?php
$generator = new \Picqer\Barcode\BarcodeGeneratorPNG();
echo '<img src="data:image/png;base64,' . base64_encode($generator->getBarcode($bar_data["barcode"], $generator::TYPE_CODE_128)) . '">';
?>
</div>
</div>
</div>
<?php
// When $bar_data["id"] reaches 24 based echo the following:
echo '</div><div class="page">';
}
}
?>
Use modulo arithmetic: http://php.net/manual/en/internals2.opcodes.mod.php
if ($bar_data['id'] % 24 == 0)
{
}
If the number you are on within your loop mod 24 == 0 then you can display the appropriate content.
The above code means that the number in $bar_data['id'] has no remainder when divided by 24. Values such as 24, 48, 72 satisfy this condition.

skip same dates in foreach loop

this is a text file content which will be chatting database
you:2016-05-02 11:41:53 Hi
Muhammad:2016-05-02 11:42:41 Hi
you:2016-05-02 11:43:33 How are you ?
Muhammad:2016-05-02 14:44:56 I'm fine!
this is the code to loop to get content
<?php
$chat = file("members/cdn/1/chats/9188.txt");
foreach($chat as $line){
$name = strchr($line,":",true);
$message = explode(' ', substr(strchr($line,":"),1), 3);
if(some thing){
?>
<div>
<!-- here i want to skip the same dates -->
<?=$message[0];?>
</div>
<?php
}
?>
<div class="container">
<div class="arrow">
<div class="outer"></div>
<div class="inner"></div>
</div>
<div class="message-body">
<p><?=$message[2];?></p>
<p class="message_time"><?=date("g:i a", strtotime($message[1]));?></p>
</div>
</div>
<div class="spacer"></div>
<?php
}
?>
I want the date of the message appear one time above of messages in the same date
Simply remember that date you last used and then compare it to the one in $message[0]
<?php
$lastDate = NULL;
$chat = file("members/cdn/1/chats/9188.txt");
foreach($chat as $line) :
$name = strchr($line,":",true);
$message = explode(' ', substr(strchr($line,":"),1), 3);
if($lastDate != $message[0]) :
$lastDate = $message[0];
?>
<div><?=$message[0];?></div>
<?php
endif;
?>
<div class="container">
<div class="arrow">
<div class="outer"></div>
<div class="inner"></div>
</div>
<div class="message-body">
<p><?=$message[2];?></p>
<p class="message_time"><?=date("g:i a", strtotime($message[1]));?></p>
</div>
</div>
<div class="spacer"></div>
<?php
endforeach;
?>
Try this:
$prevDate[] = array();
foreach($chat as $line){
$name = strchr($line,":",true);
$message = explode(' ', substr(strchr($line,":"),1), 3);
if(some thing){
?>
<div>
<!-- here i want to skip the same dates -->
<?php
if(!in_array($message[0],$prevDate)) { // check if date exist in array - means displayed previously or not
echo $message[0];
$prevDate = $message[0]; // store date in array so that next time you can check whether it has been already displayed or not
}
?>
</div>
<?php
}
?>
<div class="container">
<div class="arrow">
<div class="outer"></div>
<div class="inner"></div>
</div>
<div class="message-body">
<p><?=$message[2];?></p>
<p class="message_time"><?=date("g:i a", strtotime($message[1]));?></p>
</div>
</div>
<div class="spacer"></div>
<?php
}
First, you can use list assignment to get the components split out into separate vars:
list($user,$date,$time,$message) = explode(' ', substr(strchr($line,":"),1), 4);
Then you can use a simple comparison to see if the date is new:
if ($date != $last_date) {
$last_date = $date;
?><div><?=$date?></div><?php
}
You should declare $last_date before the loop, but you can leave its value undefined.

For loop to repeat and increment day number in code

I need loop some code trough 24 times to create a 24 day Christmas calendar. I have the for loop below but how do i add the '$i' to the code to make sure the wherever there is a day number it changes from 1 through to 24 each time? What would the code look like?
<?php for ($i = 1; $i < 25; $i++) { ?>
<!-- DAY 1 -->
<a href="offer.php?day=1<? echo '&dealership='. $dealership; ?>" class="item <? if ($today[mday] == 1) { echo "current yellow"; } else if ($today[mday] < 2 ) { echo "disabled"; }?>">
<div class="offer">
<h2>Day 1</h2>
<p><? echo Day_1_Offer('CAL_OFFER'); ?></p>
<? echo $termsLink; ?>
</div>
<div class="offer-img">
<img src="img/day1.jpg">
</div>
</a>
<?php } ?>
Just add <?php echo $i; ?> whenever you need the number.
<!-- DAY 1 -->
<a href="offer.php?day=<?php echo $i; ?><? echo '&dealership='. $dealership; ?>" class="item <? if ($today[mday] == 1) { echo "current yellow"; } else if ($today[mday] < 2 ) { echo "disabled"; }?>">
<div class="offer">
<h2>Day <?php echo $i; ?></h2>
<p><? echo call_user_func('Day_'.$i.'_Offer', 'CAL_OFFER'); ?></p>
<? echo $termsLink; ?>
</div>
<div class="offer-img">
<img src="img/day<?php echo $i; ?>.jpg">
</div>
</a>
<?php } ?>
To modify the Day_1_Offer call you must do a things a bit differently, as it's a function and in the PHP and not part of the HTML. To do this, you need call_user_func(). Try this:
<p><? echo call_user_func('Day_'.$i.'_Offer', 'CAL_OFFER'); ?></p>

Extract the correct month from timestamp with PHP

got stuck in extracting the correct month from timestamp...Here's my Code
$sql = "SELECT id,notice,description,DAY( `posted_date` ) AS DAY, MONTH(`posted_date` ) AS MONTH, YEAR( `posted_date`) AS YEAR, enable FROM n_notice";
$result=mysqli_query($con,$sql);
while ($run = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$notice = $run['notice'];
$description = $run['description'];
$day =$run['DAY'];
//$month=$run['MONTH'];
$month=date("M",$run['MONTH']);
$year=$run['YEAR'];
?>
<div class="media">
<div class="media-left media-middle">
<div id="date">
<div class="mon"><?php echo $month;?></div>
<div class="day"><?php echo $day;?></div>
<div class="year"><?php echo $year;?></div>
</div>
</div>
<div class="media-body">
<h5 class="media-heading"><?php echo $notice; ?></h5>
<?php echo $description; ?>
</div>
</div>
<?php
}
// Free result set
mysqli_free_result($result);
?>
</div>
DB timestamp column
How to get the correct month in "M" format? All the Months are shown as "Jan"!!!
$month=date("M",$run['MONTH']);
Try to change "M" to "F".
F - A full textual representation of a month (January through December)

Categories