在PHP中从MySQL查询创建时的XML结构


XML structure while creating from MySQL query in 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" />

这是一个没有所有MySQL内容的解决方案。

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

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

<?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);
?>