在PHP中使用MySQL查询创建XML结构

时间:2014-08-26 19:31:50

标签: php

我在表格中有两列,试图将行拉出来。我正在构建一个xml文件,并且无法按照我的要求获得正确的结构。

这是我的工作

<?php

if (!empty($_SERVER['REMOTE_USER'])) {
    $user = $_SERVER['REMOTE_USER'];
} else {
    $user = $_SERVER['HTTP_CLIENT_IP'];
}

//database configuration
$config['mysql_host'] = "localhost";
$config['mysql_user'] = "*****";
$config['mysql_pass'] = "*****";
$config['db_name']    = "*****";
$config['table_name'] = "user";

//connect to host
mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
//select database
@mysql_select_db($config['db_name']) or die( "Unable to select database");

$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$xml .= "<resources>";
$xml .= "\r\n";

//select all items in table
$sql = "SELECT activity_full FROM user where user = '$user'";
$sqlname = "SELECT name FROM user where user = '$user'";

$result = mysql_query($sql);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

$resultname = mysql_query($sqlname);
if (!$resultname) {
    die('Invalid query: ' . mysql_error());
}

if(mysql_num_rows($result)>0){

    while($result_array = mysql_fetch_assoc($result)){

        foreach($result_array as $key => $value){

            $xml .= "<item component=\"ComponentInfo{";

            $xml .= "$value";

            $xml .= "}\" drawable=\"";

            $result_array_name = mysql_fetch_assoc($resultname);
                foreach($result_array_name as $key => $valuename){

                    $xml .= "$valuename";

            }

            $xml .= "\" />";
            $xml .= "\r\n";
            $xml .= "\r\n";
        }
    }
}

$xml .= "</resources>";

//send the xml header to the browser
header ("Content-Type:text/xml");

//output the XML data
echo $xml;



file_put_contents("export/".$user."_appfilter.xml", $xml);

?>

产生这个

<item component="ComponentInfo{com.apps.aaa.roadside/com.apps.aaa.roadside.Splash}" drawable="aaa_roadside1" />

<item component="ComponentInfo{com.aaa.android.triptik/com.aaa.android.triptik.Bootstrap}" drawable="aaa_triptik" />

<item component="ComponentInfo{au.com.phil.abduction2.demo/au.com.phil.abduction2.demo.menus.PsymIntro}" drawable="abduction" />

<item component="ComponentInfo{au.com.phil.abduction2/au.com.phil.abduction2.menus.PsymIntro}" drawable="abduction" />

<item component="ComponentInfo{au.com.phil/au.com.phil.Intro}" drawable="abduction" />

我需要的是应用具有相同名称的标题和组项。喜欢这个

<!-- aaa_roadside1 -->
<item component="ComponentInfo{com.apps.aaa.roadside/com.apps.aaa.roadside.Splash}" drawable="aaa_roadside1" />

<!-- aaa_triptik -->
<item component="ComponentInfo{com.aaa.android.triptik/com.aaa.android.triptik.Bootstrap}" drawable="aaa_triptik" />

<!-- abduction -->
<item component="ComponentInfo{au.com.phil.abduction2.demo/au.com.phil.abduction2.demo.menus.PsymIntro}" drawable="abduction" />
<item component="ComponentInfo{au.com.phil.abduction2/au.com.phil.abduction2.menus.PsymIntro}" drawable="abduction" />
<item component="ComponentInfo{au.com.phil/au.com.phil.Intro}" drawable="abduction" />

1 个答案:

答案 0 :(得分:1)

Here's the solution没有所有MySQL的东西。

我已将其尽可能地翻译成您现有的代码,结果如下所示。它按原样工作,但由于我没有连接到你的数据库,我不能绝对肯定地说。

话虽如此,你真的真的需要停止使用mysql_*函数并开始使用PDOmysql。并在查询中使用绑定参数而不是插值。

<?php

if (!empty($_SERVER['REMOTE_USER'])) {
    $user = $_SERVER['REMOTE_USER'];
} else {
    $user = $_SERVER['HTTP_CLIENT_IP'];
}

//database configuration
$config['mysql_host'] = "localhost";
$config['mysql_user'] = "*****";
$config['mysql_pass'] = "*****";
$config['db_name']    = "*****";
$config['table_name'] = "user";

//connect to host
mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
//select database
@mysql_select_db($config['db_name']) or die( "Unable to select database");

$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$xml .= "<resources>";
$xml .= "\r\n";

//select all items in table
// get both columns in just one query
$sql = "SELECT activity_full, name FROM user where user = '$user'";

$result = mysql_query($sql);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

if(mysql_num_rows($result)>0){

    while($row = mysql_fetch_assoc($result)){

        if(!isset($previousRow) || !isset($previousRow["name"]) || $previousRow["name"] != $row["name"])
        {
            $xml .= "<!-- " . $row['name'] . " -->\r\n";
        }
        $xml .= "<item component=\"ComponentInfo{";

        $xml .= $row["activity_full"];

        $xml .= "}\" drawable=\"";

        $xml .= $row["name"];

        $xml .= "\" />";
        $xml .= "\r\n";
        $xml .= "\r\n";

        $previousRow = $row;
    }
}

$xml .= "</resources>";

//send the xml header to the browser
header ("Content-Type:text/xml");

//output the XML data
echo $xml;

file_put_contents("export/".$user."_appfilter.xml", $xml);

?>