PHP时段预订

时间:2010-12-28 18:23:48

标签: php sql timeslots

关于这个问题..

PHP Booking timeslot

我试过'GROUP BY'id_timeslot仍然没有工作,因为它只显示预订的时间段不可用

我试过这个解决方案,但是给我一个错误并且不太明白如何使用'coelence'

table timeslot  (id_timeslot integer);
table doctor (id_doctor integer);
table bookslot  (id_bookslot, id_doctor, id_timeslot integer);

insert into doctor (id_doctor)
values (1 = doc_A), (2 = doc_B), (3 = doc_C);

insert into TimeSlot (id_timeslot)
values (1 = 10:00:00), (2 = 10:15:00), (3 = 10:30:00), (4 = 10:45:00);

insert into bookslot (id_doctor,id_timeslot)
values (1,1), (1,5), (2,1), (2,4), (3,1);

加入mysql表

$q = $mysqli->query("SELECT * FROM bookslot 
  RIGHT JOIN timeslot ON bookslot.id_timeslot = timeslot.id_timeslot
  LEFT JOIN doctor ON bookslot.id_doctor = doctor.id_doctor ");

回显结果并检查它是否与今天的日期匹配,否则设置为

while($r = $q->fetch_array(MYSQLI_ASSOC)) :

 echo '<tr>';
 echo '<td align="center">' . $r['times']  . '</td>';

 if($r['booked_date'] == date('Y-m-d') && $r['id_doctor'] == 1):
  echo '<td><a href="#available" class="booked">booked</a></td>';
 else :
  echo '<td><a href="#" class="available">available</a></td>';
 endif;

 if($r['booked_date'] == date('Y-m-d') && $r['id_doctor'] == 2):
  echo '<td><a href="#available" class="booked">booked</a></td>';
 else :
  echo '<td><a href="#" class="available">available</a></td>';
 endif;

 if($r['booked_date'] == date('Y-m-d') && $r['id_doctor'] == 3):
  echo '<td><a href="#available" class="booked">booked</a></td>';
 else :
  echo '<td><a href="#" class="available">available</a></td>';
 endif;

echo '</tr>';

endwhile;

来自网页的结果 alt text

我希望结果如下:

id_timeslot  doc_A    doc_B    doc_C
----------------------------------------------
1            booked     booked     booked
2            available  available  available
3            available  available  available
4            available  booked     available
5            booked     available  available

请其他任何解决方案!

3 个答案:

答案 0 :(得分:2)

重要的是要认识到sql-table不是具有x,y-data的表。

所以你在php-output中看到的正是你从数据库中得到的,一个数据列表,每行只存储一个事实(时间,人),并重复任何插槽/人。

你需要自己压缩它,然后在你从sql获取行的列中创建。 (您也可以在严格的SQL中执行此操作,但我认为这不会让事情变得更容易,所以我会离开它。)

首先,我建议使用不同的sql,它会为每个人/插槽组合提供一个值,并在SQL中计算可用性。

SELECT *, isnull(bookslot.id_bookslot) as 'available' FROM timeslot 
LEFT JOIN doctor ON bookslot.id_doctor = doctor.id_doctor 
LEFT JOIN timeslot ON bookslot.id_timeslot = timeslot.id_timeslot
ORDER BY timeslot.times, doctor.name_doctor

您现在不需要计算可用性,而是阅读计算字段$ r ['available']。

顺序是很重要的:你需要它,所以它会列出你想要它们从左到右的所有值。所以你想要[(10点,医生A),(10点,医生B),(10:15,医生A)......]

您需要两个循环,一个用于行,另一个用于创建列。但是,由于查询已更改,您已经知道自己拥有所有值。

$prev_slot = 0;
$firstrow = true;

while($r = $q->fetch_array(MYSQLI_ASSOC)) :

$slot =  $r['times'] ;
if( $slot != $prev_slot ):
    // new timeslot, so here and only here we create a new row      
    if( !$firstrow ):
        // if needed, close the old:        
        echo '</tr>';
        $firstrow = false;
    endif;
    echo '<tr>';
    echo '<td align="center">' . $r['times']  . '</td>';
endif;

// here we are just moving sideways
if($r['available'] ):
    echo '<td><a href="#" class="available">available</a></td>';        
else :
    echo '<td><a href="#available" class="booked">booked</a></td>';
endif;
endwhile;

echo '</tr>'; // close the final row. 

答案 1 :(得分:1)

嗯,看起来你检查每个时间索引,然后打印你的表。试试这个

 while($r = $q->fetch_array(MYSQLI_ASSOC)) :
    echo '<tr>';
    echo '<td align="center">' . $r['times']  . '</td>';

    if($r['booked_date'] == date('Y-m-d') && $r['id_doctor'] == 1):
       echo '<td><a href="#available" class="booked">booked</a></td>';
    elseif($r['booked_date'] == date('Y-m-d') && $r['id_doctor'] == 2):
       echo '<td><a href="#available" class="booked">booked</a></td>';
    elseif($r['booked_date'] == date('Y-m-d') && $r['id_doctor'] == 3):
       echo '<td><a href="#available" class="booked">booked</a></td>';
    else :
       echo '<td><a href="#" class="available">available</a></td>';
    endif;

    echo '</tr>';
 endwhile;

答案 2 :(得分:0)

更改查询以在每个插槽中返回一行,或者在从结果集中获取数据后重新排列数组。

相关问题