I am working on an app which requires me to get some data from my database and export the data as xml. The data I am retrieving is from a wordpress blog including: ID, title, post, date and category, where one post can belong to one or many categorys and ID is unique to each post. I have a php which will query my database and get all of this info
SELECT wp_posts.post_content, wp_posts.post_title, wp_posts.post_excerpt, wp_posts.ID, wp_term_relationships.term_taxonomy_id FROM wp_posts, wp_term_relationships WHERE wp_posts.ID = wp_term_relationships.object_id HAVING distance <50 ORDER BY distance, wp_posts.ID;
and is put into xml with the following:
$XML = "<?xml version='1.0' encoding='utf-8'?>\n";
$XML = "<query_result>";
// rows
$index = 1;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$XML .= "<row id='".$index."'>";
$i = 0;
// cells
foreach ($row as $cell) {
// Escaping illegal characters
$cell = htmlspecialchars($cell, ENT_QUOTES, "UTF-8");
$col_name = mysql_field_name($result,$i);
// creates the "<tag>contents</tag>" representing the column
$XML .= "<" . $col_name . ">" . $cell . "</" . $col_name . ">";
$i++;
}
$XML .= "</row>";
$index = $index + 1;
}
$XML .= "</query_result>";
echo $XML;
however this query is giving me duplicate results for each post which belongs to more than one category.
What I need is a way to output this information which will give me one result for each post with a list of categories which each post belongs to.
What would be the most effective way to handle this?
No comments:
Post a Comment