2

再次需要你的帮助。

所以我有一个查询,有多个变量,我必须输出到一个表中。

这是查询:

SELECT DISTINCT
          zi.zile,
          ore.ora AS ore,
          materii.materie
        FROM
          zi LEFT JOIN  orar ON zi.id = orar.id_zi 
          LEFT JOIN  ore ON ore.id = orar.id_ora 
          LEFT JOIN  nume_scoli ON nume_scoli.id = orar.id_scoala 
          LEFT JOIN  materii_pe_clase ON materii_pe_clase.id_scoala = nume_scoli.id 
          LEFT JOIN  clase ON materii_pe_clase.id_clasa = clase.id AND orar.id_clasa = clase.id
          LEFT JOIN  elevi ON elevi.id_clasa = materii_pe_clase.id_clasa 
          LEFT JOIN  materii ON materii.id = orar.id_materie 
                WHERE clase.`id`=1

            ORDER BY zi.`zile`, ore.`id` ASC

此查询的结果如下所示:

图片

现在这是我在 php 中所做的:

  $oraru = "the query from up this page ";
    $gaseste_oraru = mysql_query($oraru);
    $numar_orar = mysql_num_rows($oraru);
  if($numar_orar==0)
    {
      echo "Orarul nu este disponibil momentan.";
    }
  else 
    {
    while($randorar=mysql_fetch_array($gaseste_oraru))
    {
        $ziorar = $randorar['zile'];
        $oraorar = $randorar['ore'];
        $materieorar = $randorar['materie'];
}
}

这是我想用数据库中的数据填充的表:

桌子

这里有表格的代码:

<div class="CSSTableGenerator" >
                <table >
                    <tr>
                        <td>
                           Orar
                        </td>
                        <td>
                           Luni
                        </td>
                        <td >
                            Marti
                        </td>
                        <td>
                            Miercuri
                        </td>
                        <td>
                            Joi
                        </td>
                        <td> 
                            Vineri
                        </td>
                    </tr>
                    <tr>
                        <td >
                            8:00 - 9:00
                        </td>
                        <td>
                           <select>
  <option>Mate</option>
  <option>Romana</option>
  <option>Geogra</option>
</select>

                        </td>
                        <td>
                            <select>
  <option>Mate</option>
  <option>Romana</option>
  <option>Geogra</option>
</select>
                        </td>
                        <td>
                            <select>
  <option>Mate</option>
  <option>Romana</option>
  <option>Geogra</option>
</select>
                        </td>
                        <td>
                           <select>
  <option>Mate</option>
  <option>Romana</option>
  <option>Geogra</option>
</select>
                        </td>

                             <td>
                            rand 1
                        </td>
                    </tr>
                    <tr>
                        <td >
                            9:00 - 10:00
                        </td>
                        <td>
                            rand 2
                        </td>
                        <td>
                            rand 2
                        </td>
                        <td>
                            rand 1
                        </td>
                        <td>
                            rand 1
                        </td>
                             <td>
                            rand 1
                        </td>
                    </tr>
                    <tr>
                        <td >
                            10:00 - 11:00
                        </td>
                        <td>
                            rand 2
                        </td>
                        <td>
                            rand 2
                        </td>
                        <td>
                            rand 1
                        </td>
                        <td>
                            rand 1
                        </td>
                             <td>
                            rand 1
                        </td>
                    </tr>
                    <tr>
                        <td >
                            11:00 - 12:00
                        </td>
                        <td>
                            rand 3
                        </td>
                        <td>
                            rand 3
                        </td>
                        <td>
                            rand 1
                        </td>
                        <td>
                            rand 1
                        </td>
                       <td>
                            rand 1
                        </td>
                </table>
            </div>

不要介意下拉菜单,我不会使用它们!

您是否知道如何填充它,但是如果在一段时间内不存在任何内容(例如 Luni 8:00-9:00),那么在表格中什么也不写?

编辑

这是我尝试过的:

