-1

私は 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 の番号はNo2 -8BE0 GR です)。アイテムと呼ばれるmysqlテーブル。

今 :

1)<tr>このようにfor eachでレスポンスをテーブルに出力したい

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>";
4

1 に答える 1

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>

mysql_また、標準関数ではなく MySQLi (MySQL が改良された) を使用しました。これは、mysql_ライブラリが非推奨になり、現在 MySQLi または PDO のいずれかを使用する必要があるためです。

于 2012-09-12T19:48:03.247 に答える