title: Inputting/overwriting information in arrays with PHP
body: <p>I have a small PHP page which takes data from MySQL and displays it via PHP in a monthly calendar. I'm having trouble arranging the data properly within an array to get the desired output.</p>
  
  <p>First, I will describe what I would like to happen:</p>
  
  <ul>
  <li>students come to classes on regular days of the week</li>
  <li>they can also make or cancel reservations</li>
  <li>the calendar also displays days when the school is not open</li>
  </ul>
  
  <p>In order to display this data on the calendar, I use MySQL to output data from a variety of sources, and then input that into an array with PHP, which I sort by date and output. </p>
  
  <p>My issue is, I would like to be able to handle more than one row of data per day, but because I am using the date as the key, I am limited on only displaying one result per day. If I use a loop to append the date with a counter in the key, I get overlapping results in situations where someone made a reservation and then cancelled that reservation on the same day.</p>
  
  <p>As for my code...</p>
  
  <p>First, I check to see if the student is registered in a weekly class, then input that class into the array.</p>
  
  <pre><code>$sql = "SELECT StudentDB.studentid, ClassDB.classID, ClassDB.class_level, ClassDB.class_title, ClassDB.time, ClassDB.teacher, StudentDB.first_name, StudentDB.last_name, StudentDB.payment_amount, ClassDB.day 
              FROM ClassDB 
              INNER JOIN RegDB ON ClassDB.classID = RegDB.classid 
              INNER JOIN StudentDB ON StudentDB.studentID = RegDB.studentid 
              WHERE StudentDB.studentid = '$studentid'";
  
      $result = $conn-&gt;query($sql);
  
      if ($result-&gt;num_rows &gt; 0) {
  
  
        while($row = $result-&gt;fetch_assoc()) {   // DISPLAY REGULAR CLASS DATA
  
          $dayofclass = $row['day'];
          $class_level = $row['class_level'];
          $class_title = $row["class_title"];
          $day = $row["day"];
          $class_time = $row["class_time"];
          $time = $row["time"];
  
  
  
  
          // check which dates match the days of the week and store in an array
  
          for ($i=1;$i&lt;=$n;$i++){
  
            if ($i&lt;10) {
              $i = "0" . $i;
            }
  
            $day=date("l",strtotime($yearmonth.$i)); //find weekdays
  
            if($day==$dayofclass){
              $time = date("H:i",strtotime($row['time']));
              $dates[]=$yearmonth.$i;
              $datesdata[$yearmonth.$i] = "0";
              $timedata[$yearmonth.$i] = $time;
              $classiddate[$yearmonth.$i] = $row['classID'];
            }
          }
       }
        echo "&lt;/table&gt;";
        $conn-&gt;close();
      } 
  </code></pre>
  
  <p>After that, I check for specific reservations (cancelations, irregular reservations, waitlists) and input them into the array:</p>
  
  <pre><code>        $lowerlimit = $yearmonth . "01";
      $upperlimit = $yearmonth . "31";
  
      $sql = "SELECT AttendanceDB.*, ClassDB.* 
      FROM StudentDB 
      INNER JOIN AttendanceDB ON StudentDB.studentid = AttendanceDB.studentid 
      INNER JOIN ClassDB ON AttendanceDB.classid = ClassDB.classID
      WHERE StudentDB.studentid = '$studentid' 
      AND AttendanceDB.class_time &gt;= '$lowerlimit'
      AND AttendanceDB.class_time &lt;= '$upperlimit'";
  
      $result = $conn-&gt;query($sql);
  
      if ($result-&gt;num_rows &gt; 0) {  
  
        $loopcount = 0;
  
        // store furikae data in the array
        while($row = $result-&gt;fetch_assoc()) {  
  
          $phpdate = strtotime( $row["class_time"] );
          $time = date("H:i",strtotime($row['time']));
          $mysqldate = date( 'Y-m-d', $phpdate );
          $loopcount++;
          $mysqldate = $mysqldate . "+" . $loopcount;
  
  
  //            $loopcount++;
  //            $mysqldate = $mysqldate . "+" . $loopcount;
  
  
  
  
  
  
          $previousdate = $mysqldate;
          $previousfurikae = $row['furikae'];
  
  
          if ($row["furikae"] == 3){
            $dates[]=$mysqldate;
            $datesdata[$mysqldate] = "1";
            $timedata[$mysqldate] = $time;
            $classiddate[$mysqldate] = $row['classID'];
          } elseif ($row["furikae"] == 8 OR $row["furikae"] == 7) {
            $dates[]=$mysqldate;
            $datesdata[$mysqldate] = "3";
            $timedata[$mysqldate] = $time;
          } elseif ($row["furikae"] == 2) {
            $dates[]=$mysqldate;
            $datesdata[$mysqldate] = "2";
            $timedata[$mysqldate] = $time;
          }              
        }
      } 
  
      $conn-&gt;close();
  </code></pre>
  
  <p>Then finally I check the school calendar and input the days off into the array:</p>
  
  <pre><code>$sql = "SELECT * 
      FROM SchoolScheduleDB 
      WHERE date &gt;= '$lowerlimit'
      AND date &lt;= '$upperlimit'";
  
      $result = $conn-&gt;query($sql);
  
      if ($result-&gt;num_rows &gt; 0) {  
  
        // store furikae data in the array
        while($row = $result-&gt;fetch_assoc()) {  
  
          $phpdate = strtotime( $row["date"] );
  //            $time = date("H:i",strtotime($row['time']));
  //            $mysqldate = date( 'Y-m-d', $phpdate ) . " " . $time;
            $mysqldate = date( 'Y-m-d', $phpdate );
            $dates[]=$mysqldate;
            $datesdata[$mysqldate] = "666";
  
        }
      } 
  
      $conn-&gt;close();
  </code></pre>
  
  <p>The way I intended it to work was that:</p>
  
  <ol>
  <li>First the regular classes would be input</li>
  <li>Then any reservations would overwrite the original plans</li>
  <li>And finally the school calendar would overwrite everything</li>
  </ol>
  
  <p>Currently, this functions as it should, but it is limited to displaying 1 result per day, but I would like to be able to display more than 1 result per day for students who come to multiple classes. </p>
  
  <p>Thank you for your help. If I made any mistakes in my question or my question is unclear I will do my best to revise it.</p>