【问题标题】:Codeigniter Reservation ModuleCodeigniter 预留模块
【发布时间】:2017-02-05 14:28:06
【问题描述】:

所以我目前正在尝试为我们的课堂项目开发一个预订模块。我有一个预订表,其中有一个预订 ID、用户 ID、预订日期和预订时间。我的预订时间的值是“6”、“7”、“8”和“9”,它们指出了预订的时间。在我的检查预订状态页面中,用户可以查看日期的状态是否已经预订了什么时间。我的问题是如何正确输出保留状态?

例如:

我有 2017 年 2 月 5 日的多条记录,我使用了 foreach 循环。问题是,如果我使用 foreach,它将重复如下所示的行,而不是只显示一行。

期望的输出:

这是我的代码。

控制器:

function court_one()
{
    $data['date'] = date("Y/m/d");
    $data['result'] = $this->model_reservation_user->getcourtone_defaultavailability();
    $this->template->load('user_template', 'view_userreservation_courtone', $data);
}

function check_availability_courtone()
{
    $searchquery = $this->input->get('search', TRUE);

    if(isset($searchquery) and !empty($searchquery))
    {   
        $data['date'] = $searchquery;
        $data['result'] = $this->model_reservation_user->getcourtone_availability($searchquery);
        $this->template->load('user_template', 'view_userreservation_courtone', $data);
    }
    else
    {
        redirect('user_reservation/court_one');
    }
}

型号:

    function getcourtone_defaultavailability()
    {
        $query = $this->db->select('*')->from('courtone_reservation')->where('reservation_date', date("m/d/Y"))->get();

        if($query->num_rows() > 0)
        {
            return $query->result();
        }
        else
        {
            return $query->result();
        }
    }

    function getcourtone_availability($searchquery)
    {
        $query = $this->db->select('*')->from('courtone_reservation')->where('reservation_date', $searchquery)->get();

        if($query->num_rows() > 0)
        {
            return $query->result();
        }
        else
        {
            return $query->result();
        }
    }

查看:

      <table class="table table-hover">
              <tr>
                <th><br>Date and Time Reserved</th>
                <th><br>6:00-7:00</th>
                <th><br>7:00-8:00</th>
                <th><br>8:00-9:00</th>
                <th><br>9:00-10:00</th>
              </tr>

              <?php foreach ($result as $result): ?>
              <tr>
                <td><?php echo date("F d, Y", strtotime($date)); ?></td>
                <?php if($result == FALSE) { echo '<td class="vacant"></td>'; } else if($result->reservation_time == 6) { echo '<td class="reserved"></td>'; } else { echo '<td class="vacant"></td>'; } ?>
                 <?php if($result == FALSE) { echo '<td class="vacant"></td>'; } else if($result->reservation_time == 7) { echo '<td class="reserved"></td>'; } else { echo '<td class="vacant"></td>'; } ?>
                 <?php if($result == FALSE) { echo '<td class="vacant"></td>'; } else if($result->reservation_time == 8) { echo '<td class="reserved"></td>'; }   else { echo '<td class="vacant"></td>'; } ?>
                 <?php if($result == FALSE) { echo '<td class="vacant"></td>'; } else if($result->reservation_time == 9) { echo '<td class="reserved"></td>'; } else { echo '<td class="vacant"></td>'; }?>
              </tr>
              <?php endforeach; ?>
      </table>

【问题讨论】:

  • 您可以在控制器中创建一个关联数组,其中键为日期,值为时间,并将其循环到视图中
  • 您能提供一个示例,我将如何在控制器中实现这一点吗?我对此很陌生,这就是为什么我对此知之甚少,对不起
  • 你能粘贴你的控制器函数和 $query->result();结果?
  • 我刚刚添加了上面的控制器功能。 $query->result() 是什么意思;结果?
  • $query->result() 的输出;

标签: php mysql database codeigniter


【解决方案1】:

这假设您在结果中只返回一个包含多个小时的日期。

在你看来……

// Start table row
<tr><td><?php echo date("F d, Y", strtotime($date)); ?></td>

<?php
// Set an array of 10 'hour' switches
$tdX = array(0,0,0,0,0,0,0,0,0,0,0);

// loop through results setting the array switches
foreach ($result as $result)
  $tdX[$result->reservation_time] = 1;
}

// loop through array building row
for ($i = 6; $i<=9; $i++) {

 if ($tdX[$i] === 1 ) {
     $tdClass = 'reserved';
 } else {
     $tdClass = 'vacant';
 }

 echo "<td class='$tdClass'></td>";

}

// close row
echo '</tr>';

【讨论】:

    【解决方案2】:

    假设你的 $query->result() 输出如下

      $array =  array(
             array(
                 'reservation' => 7,
                 'user_id' => 2011909,
                 'reservation_date' => '2/05/2017',
                 'reservation_time' => 6
             ),
             array(
                 'reservation' => 8,
                 'user_id' => 2011909,
                 'reservation_date' => '2/05/2017',
                 'reservation_time' => 7
             ),
             array(
                 'reservation' => 9,
                 'user_id' => 2011910,
                 'reservation_date' => '2/05/2017',
                 'reservation_time' => 8
             )
         );  
    

    在您的控制器中,将您的数据作为关联数组,日期如下

     foreach ($array as $key => $value) {
         $date = strtotime($value['reservation_date']);
         $reservation = $value['reservation'];
         $data[$date][$reservation] = true;         
     }
    

    将您的数据传递给查看并在视图中循环数据,如下所示

     foreach ($data as $key => $value) {
     <tr>
                <td><?php echo date("F d, Y", strtotime($key)); ?></td>
         if(isset($value[6]) && $value[6] ) { echo '<td class="reserved"></td>'; }else{} 
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-25
      • 1970-01-01
      • 2011-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多