【发布时间】: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