簡體   English   中英

如何將ajax排序結果帶入html div

[英]how to bring ajax sorted result into html divs

您好,我已經完成了一些AJAX ,PHP&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>

我不知道為什么要在返回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>

如果要在這些div中提取結果,則使用相同的div代替table / tr / tds,或者可以通過接收json / xml或任何基於對象的數據來綁定它

我傾向於把這看作是一個笑話,因為數據庫被稱為“ 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);
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM