duan032225 2013-09-28 13:18
浏览 43
已采纳

如何将mysql查询输出到html表?

need your help again.

So I have a query, with multiple variables which I have to output to a table.

Here is the query:

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

and the result of this query looks like this :

image

Now this is what I`ve done in 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'];
}
}

This is the table I want to populate with data from the database:

table

Here you have the code of the table:

<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>

NEVERMIND THE DROPDOWNS, I WON`T USE THEM!

Do you have any idea how to populate it, BUT if nothing exists for an interval (e.g Luni 8:00-9:00), then write nothing in the table?

EDIT

Here is what I tried :

<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

Here is the result:

bad

Any ideas?

  • 写回答

3条回答 默认 最新

  • doucuo9126 2013-09-28 16:15
    关注

    Ok, so this is the solution:

     $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>
    

    I figured it out with some help from a friend!

    Thank you all anyway!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?
  • ¥20 用户端如何上传图片到服务器和数据库里
  • ¥15 现在研究生在烦开题,看了一些文献,但不知道自己要做什么,求指导。
  • ¥30 vivado封装时总是显示缺少一个dcp文件
  • ¥100 pxe uefi启动 tinycore
  • ¥15 我pycharm运行jupyter时出现Jupyter server process exited with code 1,然后打开cmd显示如下
  • ¥15 可否使用carsim-simulink进行四轮独立转向汽车的联合仿真,实现四轮独立转向汽车原地旋转、斜向形式、横移等动作,如果可以的话在carsim中如何进行相应设置
  • ¥15 Caché 2016 在Java环境通过jdbc 执行sql报Parameter list mismatch错误,但是同样的sql使用连接工具可以查询出数据
  • ¥15 疾病的获得与年龄是否有关