Ajax responsetext作为表输出?

时间:2012-09-12 18:29:52

标签: php mysql ajax html-table response

我对Ajax Response并不熟悉 - 我已经从W3schools.com编辑了PHP Ajax搜索代码,如下所示:

<?php
require_once('connect_db.php');

$query = "select item_no from items";
$result = mysql_query($query);
$a = array();

while ($row = mysql_fetch_assoc($result)){
    $a[] = $row['item_no'];
}

//get the q parameter from URL
$q=$_GET["q"];

//lookup all hints from array if length of q>0
if (strlen($q) > 0)
  {
  $hint="";
  for($i=0; $i<count($a); $i++)
    {
    if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
      {
      if ($hint=="")
        {
        $hint=$a[$i];
        }
      else
        {
        $hint=$hint." , ".$a[$i];
        }
      }
    }
  }

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
  {
  $response="No Suggestion";
  }
else
  {
  $response=$hint;
  }

//output the response
echo "<table border=1><tr><td>".$response."</td></tr></table>";

?>

上述代码的输出效果很好,但它们都是这样列出的(2L500BU,2L500GO,2L500NA,2L500RD,2L802CA,2L802WH,2L803GR,2L804BE,2L804BK,2L804CO,2L805BU,2L806BE,2L806GR) - 这些数字是名为items的mysql表中的Item No。

现在:

1)我想将响应输出到表<tr>,每个人都这样

2l500BU

2L500GO

。 等

2)您认为可以根据输入的提示从Mysql输出所有表记录,如下所示:

$sql="SELECT * FROM items WHERE item_no = '".**$hint**."'";

$result = mysql_query($sql);

echo "<table align='center' cellpadding='3' cellspacing='3' width='800px' border='1' font style='font-family:arial;'>";
echo "
<tr align=center>
<th style=font-size:18px; bgcolor=#20c500>Item Number</th>
<th style=font-size:18px; bgcolor=#20c500>QTY</th>
<th style=font-size:18px; bgcolor=#20c500>Actual Price</th>
<th style=font-size:18px; bgcolor=#20c500>Selling Price</th>
<th style=font-size:18px; bgcolor=#20c500>Difference</th>
<th style=font-size:18px; bgcolor=#20c500>Date</th>
</tr>";


while($row = mysql_fetch_assoc($result)){

echo "<tr align=center bgcolor=#e3e3e3>";
  echo "<td style='font-size:18px; font-weight:bold;'>" . strtoupper($row['item_no']) . "</td>";
  echo "<td style='font-size:18px; font-weight:bold;'>" . $row['qty'] . "</td>";
  echo "<td style='font-size:18px; font-weight:bold;'>" . $row['actual_price'] . "&nbsp;<font style=font-size:12px;>JD</font></td>";
  echo "<td style='font-size:18px; font-weight:bold;'>" . $row['discount_price'] . "&nbsp;<font style=font-size:12px;>JD</font></td>";
  echo "<td style='font-size:18px; font-weight:bold;'>" . $row['difference_price'] . "&nbsp;<font style=font-size:12px;>JD</font></td>";
  echo "<td style='font-size:18px; font-weight:bold;'>" . date("d-m-Y",strtotime($row['date'])) . "</td>";
  echo "</tr>";

}
echo "<table>";

1 个答案:

答案 0 :(得分:1)

如果您想从数据库中获取项目并为每个项目显示一行,那么您就可以使用jQuery来实现这一点。

您的PHP脚本:

<?php
    $mysqli = new mysqli('localhost', 'user', 'password', 'database');
    $sql = "SELECT item_no FROM items";
    $res = $mysqli->query($sql);
    while ($row = $res->fetch_assoc()) {
        $rows[] = $row['item_no'];
    }
    header('Content-Type: application/json');
    echo json_encode($rows);
?>

您的HTML与表格:

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <table>
      <thead>
        <tr>
          <th scope="col">Item No.</th>
        </tr>
      </thead>
      <tbody>
      </tbody>
    </table>
  </body>
</html>
<script src="js/lib/jquery.js"></script>
<script>
    $(document).ready(function() {
        $.getJSON('yourscript.php', function(items) {
            $.each(items, function(i, item) {
                $('tbody').append('<tr><td>' + item + '</td></tr>);
            });
        });
    });
</script>

我还使用了MySQLi(MySQL改进版)而不是标准mysql_函数,因为mysql_库已被弃用,您现在应该使用MySQLi或PDO。