<div class="CSSTableGenerator" >
   <table >
                    <tr>
                        <td>
                           Orar
                        </td>
                        <?php
  {
      $oraru = "the query from up this page";
    $gaseste_oraru = mysql_query($oraru);

    while($randorar=mysql_fetch_array($gaseste_oraru))
    {
        $ziorar = $randorar['zile'];
        $oraorar = $randorar['ore'];
        $materieorar = $randorar['materie'];
        echo "<td>";
        echo $randorar['zile'];
        echo "</td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td>";
        echo $randorar['ore'];
        echo "</td>";
        echo"<td>";
        echo $randorar['materie'];
        echo "</td>";
        echo "</tr>";
    }//sfarsit while

结果如下:

坏的

有任何想法吗?

4

3 回答 3

1

我建议您这样做ORDER BY ore.id, zi.zile

由于您逐行而不是逐列迭代,因此您将能够优化while循环。

于 2013-09-28T13:31:47.797 回答
1

尝试做这样的事情

$xarr=Array();
$xcols=Array();
while($randorar=mysql_fetch_array($gaseste_oraru))
{
    $xcols[$randorar['zile']]++;
    $xarr[$randorar['ore']][$randorar['zile']] = $randorar['materie']
} 
echo "<tr><th>orar</th>";
foreach(array_keys($xcols) as $x)
   echo '<th>'.$x.'</th>';
echo "</tr>\n";

foreach($xarr as $key=>$y){
   echo '<tr><th>'.$key.'</th>';
   foreach(array_keys($xcols) as $x)
      echo '<td>'.$y[$x].'</td>';
   echo "</tr>\n";
}
于 2013-09-28T13:46:40.363 回答
0

好的,这就是解决方案:

 $oraru = "
        SELECT DISTINCT
              zi.zile,
              ore.ora AS ore,
              materii.materie
            FROM zi 
              LEFT JOIN  orar ON zi.id = orar.id_zi 
              LEFT JOIN  ore ON ore.id = orar.id_ora 
              LEFT JOIN  nume_scoli ON nume_scoli.id = orar.id_scoala 
              LEFT JOIN  materii_pe_clase ON materii_pe_clase.id_scoala = nume_scoli.id 
              LEFT JOIN  clase ON materii_pe_clase.id_clasa = clase.id AND orar.id_clasa = clase.id
              LEFT JOIN  elevi ON elevi.id_clasa = materii_pe_clase.id_clasa 
              LEFT JOIN  materii ON materii.id = orar.id_materie 
              WHERE clase.`id`=1
            ORDER BY  ore.`id`, zi.`zile` ASC";
      $gaseste_oraru = mysql_query($oraru);
    $rows = array();
    while($randorar=mysql_fetch_array($gaseste_oraru))
    {        
        $ziorar = $randorar['zile'];
        $oraorar = $randorar['ore'];
        $materieorar = $randorar['materie'];
        if(!isset($rows[$oraorar])){
            $rows[$oraorar] = array();
        }
        $rows[$oraorar][$ziorar] = $materieorar;        
    }//sfarsit while

    ?>
<div class="CSSTableGenerator" >
    <table >
        <tr>
            <td>Orar</td><td>Luni</td><td>Marti</td><td>Mercuri</td><td>Joi</td><td>Vineri</td>
        </tr>
    <?php

        foreach ($rows as $key => $row) {
          if(!isset($row['Luni'])) $row['Luni'] = '&nbsp;';//incase no data from database
          if(!isset($row['Marti'])) $row['Marti'] = '&nbsp;';//incase no data from database
          if(!isset($row['Miercuri'])) $row['Miercuri'] = '&nbsp;';//incase no data from database
          if(!isset($row['Joi'])) $row['Joi'] = '&nbsp;';//incase no data from database
          if(!isset($row['Vineri'])) $row['Vineri'] = '&nbsp;';//incase no data from database
          echo "<tr>";
          echo "<td>".$key."</td>";
             foreach ($row as $day => $study) {
               echo "<td>$study</td>";    
             }
             echo "</tr>";
         } 
     ?>
    </table>
</div>

我在朋友的帮助下弄明白了!

总之谢谢大家!

于 2013-09-28T16:15:42.537 回答