2016-11-30 5 views
-1

私は特定のtdのすべてのデータを表示しようとしますが、私のコードを修正する場所はわかりません。明確な問題を理解するには、以下のスクリーンショットを確認してください。この問題を解決するために何か助けてください。特定のクラス、つまりAまたはBの件名を表示したいが、現在のIはAのみを表示していますか?

<table class="table table-hover table-bordered "> 

     <tr><th>Day</th><th colspan="2">08:00-08:40</th><th colspan="2">8:40-09:20</th><th colspan="2">09:20-10:00</th><th>10:00-10:15</th><th colspan="2">10:15-10:55</th><th colspan="2">10:55-11:35</th><th colspan="2">11:35-12:15</th><th>12:15-01:15</th><th colspan="2">01:15-01:55</th><th colspan="2">01:55-02:35</th></tr> 

<?php 

    $timesVariants = array("08:00-08:40", "08:40-09:20", "09:20-10:00", "10:00-10:15", "10:15-10:55", "10:55-11:35", "11:35-12:15", "12:15-01:15", "01:15-01:55","01:55-02:35"); 

$sqlquery = "SELECT * FROM timetable,classroom,subprogramme WHERE classroom.classid = timetable.classid AND subprogramme.subid = timetable.subid"; 
    $res = $connect->query($sqlquery); 

$classes = array(); 
while($row = $res->fetch_assoc()) { 
    $classes[$row['day']][$row['tid']][$row['time']] = array('courseid'=> $row['cid'], 'classname' => $row['classname'], 'subname' => $row['subname']); 
} 

//This is a loop 
foreach($classes as $day => $daySchedule) { 
foreach($daySchedule as $teacher) { 

    print '<tr>'; 
    print "<td>$day</td>"; 
    foreach($timesVariants as $time) { 
     if (empty($teacher[$time])){ 

      print "<td>*</td><td>*</td>"; 
     } 


     else{ 

      print '<td>' . $teacher[$time]['courseid'] . '</td><td>' . $teacher[$time]['subname'] . ''. $teacher[$time]['classname'] . '</td>'; 
     } 

    } 
    print '</tr>'; 
} 
} 

?> 
</table> 

出力

Results display on the webpage

答えて

0
 <?php 

     if(isset($_POST["tid"])){ 

      $tid = $connect->real_escape_string($_POST["tid"]); 


     $sqlquery = "SELECT * FROM timetable,classroom,subprogramme WHERE classroom.classid = timetable.classid AND subprogramme.subid = timetable.subid AND timetable.tid = ".$tid." ORDER BY timetable.timid ASC"; 
     $res = $connect->query($sqlquery); 

      if($res->num_rows > 0){ 


     ?> 

      <table class="table table-bordered table-striped"> 

      <tr><th>Day</th><th colspan="2">8:00-8:40</th><th colspan="2">8:40-9:20</th><th colspan="2">9:20-10:00</th><th colspan="2">10:00-10:15</th><th colspan="2">10:15-10:55</th><th colspan="2">10:55-11:35</th><th colspan="2">11:35-12:15</th><th colspan="2">12:15-1:15</th><th colspan="2">1:15-1:55</th><th colspan="2">1:55-2:35</th></tr> 
    <?php 


     $timesVariants = array("8:00-8:40", "8:40-9:20", "9:20-10:00", "10:00-10:15", "10:15-10:55", "10:55-11:35", "11:35-12:15", "12:15-01:15", "1:15-1:55","1:55-2:35"); 

    $classes = array(); 
    while($row = $res->fetch_assoc()) { 
     $classes[$row['day']][$row['tid']][$row['time']] = array('courseid'=> $row['cid'], 'classname' => $row['classname'], 'subname' => $row['subname']); 
    } 

    //This is a loop 
    foreach($classes as $day => $daySchedule) { 
    foreach($daySchedule as $teacher) { 

     print '<tr>'; 
     print "<td>$day</td>"; 
     foreach($timesVariants as $time) { 
      if (empty($teacher[$time])){ 

       print "<td>*</td><td>*</td>"; 
      } 


      else{ 

       print '<td>' . $teacher[$time]['courseid'] . '</td><td>' . $teacher[$time]['subname'] . ''. $teacher[$time]['classname'] . '</td>'; 
      } 

     } 
     print '</tr>'; 
    } 
    } 

    ?> 
    </table> 

    <?php 
       } 

      else{ print "<div class='alert alert-danger col-md-4'><span class='glyphicon glyphicon-remove'></span> Not yet set timetable</div>"; } 
     } ?> 
    </div> 

The problems was on td I forgot to put colspan = 2, and other problem on sql query I forgot to inner join the tables in order to get all right that i want to display on the page. 
0

あなたの悩みは、GROUP BYです。 MySqlには、各列が集計関数を使用するという厳密な制限はありません。結果はSQLを使用するPHPを複雑にするのではなく、最初の行です。結果は膨大ではないようですので、別のループはパフォーマンスに影響しません。

$timesVariants = array("08:00-08:40", "08:40-09:20", "09:20-10:00", "10:00-10:15", "10:15-10:55", "10:55-11:35", "11:35-12:15", "12:15-1:15", "01:15-01:55","01:55-02:35"); 

$sqlquery = "SELECT * FROM timetable"; 

$classes = array(); 
while($row = $res->fetch_assoc()) { 
    $classes[$row['day']][$row['tid']][$row['time']] = array('subject'=> $row['subject'], 'class' => $row['class'], 'progid' => $row['progid']); 
} 

//This is a loop 
foreach($classes as $day => $daySchedule) { 
foreach($daySchedule as $teacher) { 

    print '<tr>'; 
    print "<td>$day</td>"; 
    foreach($timesVariants as $time) { 
     if (empty($teacher[$time])) 
     print "<td>None</td><td>None</td>"; 
     else 
     print '<td>' . $teacher[$time]['subject'] . '</td><td>' . $teacher[$time]['class'] . '</td>'; 
    } 
    print '</tr>'; 
} 
} 
+0

2oppion @エラーは$ timesVariants、$クラス –

+0

@Erickで発生し、私もチェックするエディタに入れてやりなさい、しかし、あなたはセミコロンを追加し、決定することができなかったことを「$行」私は正確にどこを修正するかわかりませんが、$ timesVariants = [];これは未定義ですか?)、答えを更新しました。 – 2oppin

+0

@ 2oppion $ classes = []; [件名] => $行['件名']、 'クラス' = $ classes ['日'] [$行['tid']] [$行['時間']] = [ > $ rows ['class']、 'progid' => $行['progid']];この3つの領域には、Dreamweaverに赤いマークが表示されます。コンソールタイプ 'php -l yourphp.php'に –

関連する問題