0

I recently asked this question, XML structure while creating from MySQL query in PHP. I got the answer I needed but now I have a similar case that is missing just one thing.

I duplicated the code and changed as needed but the category rows are not working.

Here is my code

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

//select all items in table
$sql2 = "SELECT distinct category, name FROM user where user = '$user' and category is not null order by category";

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

if(mysql_num_rows($result2)>0){

    while($row2 = mysql_fetch_assoc($result2)){

        if(!isset($previousRow2) || !isset($previousRow2["category"]) || $previousRow2["category"] != $row2["category"])
        {
            $xml2 .= "\r\n";
            $xml2 .= "<category title=\"" . $row["category"] . "\" />\r\n";
        }
        $xml2 .= "<item drawable=\"";

        $xml2 .= $row2["name"];

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


        $previousRow2 = $row2;
    }
}

$xml2 .= "</resources>";

This is outputting this

<category title="" />
<item drawable="bigdx_clean" />

<category title="" />
<item drawable="bluetooth" />
<item drawable="bluetooth_audio" />
<item drawable="browser" />
<item drawable="calculator" />
<item drawable="calendar" />
<item drawable="call_history" />
<item drawable="camera" />

The titles are blank though.

I'm using the same concept of the working code from other question. It's a different query so I must have something wrong with it?

  $xml2 .= "<category title=\"" . $row["category"] . "\" />\r\n";
0

1 Answer 1

1

You are accessing $row["category"], but this variable is not defined anywhere. Did you mean $row2?

For what it's worth, $row2 isn't a very descriptive name for a variable. If you are looking for another variable name because $row is taken, perhaps you need to move this into another function, where $row will just be local?

If you are (inadvertently) accessing variables that do not exist, it is likely that your on-screen errors are disabled in your local PHP configuration. It is a good idea to turn these on to aid your development process - you'll find that setting in your php.ini configuration.

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

2 Comments

Thanks. i hate when i overlook these things
No probs. If you make the configuration change I suggest, you won't miss this in the future :).

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.