tesfa koli
tesfa koli

Reputation: 755

Writing out database table to xml

Firstly, I do not want an alternative code, I only want help to make this work. Using the code below I want to write out the data from the database table to the xml file but I am only getting the "john" and "123" with out the correct xml tags and my second foreach() doesnt work, but the first foreach() to display the table works.

<?php

$dbhandle = sqlite_popen("propertydb", 0666, $err_msg);
if(!$dbhandle) die("Could not open the database");

$query = "CREATE TABLE property(pCode VARCHAR(8), price VARCHAR(6), 
iFile VARCHAR(15),          visits VARCHAR(3))";
if(!sqlite_query($dbhandle, $query)){ echo "table not created (maybe already
created)";
} 

$query = sqlite_query($dbhandle, 'SELECT * FROM property');  
$result = sqlite_fetch_all($query, SQLITE_ASSOC);  

foreach ($result as $arow) {
echo '<br> Postcode:  ' . $arow['pCode'] . '  Price:  ' . $arow['price'] . 
' Image  File:  ' . $arow['iFile'] . ' Visits:  ' . $arow['visits'];
}

$outfilepointer = fopen("property.xml","w");
$outtext = "<estateagent> \n";
$outtext .= "<agentname>john</agentname> \n";
$outtext .= "<agentphone>123</agentphone> \n";
foreach ($result->property as $oneproperty)
{
$propertyname = $oneproperty->propertyname;
outtext .= '<property><postcode> $arow["pCode"] </postcode>
<price> $arow["price"]</price> <imagefile> $arow["iFile"] </imagefile> <visits>
$arow["visits"] 
</visits>    </property> \n';
}
$outtex .= "</estateagent>";
fwrite($outfilepointer, $outtext);
fclose($outfilepointer);
sqlite_close($dbhandle);
?>

This is what my output should be...

<estateagent>
<agentname></agentname>
<agentphone></agentphone>
<property>
<postcode></postcode><price></price><imagefile></imagefile><visits></visits>
</property>
<property>...</property>
<property>...</property>
</estateagent>

Upvotes: 0

Views: 215

Answers (1)

F21
F21

Reputation: 33421

I just tried using your code and the XML tags do show up. Try opening it in notepad to see.

As for your foreach loop, you need to concatenate your strings.

outtext .= '<property><postcode> $arow["pCode"] </postcode> 
<price> $arow["price"]</price> <imagefile> $arow["iFile"] </imagefile> <visits> 
$arow["visits"]  
</visits>    </property> \n'; 

For the foreach, my understanding is that sqlite_fetch_all returns an array. You are treating it as a variable.

Try using: foreach ($result as $oneproperty). As I am not sure how your database is structured, if that does not work for you, post the output of $result using var_dump($result).

The above means that the literal string will be outputted and no variables will be inserted. You need to do this:

outtext .= '<property><postcode>'. $arow["pCode"]. '</postcode> 
<price>'. $arow["price"].'</price> <imagefile>'. $arow["iFile"].' </imagefile> <visits>'. 
$arow["visits"].'
</visits>    </property> \n'; 

Finally, instead of creating your tags manually with strings, consider using the XML Writer.

Upvotes: 1

Related Questions