Want to show opening hours dynamically in php - php

Hi i want to show Opening hours which i'm getting dynamically through database. here is the array of that
Array
(
[from] => Array
(
[0] => 09:30
[1] => 09:30
[2] => 09:30
[3] => 09:30
[4] => 09:30
[5] => 09:30
[6] => 09:30
)
[to] => Array
(
[0] => 18:30
[1] => 18:30
[2] => 18:30
[3] => 18:30
[4] => 18:30
[5] => 18:30
[6] => 18:30
)
)
Now what i want is if the time is equals to fri then it will show the time Mon-Fri and then Sat Sun closed or if the time is same till Saturday then it will show Mon-Sat and if the whole week time is same then it will show Mon-Sun. Hope i am able to tell you exactly what i want. This is my PHP code
Updated Answer
$result = array_unique($time['from']);
if (count(array_unique($time['from'])) == 3)
{
echo '<li><strong>Mon-Fri:</strong><span>' . $time['from'] . ' to ' . $time['to'] . '</span></li>
<li>
<strong>Sat-Sun:</strong>
<span>'. $time['from'][6] .'</span>
</li>';
} else if(count(array_unique($time['from'])) == 2)
{
echo '<li><strong>Mon-sat:</strong><span>' . $time['from'][0] . ' to ' . $time['to'][0] . '</span></li>
<li>
<strong>Sun:</strong>
<span>'. $time['from'][6] .'</span>
</li>';
}
else if(count(array_unique($time['from'])) == 1) {
echo '<li><strong>Mon - Sun:</strong><span>' . $time['from'][0] . ' to ' . $time['to'][0] . '</span></li>';
}

Related

How to print the all the dates of the month by 15 days first and next 15 days in 2 array?

These are the code for all the dates in a month. And i want to make 2 arrays with 15 days interval dates.
$date = "2018-05";
$list=array();
$month = date('m', strtotime($date));
$year = date('Y', strtotime($date));
for($d=1; $d<=31; $d++) {
$time=mktime(12, 0, 0, $month, $d, $year);
if (date('m', $time)==$month){
$list[]=date('Y-m-d', $time);
}
}
echo "<pre>"; print_r($list); echo "</pre>";
Result :
Array
(
[0] => 2018-05-01
[1] => 2018-05-02
[2] => 2018-05-03
[3] => 2018-05-04
[4] => 2018-05-05
[5] => 2018-05-06
[6] => 2018-05-07
[7] => 2018-05-08
[8] => 2018-05-09
[9] => 2018-05-10
[10] => 2018-05-11
[11] => 2018-05-12
[12] => 2018-05-13
[13] => 2018-05-14
[14] => 2018-05-15
[15] => 2018-05-16
[16] => 2018-05-17
[17] => 2018-05-18
[18] => 2018-05-19
[19] => 2018-05-20
[20] => 2018-05-21
[21] => 2018-05-22
[22] => 2018-05-23
[23] => 2018-05-24
[24] => 2018-05-25
[25] => 2018-05-26
[26] => 2018-05-27
[27] => 2018-05-28
[28] => 2018-05-29
[29] => 2018-05-30
[30] => 2018-05-31
)
instead of this how to make 2 arrays.first 15 days of the month and then the 2nd array will be the next 15 days?
Try this. I've explained the code as much as I could. Your question was too broad, so I can't punctually answer the question. You asked HOW to create the output and here is exactly how.
The output is almost identical to what you asked:
<style>
th, td { border: 1px solid gray; }
th { height:20px;}
/* here I'm giving borders to the grid, and a default height so 2nd line doesn't collapse */
</style>
<?php
//settings:
$month = 5;
$year = 2018;
$ymd = $year . '-' . str_pad($month,2,"0",STR_PAD_LEFT); //this trows 2018-05-01
$lastDay = date('d', strtotime( $ymd . '-01 + 1 month - 1 day')); //I calculate the last day of the month (in this case, 31)
//title:
echo date('M Y', strtotime($ymd)); //print month and year (May 2018)
//space:
echo '<br><br>';
//first table:
makeTable(1,15); //call to the function below, days 1 to 15
//space:
echo '<br><br>';
//2nd table:
makeTable(16,$lastDay); //call to the function below, days 15 to last day
function makeTable($from,$to){
global $month;
//begin table:
echo '<table>';
echo '<tr>';
//first row with dates:
for($d=$from; $d<=$to; $d++) {
//here I create each cell of first row
echo '<th>' . str_pad($d,2,"0",STR_PAD_LEFT) . '-' . str_pad($month,2,"0",STR_PAD_LEFT) . '</th>';
}
echo '</tr><tr>';
//second row blank:
for($d=$from; $d<=$to; $d++) {
//same as above but without text inside the cell
echo '<th> </th>';
}
//end table:
echo '</tr>';
echo '</table>';
}
?>

Getting a specific id value for use

I'm currently working on a friendshipsystem. To accept a friend I need to get the friendship_id value.
Based on the email (from the session) I can get a lot of information, such as surname, name, photo, ID NUMBER,... from the specific friendshiprequest
I can print the information for each friendship in this way. So I have the FRIENDSHIP ID VALUE TO DISPLAY AS INFORMATION but now I want to use it in a function.
$friendrequests=$friendship->GetAllFriendRequests($email);
<?php
foreach ($friendrequests as $request) {
echo "
<div><p>
<a href='profile.php?user_id=".$request['friendship_applicant_id'] . "'>
<img src='uploads/" . $request['friendship_applicant_avatar'] . " " . " ' alt='' />" . $request['friendship_applicant_surname'] . $request['friendship_id'] . " " . $request['friendship_applicant_name'] . "
</a> has send you a friend request" . "
<form action='" . $_SERVER['REQUEST_URI'] . "' method='post'>
<button type='submit' name=''>Accept</button>
<button type='submit' name=''>Decline</button>
</form>
</p></div>";
}
?>
So i tried to get the specific number with the following code, but it says undefined index for friendship_id: $friendrequestnumber = $friendrequests['friendship_id'];
This is the code the GetAllFriendRequests function. Can I use this code or should I do it in a totally different way?
public function GetAllFriendRequests($email) {
$db = new Db();
$select = "SELECT * FROM friendship WHERE friendship_recipient = '" . $email . "' AND friendship_status = 'pending' ORDER BY friendship_id DESC";
$result = $db -> conn -> query($select);
$result_array=array();
while ($row = mysqli_fetch_array($result)) {
$result_array[]=$row;
}
return $result_array;
}
With a var_dump I get this information of the 2 requests:
Array ( [0] => Array ( [0] => 84 [friendship_id] => 84 [1] => ron#hot.com [friendship_applicant] => ron#hot.com [2] => 29 [friendship_applicant_id] => 29 [3] => ron [friendship_applicant_name] => ron [4] => ron [friendship_applicant_surname] => ron [5] => 1394134610fuckyou.jpg [friendship_applicant_avatar] => 1394134610fuckyou.jpg [6] => jan#hot.com [friendship_recipient] => jan#hot.com [7] => 1 [friendship_recipient_id] => 1 [8] => Vandenbergh [friendship_recipient_name] => Vandenbergh [9] => Jan [friendship_recipient_surname] => Jan [10] => 1394041001fuckyou.jpg [friendship_recipient_avatar] => 1394041001fuckyou.jpg [11] => Pending [friendship_status] => Pending [12] => 0000-00-00 00:00:00 [friendship_time] => 0000-00-00 00:00:00 ) [1] => Array ( [0] => 78 [friendship_id] => 78 [1] => Bert#hot.com [friendship_applicant] => Bert#hot.com [2] => 2 [friendship_applicant_id] => 2 [3] => Van Damme [friendship_applicant_name] => Van Damme [4] => Bert [friendship_applicant_surname] => Bert [5] => sdfds.png [friendship_applicant_avatar] => sdfds.png [6] => Jan#hot.com [friendship_recipient] => Jan#hot.com [7] => 1 [friendship_recipient_id] => 1 [8] => Vandenbergh [friendship_recipient_name] => Vandenbergh [9] => Jan [friendship_recipient_surname] => Jan [10] => 1394041001fuckyou.jpg [friendship_recipient_avatar] => 1394041001fuckyou.jpg [11] => Pending [friendship_status] => Pending [12] => 0000-00-00 00:00:00 [friendship_time] => 0000-00-00 00:00:00 ) )
From your var_dump, $friendrequests is a nested array. So you need to use a loop to iterate over all values like in the case above where $request['friendship_id'] can use it or if you simply want the first value, use $friendrequests[0]['friendship_id']

Display multidimensional array in a customize way

I have a multidimensional array and it is like this:
Array
(
[Monday] => Array
(
[open] => 05.00 PM
[close] => 04.00 PM
[state] => 0
)
[Tuesday] => Array
(
[open] =>
[close] =>
[state] => 1
)
[Wednesday] => Array
(
[open] => 03.00 AM
[close] => 06.00 PM
[state] => 0
)
[Thursday] => Array
(
[open] =>
[close] =>
[state] => 1
)
[Friday] => Array
(
[open] => 05.00 PM
[close] => 03.00 PM
[state] => 0
)
[Saturday] => Array
(
[open] => 05.00 PM
[close] => 06.00 PM
[state] => 0
)
[Sunday] => Array
(
[open] =>
[close] =>
[state] => 1
)
)
Using this array I want to create output like this:
Monday - 05.00 PM - 04.00 PM
Tuesday - Closed
Wednesday - 03.00 AM - 06.00 PM
Thursday - Closed
Friday - 05.00 PM - 03.00 PM
Saturday - 05.00 PM - 06.00 PM
Sunday - Closed
I tried it with 2 foreach loops, but I couldn't get it to show the expected output.
foreach ($result as $days => $values) {
echo "$days";
foreach ($values as $k) {
echo " - $k";
}
echo "<br/>";
}
Its output is similar to this:
Monday - 05.00 PM - 04.00 PM - 0
Tuesday - - - 1
Wednesday - 03.00 AM - 06.00 PM - 0
Thursday - - - 1
Friday - 05.00 PM - 03.00 PM - 0
Saturday - 05.00 PM - 06.00 PM - 0
Sunday - - - 1
Can anybody tell me how can I figure this out?
NOTE: if state = 0 it doesn't need to display and state = 1 it should be Closed
Do the foreach loop like that:
foreach ($result as $days => $values) {
echo $days." - ";
echo $values["state"] ? "Closed" : $values["open"]." - ".$values["close"];
echo "<br />";
}
You should check state index exist(1) or not(0) like: if ($values['state'])
Try this one:
foreach ($result as $days => $values) {
echo "$days";
if ($values['state']) {
echo " - Closed";
} else {
echo ": ".$values['open']." - ".$values['close'];
}
echo "<br/>";
}
Do foreach loop with html table to get currect aligned line
$data = "<table >";
foreach($result as $day=>$value) {
if($value["state"]==1) {
$value = "<td colspan='3'> Closed </td>";
} else {
$value = "<td> $value[open] </td><td> - </td><td> $value[close] </td>";;
}
$data .= "<tr><td> $day </td><td> - </td> $value</tr>";
}
$data .= "</table>";
echo $data;

display Array within ul li tags

I have an array that looks like this and I want to display it in a more readable format. I would like the bird name (Gray Hawk etc) and then each listing (which the number of result will vary. This is the code I currently have:
$xml = simplexml_load_file($noteable);
$result = array();
foreach ($xml->result->sighting as $sighting) {
$location = (string) $sighting->{'loc-name'};
$bird = (string) $sighting->{'com-name'};
$howMany = (string) $sighting->{'how-many'};
$obsdt = (string) $sighting->{'obs-dt'};
$thenotedate = $obsdt;
$thenotedate = split('T',$thenotedate);
$thenotedate = $thenotedate[0];
$thenotedate = strftime('%a %b %e at %I:%M %p',strtotime($thenotedate));
ksort($result);
if (!isset($result[$bird])) $result[$bird] = array();
$result[$bird][] = $howMany . ' seen at ' . $location . ' on ' . $thenotedate;
}
print"<pre>";
print_r($result);
print"</pre>";
}
And this is the array
[Gray Hawk] => Array
(
[0] => 1 seen at Florida Canyon--lower on Sun Jun 2 at 04:50 PM
[1] => 1 seen at Madera Canyon--Whitehouse Picnic area on Sat Jun 1 at 07:30 AM
[2] => 1 seen at Florida Canyon--lower on Thu May 30 at 07:56 AM
[3] => 1 seen at Florida Canyon--lower on Wed May 29 at 07:40 AM
[4] => 1 seen at Florida Canyon--lower on Wed May 29 at 07:37 AM
[5] => 1 seen at Madera Canyon--Madera Picnic Area on Tue May 28 at 04:45 PM
[6] => 1 seen at Madera Canyon--Proctor Rd. on Mon May 27 at 09:40 AM
)
[MacGillivray's Warbler] => Array
(
[0] => 1 seen at Madera Canyon--Proctor Rd. on Sat May 25 at 05:45 PM
[1] => 1 seen at Madera Canyon--Proctor Rd. on Sat May 25 at 05:45 PM
[2] => 1 seen at Madera Canyon--Proctor Rd. on Sat May 25 at 05:30 PM
)
try something like this -
foreach ($result as $key=>$value){
//echo Bird Name
echo "<h3>$key</h3>";
//start an unordered list
echo "<ul>";
//echo each sighting
foreach($value as $row){
echo "<li>$row</li>";
}
//close the unordered list
echo "</ul>";
}
when printing out the result don't use print_r($result); use a loop which returns each element in the array! If you need infos on how to display the array as you want to tell us exactly how do you want it to be outputted

PHP filter and sum array values

I've got a php script with following array (date,task,actor,hh:mm).
Array
(
[0] => 2013-01-29|Making movies|Laurel|07:30
[1] => 2013-01-29|Making movies|Hardy|00:30
[2] => 2013-01-29|Learning PHP|Hardy|07:00
[3] => 2013-01-29|Singing autographs|Keaton|07:30
[4] => 2013-01-29|Making movies|Lloyd|07:30
[5] => 2013-01-28|Learning PHP|Laurel|07:30
[6] => 2013-01-28|Making movies|Hardy|07:30
[7] => 2013-01-28|Learning PHP|Keaton|07:30
[8] => 2013-01-28|Making movies|Lloyd|07:30
[9] => 2013-01-27|Learning PHP|Laurel|05:30
[10] => 2013-01-27|Making movies|Laurel|02:30
[11] => 2013-01-27|Learning PHP|Hardy|07:30
[12] => 2013-01-27|Making movies|Keaton|07:30
[13] => 2013-01-27|Making movies|Lloyd|07:30
)
I'd like to create a filter that lists the tasks and sums the time values of each task, for example:
Learning PHP (<-selected option)
2013-01-29 Hardy 07:00
2013-01-28 Laurel 07:30
2013-01-28 Keaton 07:30
2013-01-27 Laurel 05:30
2013-01-27 Hardy 07:30
=======================
TOTAL 35:00
What would be the way to make this happen?
Any suggestions or next steps are more than welcome.
This would be easier to filter if you had a multidimensional array. Something like this would work for the current structure though:
$task = "Learning PHP";
foreach($arr as $k=>$v) {
$pieces = explode("|", $v);
if($pieces[1]==$task) {
$total += (int) str_replace(':','',$pieces[3]);
echo $pieces[0] . " " . $pieces[2] . " " . $pieces[3];
}
}
echo "Total: " . $total;

Categories