Home > Back-end >  How I sort an array of guests per rooms and weekday
How I sort an array of guests per rooms and weekday

Time:08-05

How could I sort this array correctly?

I want to create an HTML-Table with entries of DB like this:

Mon, 2022-08-01 Tue, 2022-08-02 Wed, 2022-08-03 Thurs, 2022-08-04 Fri, 2022-08-05 Sat, 2022-08-06 Sun, 2022-08-07
Room1 Otto Anna Peter Susi ------ -------- ------
Room1 ---(free)--- ------- Anna -------- ------ -------- ------
Room1 ------ ------- --------- -------- ------ -------- ------
Room1 ------ ------- --------- -------- ------ -------- ------
Room2 ------ ------- --------- Martina Jani -------- ------
Room2 ------ ------- --------- -------- ------ -------- ------
Room3 ------ ------- --------- -------- ------ -------- ------

I generatE an array of weekdays, rooms and guests. The first Column represents the guest capacity of the rooms. So Room 1 could have 4 person, Room2 2 person and room 3 could only 1 person. For the room column, I differentiate if it is the first line ever and if no more than 1 person in it or if I have to repeat this room-line, when more than 1 person could be in this room.

So I had two areas for the output of guest names.

I could sort it from Monday to Sunday but had no idea how I should create the code when there is more than one person per day in the same room.

Do you have an idea what sort I should use?

If you need: https://phpize.online/sql/mysql57/undefined/php/php81/6edf73078595bfca86c6d95518bb68d9/

Complete code:

<?php

$calendar['Monday, 2022-08-01'][] = array( 
             "Firstname" => "Otto",
             "Lastname" => "Exmapleman",
             "Room" => "100");
$calendar['Tuesday, 2022-08-2'][] = array( 
             "Firstname" => "Anna",
             "Lastname" => "Examplewoman",
             "Room" => "100");
$calendar['Wednesday, 2022-08-03'][] = array( 
             "Firstname" => "Peter",
             "Lastname" => "Man",
             "Room" => "100");
$calendar['Wednesday, 2022-08-03'][] = array( 
             "Firstname" => "Anna",
             "Lastname" => "Examplewoman",
             "Room" => "100");
$calendar['Thursday, 2022-08-04'][] = array( 
             "Firstname" => "Susi",
             "Lastname" => "Woman",
             "Room" => "100");
$calendar['Friday, 2022-08-05'][] = array( 
             "Firstname" => "Martina",
             "Lastname" => "Flower",
             "Room" => "100");
$calendar['Saturday, 2022-08-06'][] = array( 
             "Firstname" => "Janni",
             "Lastname" => "Milk",
             "Room" => "100");
/*$calendar['Sunday, 2022-08-07'][] = array( 
             "Firstname" => "Jack",
             "Lastname" => "End",
             "Room" => "100");*/

var_dump($calendar);

$roomnrarray [] = array( 
                "roomnumber" =>'100',
                "guestcount" =>'4');
$roomnrarray [] = array( 
                "roomnumber" =>'200',
                "guestcount" =>'2');
$roomnrarray [] = array( 
                "roomnumber" =>'300',
                "guestcount" =>'1');

var_dump($roomnrarray);

$daynames = array("Monday, 2022-08-01", "Tuesday, 2022-08-02", "Wednesday, 2022-08-03", "Thursday, 2022-08-04", "Friday, 2022-08-05", "Saturday, 2022-08-06", "Sunday, 2022-08-07");
var_dump($daynames);

$totalguestcapacity = 0;
foreach ($roomnrarray as $room)
    {
     $totalguestcapacity = $totalguestcapacity   $room['guestcount'];
    }

$loopcount =0;
$weekdaycount = 0;
$wdh=false;
$varRoomcount = 0;
$wdhcount= 0;

echo "<table>";
echo "<tr>";
for($i=0; $i<=7; $i  ) { 
  if ($i==0)
  {
     echo "<th></th>";
  }
  else
  {
    echo "<th>". $daynames[$weekdaycount] ."</th>";
    $weekdaycount  ;
  }
}
echo "</tr>";

do
{
    echo "<tr>\n";
    if ($wdh!=true) //First line with this roomnumber
    {
        echo "<td>Room" . $roomnrarray[$varRoomcount]['roomnumber'] ."</td>\n";
        //If guestcapacity of current room not 1, then repeat a new row with same room
        if ($roomnrarray[$varRoomcount]['guestcount']!=1)
        { 
            $wdh =true;
            $wdhcount  ;
        }
        else
        {
            $wdh= false;
            $wdhcount =0;
            if ($varRoomcount < count($roomnrarray))$varRoomcount  ;
        }
        //OUTPUT GUEST
    }
    else //repeat the roomnumber if guestcapacity of room is more than 1 person
    {
        echo "<td>Room" . $roomnrarray[$varRoomcount]['roomnumber'] ."</td>\n";
        $wdhcount  ;
        if ($wdhcount >= $roomnrarray[$varRoomcount]['guestcount'])  //If repeat same as roomcapacity
        {
            if ($varRoomcount < count($roomnrarray))$varRoomcount  ;
            $wdh= false;
            $wdhcount = 0;
        }
        //OUTPUT GUEST
    }

    echo "</tr>\n";

    $loopcount  ;
}while($loopcount < $totalguestcapacity);

?>

CodePudding user response:

Your sample data doesn't match the result you say you want - you got Martina and Janni in Room 100, it should be 200. (And they all should be 1 instead of 100, 2 instead 200, etc. to begin with, according to the desired output you have shown - I ignored that, and will list your rooms as "Room100" etc.)

Once that's fixed, I'd recommend that you (re-)organize your data in a different way - by room first, then by day second:

$calendarByRoom = [];
foreach($calendar as $day => $data) {
    foreach($data as $dayData) {
       $calendarByRoom[$dayData['Room']][$day][] = $dayData;
    }
}
var_dump($calendarByRoom);

Once you done that, you loop over your $roomnrarray array. Then inside that loop, you do a loop for the guestcount of that day - that's how many rows you need to create for each room. And then you simply check your reorganized data array, whether there is an entry under that room number, day name and row number:

foreach($roomnrarray as $room) {
    for($i=0; $i<$room['guestcount'];   $i) {
        echo '<tr>';
        echo '<td>Room' . $room['roomnumber'] . '</td>';
        foreach($daynames as $dayname) {
            echo '<td>';
            if(isset($calendarByRoom[$room['roomnumber']][$dayname][$i])) {
                echo $calendarByRoom[$room['roomnumber']][$dayname][$i]['Firstname'];
            } else {
                echo '------';
            }
            echo '</td>';
        }
        echo '</tr>';
    }
}
  • Related