如何从php图库中生成xml

时间:2013-04-01 04:42:57

标签: php jquery html ajax

我是一个新的PHP和ajax的新手,所以我还在学习。下面的功能是我目前拥有的,它查询数据库并输出我的网站的图像。这很好用。

我现在正尝试从结果中生成xml以提供给javascript,这样如果用户想要拥有不同的图库视图 - (例如2行而不是4行),页面只会更新而不刷新。我是否必须将所有结果分配给一个数组然后用javascript解压缩?不确定从哪里开始。请指点我,非常感谢!

<?php include 'library/connect.php';
$result = mysql_query("SELECT * FROM fproduct WHERE fproduct.category='Shirts'");
$cols=3;
echo "<table>";
do{
   echo "<tr>"; // adds a new row after the for loop has been executed for 4 times (4 columns)
    for($i=1;$i<=$cols;$i++){
        $row=mysql_fetch_array($result); 
        if ($row) {?>               
            <td> <a href = "Shirtbuy.php?id=<? echo $row['product_id'] ?>" ><img border="0" alt="pic" src="images/products/shirts/largethumbs/<? echo $row['pic']?>" /></a>     
            <br/>
            <b><?= $row['prod_name']?> </b><br/>    
            <b><?= $row['price']?>   </b><br/> 
            </td>
            <td width="100">&nbsp;</td> <?  
            }
        else{
            echo "<td>&nbsp;</td>"; //If there are no more records at the end, add a blank column
            }
    } 
} while($row);
    echo "</table>";
include 'library/close.php';

1 个答案:

答案 0 :(得分:0)

而不是XML使用json

您可以使用php函数json_enode将php数组转换为json格式字符串。

$json_data = json_encode($result_array);

然后将其传递给您的javascript&amp;解析它

Json Encode

在javascript方面。使用jquery并解析json函数

var obj = $.parseJSON(json_data);

Parse Json

Jquery Ajax示例请求

  function send_data() {
    $.ajax({
      url   : "my_php_code.php", 
      type  : "POST",
      cache : false,
      data  : {
        the_key : the_value
      }
    });
  }