0

I have 2 columns in a table im trying to pull rows out of. Im building an xml file and can't get the structure right with my requirements.

Here is what i have working

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

?>

That produces this

<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" />

What i need is to apply a header and group items with the same name. Like this

<!-- 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" />
10
  • And what exactly is the logic that dictates what the text of the header should be? Commented Aug 26, 2014 at 19:36
  • the name. I added query to the question. When name appears more than once the activity should be duplicated under Commented Aug 26, 2014 at 19:38
  • You still seem to be omitting relevant code here. Could you please post the actual code that you're running? Where are $result and $resultname defined? It looks like the name (the value that you are giving to the drawable attribute) can actually be a concatenation of multiple names. How do you want to handle that case? Commented Aug 26, 2014 at 19:42
  • i apologize. i have updated Commented Aug 26, 2014 at 19:53
  • Also, yes the name that is used for drawable is same that is used for header tag needed. Commented Aug 26, 2014 at 19:55

1 Answer 1

1

Here's the solution without all the MySQL stuff.

I've translated it as best I could into your existing code, resulting in the below. It should work as-is, but since I'm not connecting to your db, I can't say with absolute certainty.

That being said, you really, really need to stop using the mysql_* functions and start using either PDO or mysql. And use bound parameters in your queries instead of doing interpolation.

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

?>
Sign up to request clarification or add additional context in comments.

1 Comment

exactly what i needed. Thank you so much. Im sorry i wasn't more clear on my question but you nailed it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.