从mysql db自动更新xml feed而不刷新页面

时间:2012-10-26 05:14:12

标签: php mysql xml

我必须使用php代码从mysql数据库开发一个xml feed。我必须在我的mysql数据库上插入或更新任何数据意味着插入和更新的数据会自动更改并插入我的xml feed也无需刷新页面我可以开发这个。请帮助我。

我使用了以下代码:

$catname=func_query_first_cell("select status from $sql_tbl[orders] where status='Q'");
     $file= fopen("orderdetails1.xml", "w");        
     $_xml ="<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\r\n"; 
     $_xml .="\t<Feed>\r\n";
     $_xml .="\t<order>\r\n";

$_xml .="\t<status>" .htmlspecialchars($catname,ENT_QUOTES). "</status>\r\n";   
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$limit = 2;
$startpoint = ($page * $limit) - $limit;
$statement = "`xcart_orders` where `active` = 1";    
$counterr=0;
$r=func_query("select * from $sql_tbl[orders] LIMIT {$startpoint}, {$limit}");
foreach($r as $n)
 {
$products=func_query_first("select * from $sql_tbl[orders] where status='Q'");
 $products=func_query_first("select product from $sql_tbl[order_details] where orderid=$n[orderid]");

$infeed_counter++;

 echo $manufacturer."=====";

if($row[avail]>0)
                $avail='Y'; 
            else 
                $avail='N'; 


$_xml .="\t<Order>\r\n";
  $_xml .="\t<orderid>" .$n[orderid]. "</orderid>\r\n";
   $_xml .="\t<login>" .  htmlspecialchars(strip_tags(substr($n[login],0,50)) , ENT_QUOTES ). "</login>\r\n";   
$_xml .="\t<total>" . $n[total]. "</total>\r\n";
    $_xml .="\t<product>" .  $products[product]. "</product>\r\n";
    $_xml .="\t</Order>\r\n";   
}

    $_xml .="\t</order>\r\n";   
    $_xml .="\t</Feed>\r\n";    
fwrite($file, $_xml);       
 fclose($file);     
echo "XML version of products available here with $infeed_counter products.  <a href=\"orderdetails1.xml?page=$page\">View the XML.</a>";
     exit;
     ?>

现在我得到了以下xml提要:

<Feed>
<order>
<status>Q</status>
<Order>
   <orderid>1</orderid>
        <login>krishna</login>
        <total>399.99</total>
        <product>Designing Web Usability</product>
</Order>
 <Order>
        <orderid>65</orderid>
        <login>krishna</login>
        <total>399.99</total>
        <product>Three Stone Princess Cut Diamond Ring</product>
  </Order>
 <Order>
          <orderid>2</orderid>
          <login>krishna</login>
          <total>34.65</total>
           <product>Three Stone Princess Cut Diamond Ring</product>
</Order>

现在我希望在我的mysql数据库上更改总计399.99到500.00 for orderid = 1表示必须刷新页面。然后我的xml feed才更改。但是我希望解决方案是数据库更改是自动的更新我的xml feed而不刷新page.please帮助我。我可以开发这个。

2 个答案:

答案 0 :(得分:1)

试试这样: feed.php

$catname = func_query_first_cell("select status from $sql_tbl[orders] where status='Q'");
$file = fopen("orderdetails1.xml", "w");
$_xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>";
$_xml .= "<Feed>";
$_xml .= "<order>";

$_xml .= "<status>" . htmlspecialchars($catname, ENT_QUOTES) . "</status>";
$page = (int)(!isset($_GET["page"]) ? 1 : $_GET["page"]);
$limit = 2;
$startpoint = ($page * $limit) - $limit;
$statement = "`xcart_orders` where `active` = 1";
$counterr = 0;
$r = func_query("select * from $sql_tbl[orders] LIMIT {$startpoint}, {$limit}");
foreach ($r as $n)
{
    $products = func_query_first("select * from $sql_tbl[orders] where status='Q'");
    $products = func_query_first("select product from $sql_tbl[order_details] where orderid=$n[orderid]");

    $infeed_counter++;

    // echo $manufacturer."=====";

    if ($row[avail] > 0)
        $avail = 'Y';
    else
        $avail = 'N';


    $_xml .= "<Order>";
    $_xml .= "<orderid>" . $n[orderid] . "</orderid>";
    $_xml .= "<login>" . htmlspecialchars(strip_tags(substr($n[login], 0, 50)), ENT_QUOTES) . "</login>";
    $_xml .= "<total>" . $n[total] . "</total>";
    $_xml .= "<product>" . $products[product] . "</product>";
    $_xml .= "</Order>";
}

$_xml .= "</order>";
$_xml .= "</Feed>";

header ("Content-Type:text/xml"); 
echo $_xml;

在XML Feed中访问URL,如下所示:

http://www.exampleyourdoamin/feed.php

答案 1 :(得分:0)

您需要使用一种称为长轮询的技术。长轮询使连接保持打开一段时间。在此期间,它会检查数据库中是否有任何更改。如果是,它会中断连接并将这些更改发送到前端。然后立即开始另一个连接。您可以在此处找到更多信息。 http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery