如何在XML中更改子标记顺序(由PHP生成)

时间:2011-09-23 06:10:47

标签: php xml

我在flash中有一个应用程序,其中多个用户可以登录并可以上传图像。当用户登录时,他可以查看他上传的图像,也可以查看其他用户上传的图像(仅限允许的图像)。因为我通过php生成xml文件。

请检查xml文件。

<images>
    <users user_name="Hardik">
        <image image_id="1316683023140" image_title="water" image_desc="water" image_path="all_users/Hardik/1316683023140.jpg" image_like="false"/>
        <image image_id="1316683057577" image_title="sunset" image_desc="sunset" image_path="all_users/Hardik/1316683057577.jpg" image_like="false"/>
        <image image_id="1316683115124" image_title="hills" image_desc="hills" image_path="all_users/Hardik/1316683115124.jpg" image_like="false"/>
        <image image_id="1316683713159" image_title="sun" image_desc="sun" image_path="all_users/Hardik/1316683713159.jpg" image_like="false"/>
        <image image_id="1316684544200" image_title="sun" image_desc="sun" image_path="all_users/Hardik/1316684544200.jpg" image_like="false"/>
        <image image_id="1316686014899" image_title="sun" image_desc="sun" image_path="all_users/Hardik/1316686014899.jpg" image_like="false"/>
        <image image_id="1316600184214" image_title="asd" image_desc="asd" image_path="all_users/Hardik/1316600184214.jpg" image_like="false"/>
        <image image_id="1316668356801" image_title="hello" image_desc="hello" image_path="all_users/Hardik/1316668356801.jpg" image_like="false"/>
        <image image_id="1316600221759" image_title="asd" image_desc="asd" image_path="all_users/Hardik/1316600221759.jpg" image_like="false"/>
        <image image_id="1316600193960" image_title="asd" image_desc="asd" image_path="all_users/Hardik/1316600193960.jpg" image_like="false"/>
        <image image_id="1316600172938" image_title="asd" image_desc="asd" image_path="all_users/Hardik/1316600172938.jpg" image_like="false"/>
        <image image_id="1316600144316" image_title="asd" image_desc="asd" image_path="all_users/Hardik/1316600144316.jpg" image_like="false"/>
        <image image_id="1316600173551" image_title="asd" image_desc="asd" image_path="all_users/Hardik/1316600173551.jpg" image_like="false"/>
        <image image_id="1316600177792" image_title="asd" image_desc="asd" image_path="all_users/Hardik/1316600177792.jpg" image_like="false"/>
        <image image_id="1316496700758" image_title="sunset" image_desc="sunset" image_path="all_users/Hardik/1316496700758.jpg" image_like="false"/>
        <image image_id="1316252181829" image_title="allow" image_desc="allow" image_path="all_users/Hardik/1316252181829.jpg" image_like="false"/>
        <image image_id="1316690195793" image_title="asasdas" image_desc="asdasd" image_path="all_users/Hardik/1316690195793.jpg" image_like="false"/>
        <image image_id="1316600153509" image_title="asd" image_desc="asd" image_path="all_users/Hardik/1316600153509.jpg" image_like="false"/>
        <image image_id="1316408901775" image_title="winter" image_desc="winter" image_path="all_users/Hardik/1316408901775.jpg" image_like="false"/>
    </users>
    <users user_name="raj">
            <image image_id="1315996252734" image_title="abc" image_desc="abc" image_path="all_users/raj/1315996252734.jpg" image_like="false"/>
    </users>
    <users user_name="sandip">
            <image image_id="1315996256153" image_title="abc" image_desc="abc" image_path="all_users/sandip/1315996256153.jpg" image_like="false"/>
    </users>
</images>

我的XML文件存在一个逻辑问题(由PHP生成)。现在我想更改子标签顺序。我希望在第一个位置拥有用户的子标签(已登录)。

说如果sandip登录他的孩子标签应该先来而不是硬件。

你可以在这里检查我的PHP代码

<?php
    require_once('connection.php');

    header('Content-type: text/xml');
    echo "<?xml version='1.0' encoding='UTF-8'?>";
    echo "<images>";

    $id='';
    $count=0;
    $result_1 = '';


    $query = "select  ui.userId as 'UI userId', ua.userName as 'UI userName', ui.imageId as 'UI ImageId', ul.imageId as 'UL ImageId' , ui.imageRights, ui.imagePath, ui.imageTitle, ui.imageDesc from user_account ua, (select * from user_images where userId=".$_REQUEST['userId']." or imageRights='Allow') as ui left join (select * from user_likes where userId=".$_REQUEST['userId'].") as ul on ui.imageId = ul.imageId where ua.userId=ui.userId order by ui.userId";

    $result_row = mysql_query($query);

    while($row = mysql_fetch_array($result_row, MYSQL_ASSOC))
    {
            if($id != $row['UI userId'])
            {
                if($count == 0)
                {
                    $result_1 .= "<users user_name='".$row['UI userName']."'>";
                }
                else
                {
                    $result_1 .= "</users>";
                    $result_1 .= "<users user_name='".$row['UI userName']."'>";
                }
                $id = $row['UI userId'];
            }

            $result_1 .= "<image image_id='".$row['UI ImageId']."' image_title='".$row['imageTitle']."' image_desc='".$row['imageDesc']."' image_path='".$row['imagePath']."' image_like='";

            if($row['UL ImageId'])
                $result_1 .= "true";
            else
                $result_1 .= "false";
            $result_1 .= "' />";

            $count++;

            if($count == mysql_num_rows($result_row))
                $result_1 .= "</users>";
    }

    echo $result_1;
    echo "</images>";
?>

如果你有更好的解决方案,请转发。

1 个答案:

答案 0 :(得分:1)

除了解决您的问题之外,我还建议您对PHP脚本进行一些修复:

$num_of_rows = mysql_num_rows($result_row); // Don't call this every loop it's very slow
$rows = [];
while($row = mysql_fetch_array($result_row, MYSQL_ASSOC)){
    if($id == $row['UI userId']){
        // Print the logged in user's images
    } else {
        $rows[] = $row;
    }
}

// Print here the rest of the results
foreach ($rows as $row) {
    if($id != $row['UI userId'])
    {
        if($count == 0)
        {
            $result_1 .= "<users user_name='".$row['UI userName']."'>";
        }
        else
        {
            $result_1 .= "</users>";
            $result_1 .= "<users user_name='".$row['UI userName']."'>";
        }
        $id = $row['UI userId'];
    }

    $result_1 .= "<image image_id='".$row['UI ImageId']."' image_title='".$row['imageTitle']."' image_desc='".$row['imageDesc']."' image_path='".$row['imagePath']."' image_like='";

    if($row['UL ImageId'])
        $result_1 .= "true";
    else
        $result_1 .= "false";
    $result_1 .= "' />";

    $count++;

    if($count == $num_of_rows)
        $result_1 .= "</users>";
}

echo $result_1;
echo "</images>";
?>