简体   繁体   中英

Problems displaying array results in table using CodeIgniter

I am in need of some help displaying the correct values from an array within an html table. I am writing a scheduling engine of sorts that will allow the user to schedule classes based on preferred dates/times and check the availability of educators.

The code below properly outputs a table with 8 weeks (in rows) and also displays my available educators (in columns). My dates are incremented 1 week at a time correctly, but the problem I am having is displaying the correct availability based on the Educator_Id. My view does not seem to be looping through the educators properly, as the same status is displayed for each educator. Reviewing the mysql logs, shows that the correct queries were executed (I can see that multiple id's were passed).

Here is what my table looks like. Week 4 should only show a personal day for Jane Doe.

Thanks for any help you can give.

这是我的桌子

My view:

<table>
 <thead>
  <tr>
   <th>Week #</th>
   <th>Date</th>
   <th>Time</th>
   <?php foreach($educators as $educator): ?>
    <th><?php echo $educator->First_Name.' '.$educator->Last_Name; ?></th>
   <?php endforeach; ?>
  </tr>
 </thead>

 <tbody>

 <?php
 $i = 0;
 foreach($weeks as $key => $week):
 $class_date= $class_dates[$key];
 $week = $weeks[$key];
 $master_schedule = $master_schedules[$key];
 $educator_schedule = $educator_schedules[$key];
 $educator_availability = $educator_availabilities[$key];
 $week_day_id = $i + 1;
 ?>
  <tr>
   <td>Week <?php echo $week_day_id; ?></td>
   <td><?php echo $class_date; ?></td>
   <td><?php echo $opportunity->First_Preferred_Start_Time.' - '.$opportunity->First_Preferred_End_Time; ?></td>

   <?php foreach($educators as $educator): ?>
   <td>
    <?php if($week->Count == 1): echo 'Class Conflict'; endif; ?>
    <?php if($master_schedule->Count == 1): echo $master_schedule->Title; endif; ?>
    <?php if($educator_schedule->Count == 1): echo $educator_schedule->Title; endif; ?>
    <?php if($educator_availability->Count == 1): echo 'Not Scheduled'; endif; ?>
   </td>
   <?php endforeach; ?>
  </tr>
  <?php if($i ++ == 7) break; endforeach; ?>
 </tbody>
</table>

My controller:

function schedule_opportunity($Opportunity_Id) {
    //retrieve opportunity details
    $this->data['opportunity'] = $this->ion_auth_model->get_opportunity($Opportunity_Id)->row();
    $opportunity = $this->ion_auth_model->get_opportunity($Opportunity_Id)->row();

    $Curricula_Id = $opportunity->Curricula_Id;
    $Preferred_Start_Time = $opportunity->First_Preferred_Start_Time;
    $Preferred_End_Time = $opportunity->First_Preferred_End_Time;
    $Preferred_Start_Date = $opportunity->First_Preferred_Start_Date;

    //find available educators
    $this->data['educators'] = $this->ion_auth_model->available_educators($Curricula_Id);
    $educators = $this->ion_auth_model->available_educators($Curricula_Id);

    foreach($educators as $educator):
        $Educator_Id = $educator->Educator_Id;

        for($i=0; $i < 8; $i++):
            $Date = strtotime("+$i week", strtotime($Preferred_Start_Date));
            $Class_Date = date("Y-m-d", $Date);

            $this->data['class_dates'][] = date("Y-m-d", $Date);
            $this->data['weeks'][] = $this->ion_auth_model->week($Educator_Id, $Class_Date, $Preferred_Start_Time, $Preferred_End_Time)->row();
            $this->data['master_schedules'][] = $this->ion_auth_model->master_schedule_check($Class_Date, $Preferred_Start_Time, $Preferred_End_Time)->row();
            $this->data['educator_schedules'][] = $this->ion_auth_model->educator_schedule_check($Educator_Id, $Class_Date, $Preferred_Start_Time, $Preferred_End_Time)->row();
            $this->data['educator_availabilities'][] = $this->ion_auth_model->educator_availability_check($Educator_Id, $Class_Date, $Preferred_Start_Time, $Preferred_End_Time)->row();
        endfor;
    endforeach;

    $this->data['main_content'] = 'schedule_opportunity';
    $this->load->view('./_blocks/template', $this->data);
}

My model:

function educator_schedule_check($Educator_Id, $Class_Date, $Preferred_Start_Time, $Preferred_End_Time) {
    $Date = "('$Class_Date' BETWEEN Start_Date AND End_Date AND All_Day = 1 AND Educator_Id = '$Educator_Id' OR '$Class_Date' BETWEEN Start_Date AND End_Date AND (Start_Time BETWEEN '$Preferred_Start_Time' AND '$Preferred_End_Time' OR End_Time BETWEEN '$Preferred_Start_Time' AND '$Preferred_End_Time'))";

    $this->db->select("Count(*) AS Count, Title")->from('Educators_Schedule')->where($Date)->where('Educator_Id', $Educator_Id);
    return $this->db->get();
}

Something about the following for loop does not seem quite right although it is difficult for me to say for certain without using a debugger.

<?php foreach($educators as $educator):
  <td>
    <?php if($week->Count == 1): echo 'Class Conflict'; endif; ?>
    <?php if($master_schedule->Count == 1): echo $master_schedule->Title; endif; ?>
    <?php if($educator_schedule->Count == 1): echo $educator_schedule->Title; endif; ?>
    <?php if($educator_availability->Count == 1): echo 'Not Scheduled'; endif; ?>
  </td>
<?php endforeach; ?>

I see that you are looping over the educators, but what I don't see is the current educator actually changing within the iteration. You do not appear to be moving through the $week, $master, $educator_schedule, and $educator_availability arrays. Instead you may be accessing the same element in each of those arrays.

In other words, consider two arrays. You loop over one array as such:

 <?php
     foreach($foos as $foo):
         echo $bar;
     endforeach;
 ?>

All that you would do in that loop is print out the value for $bar for each element of $foos .

Instead you need to access the elements of your $week, $master, $educator_schedule, and $educator_availability according to the educator being referenced in a particular iteration of the for loop so something that functions as:

<?php foreach($educators as $educator):
  <td>
    <?php if($week[$educator]->Count == 1): echo 'Class Conflict'; endif; ?>
    <?php if($master_schedule[$educator]->Count == 1): echo $master_schedule->Title; endif; ?>
    <?php if($educator_schedule[$educator]->Count == 1): echo $educator_schedule->Title; endif; ?>
    <?php if($educator_availability[$educator]->Count == 1): echo 'Not Scheduled'; endif; ?>
  </td>
<?php endforeach; ?>

Which will not work with the code in its current state, but may point you in the right direction.

Lastly, I am a little concerned with how you are controlling the order of elements returned from your database queries. Most of the time, your results will come back in the same order, but it might help for clarity and sanity if you add an order_by clause to some of your queries so that you can be sure that your results are properly correlated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM