0

嗨,我已经做了一些AJAXPHP&MySQL 排序,它给了我结果,如下面的代码所示,我的问题是如何将 $result 带入 html divs

请帮忙

使用的 PHP 代码

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'root', '');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("security_software", $con);

$sql="SELECT * FROM internet_security ORDER by '".$q."' DESC" ;


$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>id</th>
<th>title</th>
<th>image</th>
<th>description</th>
<th>rating</th>
<th>download</th>
<th>buy</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['title'] . "</td>";
  echo "<td>" . $row['image'] . "</td>";
  echo "<td>" . $row['description'] . "</td>";
  echo "<td>" . $row['rating'] . "</td>";
  echo "<td>" . $row['download'] . "</td>";
  echo "<td>" . $row['buy'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?> 

我想要这些 HTML Div 的结果

<div class="category-container">
    <div class="category-image"></div>
    <div class="category-link"><a href="#">#</a></div>
    <div class="category-desc"><p>#</p> </div>          
    <div class="rating5" >Editors' rating: </div>        
    <div class="category-download-btn"><a href="#">Download </a></div><
    <div class="category-buy-btn"><a href="#">Buy</a></div>
</div>
4

3 回答 3

1

我不知道您为什么在返回 ajax 响应时创建表。我建议您通过 ajax 创建 json 响应。使用此结果 JSON,您可以创建表,也可以在 html 中呈现它们。在发送 ajax 请求的 php 代码中:ajax.php

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'root', '');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("security_software", $con);

$sql="SELECT * FROM internet_security ORDER by '".$q."' DESC" ;


$result = mysql_query($sql);
$response = array();
$i=0;
while($row = mysql_fetch_array($result))
  {
  $response[$i]['id']           =$row['id'];
  $response[$i]['title']        = $row['title'];
  $response[$i]['image']        = $row['image'];
  $response[$i]['description']  = $row['description'];
  $response[$i]['rating']       = $row['rating'];
  $response[$i]['download']     = $row['download'];
  $response[$i]['buy']          = $row['buy'];
  $i++;
  }
mysql_close($con);

echo json_encode($response);
?>

在您获得此 ajax 响应的 html 文件中,我提示您如何使用此 ajax 响应:

<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script type="text/javascript">
    $.ajax({
        url: 'ajax.php',
        dataType: 'json',
        success: function(response){
            data = '';
            $.each(response,function(i,val){
              data = '<div class="category-image">'+val.image+'</div>'+
            '<div class="category-link"><a href="#">'+val.id+'</a></div>'+
            '<div class="category-desc"><p>'+val.description+'</p> </div>'+
            '<div class="rating5" >'+val.rating+'</div>'+ 
            '<div class="category-download-btn"><a href="'+val.download+'">Download </a></div>'+
            '<div class="category-buy-btn"><a href="'+val.buy+'">Buy</a></div>';
            $('<div>').attr('id',i).html(data).appendTo('#response');
        });
            });
        }
    });
  </script>
</head>

<body>
<div id='response'></div>   
</body>
</html>
于 2012-05-15T09:25:36.693 回答
0

如果您想在这些 div 中提取结果,则使用相同的 div 而不是 table/tr/tds,或者您可以通过接收 json/xml 或任何类型的基于对象的数据来绑定它

于 2012-05-15T08:39:20.797 回答
0

我倾向于将此视为一个笑话,因为数据库称为“security_software”,并且您将 GET var 直接放入数据库查询中而无需进行任何清理。在将数据库吐回页面之前,您也没有尝试清理来自数据库的任何内容......

无论如何,假设这不是一个玩笑,以下内容应该为您指明正确的方向:

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'root', '');
if (!$con) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("security_software", $con);

$sql="SELECT * FROM internet_security ORDER by '".$q."' DESC" ;

$result = mysql_query($sql);

while($row = mysql_fetch_array($result)) {
    echo '<div class="category-image"><img src="' $row['image'] . '</div>';
    echo '<div class="category-link"><a href="#">' . $row['title'] . '</a></div>';
    echo '<div class="category-desc"><p>' . $row['description'] . '</p></div>';        
    echo '<div class="rating5" >Editors' rating: ' . $row['rating'] . '</div>';       
    echo '<div class="category-download-btn"><a href="' . $row['download'] .'">Download</a></div>';
    echo '<div class="category-buy-btn"><a href="' . $row['buy'] . '">Buy</a></div>';
 }
echo "</table>";

mysql_close($con);
?>
于 2012-05-15T08:49:57.900 回答