duanditang2916 2017-07-31 19:35
浏览 22
已采纳

查询从mysql返回到php表不在顶部

I am very new at using PHP and am trying to populate a table with data from a MySQL database, but the results that display are dotted all over the table and are not in their relevant columns at the top of the table.

Attached is a screenshot of where I need the data to be.

Screenshot

Here's the code.

Any help is appreciated.

<?php
$server = mysql_connect("xxxx", "xxxx", "xxxxx"); 
  $db = mysql_select_db("xxxxxx", $server); 
  $query = mysql_query("SELECT * FROM stores"); 

?>  


<html>
<head>
<title>Stores Tracker Dashboard</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p align="center"><font face="Arial"><strong>Stores Tracker Dashboard</strong></font></p>

<table align="center" border="1" width="90%">
<tr>
    <td align="center"><font face="Arial"><strong>Not Started</strong></font></td>
    <td align="center"><font face="Arial"><strong>In Transit</strong></font></td>
    <td align="center"><font face="Arial"><strong>Awaiting Build</strong></font></td>
    <td align="center"><font face="Arial"><strong>Being Built</strong></font></td>
    <td align="center"><font face="Arial"><strong>On Return</strong></font></td>
    <td align="center"><font face="Arial"><strong>Received Back</strong></font></td>
</tr>


<?php

        while ($row = mysql_fetch_array($query)) {?>

<tr>
        <td align="center"><font face="Arial"><?php if ($row['status'] == "Not Started") { echo $row['ticket'], '&nbsp; (', $row['site'], ')'; }?></font></td>
    <td align="center"><font face="Arial"><?php if ($row['status'] == "In Transit") { echo $row['ticket'], '&nbsp; (', $row['site'], ')'; }?></font></td>
    <td align="center"><font face="Arial"><?php if ($row['status'] == "Awaiting Build") { echo $row['ticket'], '&nbsp; (', $row['site'], ')'; }?></font></td>
    <td align="center"><font face="Arial"><?php if ($row['status'] == "Being Built") { echo $row['ticket'], '&nbsp; (', $row['site'], ')'; }?></font></td>
    <td align="center"><font face="Arial"><?php if ($row['status'] == "On Return from CFC2") { echo $row['ticket'], '&nbsp; (', $row['site'], ')'; }?></font></td>
    <td align="center"><font face="Arial"><?php if ($row['status'] == "Received") { echo $row['ticket'], '&nbsp; (', $row['site'], ')'; }?></font></td>
</tr>
<?php  } ?>
  • 写回答

2条回答 默认 最新

  • dragon_9000 2017-08-01 16:33
    关注

    This one uses PDO. Please take your time and learn what is going on there!

    <?php
      $host = "set_hostname_here";
      $daba = "set_database_here";
      $user = "set_loginname_here";
      $pass = "set_password_here";
    
      $notstarted = array();
      $intransit = array();
      $awaiting = array();
      $built = array();
      $cfc2 = array();
      $received = array();
    
      try {    
        //open the PDO-connection to a MySQL-host
        $DBH = new PDO("mysql:host=$host;dbname=$daba", $user, $pass);
    
        //error mode set to EXCEPTION (ERRMODE_SILENT or ERRMODE_WARNING are also possible)
        $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    
        //execute the query
        $STH = $DBH->query('SELECT status, ticket, site FROM stores');
    
        //set default fetch mode to FETCH_ASSOC (if you want to try FETCH_OBJ)
        $STH->setFetchMode(PDO::FETCH_ASSOC);
      }
      catch(PDOException $e) {
        echo $e->getMessage();
      }
    
      //go through all results and assign them to the related arrays
      while($row = $STH->fetch()) {
        $data = $row['ticket'] . '&nbsp; (' . $row['site'] . ')';
        if ($row['status'] == "Not Started") { $notstarted[] = $data; }
        if ($row['status'] == "In Transit") { $intransit[] = $data; }
        if ($row['status'] == "Awaiting Build") { $awaiting[] = $data; }
        if ($row['status'] == "Being Built") { $built[] = $data; }
        if ($row['status'] == "On Return from CFC2") { $cfc2[] = $data; }
        if ($row['status'] == "Received") { $received[] = $data; }
      }
    
      //close the connection
      $DBH = null;
    ?>
    
    <html>
      <head>
        <title>Stores Tracker Dashboard</title>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
      </head>
    
      <body>
        <p align="center"><font face="Arial"><strong>Stores Tracker Dashboard</strong></font></p>
    
        <table align="center" border="1" width="90%">
          <tr>
            <td align="center"><font face="Arial"><strong>Not Started</strong></font></td>
            <td align="center"><font face="Arial"><strong>In Transit</strong></font></td>
            <td align="center"><font face="Arial"><strong>Awaiting Build</strong></font></td>
            <td align="center"><font face="Arial"><strong>Being Built</strong></font></td>
            <td align="center"><font face="Arial"><strong>On Return</strong></font></td>
            <td align="center"><font face="Arial"><strong>Received Back</strong></font></td>
          </tr>
    
          <?php
            for ($i = 0; $i < max(count($notstarted),count($intransit),count($awaiting),count($built),count($cfc2),count($received)); $i++) {
              echo "<tr>";
              echo "<td align=\"center\"><font face=\"Arial\">";
              if (count($notstarted) > $i) echo $notstarted[$i];
              echo "</font></td>";
              echo "<td align=\"center\"><font face=\"Arial\">";
              if (count($intransit) > $i) echo $intransit[$i];
              echo "</font></td>";
              echo "<td align=\"center\"><font face=\"Arial\">";
              if (count($awaiting) > $i) echo $awaiting[$i];
              echo "</font></td>";
              echo "<td align=\"center\"><font face=\"Arial\">";
              if (count($built) > $i) echo $built[$i];
              echo "</font></td>";
              echo "<td align=\"center\"><font face=\"Arial\">";
              if (count($cfc2) > $i) echo $cfc2[$i];
              echo "</font></td>";
              echo "<td align=\"center\"><font face=\"Arial\">";
              if (count($received) > $i) echo $received[$i];
              echo "</font></td>";
              echo "</tr>";
            }
          ?>
        </table
      </body>
    </html>
    

    I used the example of the answer by manassehkatz. Most of the code relating the PDO connection is nearly copy/pasted from the link given by tadman ...

    Good luck for your project.

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

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)