php and mysql to xml - php

i have 2 tables, first one which stores restaurant info and second stores dishes served in each. They are linked using res_id.
1) info_main [id, res_id, res_name,res_pc]
2) dishes [id,dishName,price,res_id(Foreign key)]
My SQL query is
$query = "SELECT * FROM info_main LEFT JOIN dishes ON info_main.res_id = dishes.res_id";
I am inserting the results from the query into an xml file which works fine. Below is the code:
$query = "SELECT * FROM info_main LEFT JOIN dishes ON info_main.res_id = dishes.res_id";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = #mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<marker> ';
echo '<detail1>';
echo '<resdetails ';
echo 'name="' . parseToXML($row['res_name']) . '" ';
echo 'id="' . parseToXML($row['res_ID']) . '" ';
echo 'pc="' . parseToXML($row['res_pc'] ). '" ';
echo '/>';
echo '<dishdetails ';
echo 'name="' . parseToXML($row['dishName']) . '" ';
echo 'price="' . parseToXML($row['price']) . '" ';
echo '/>';
echo '</detail1>';
echo '</marker>';
}
This work fine however if a restaurant has 3 dishes in the database, then xml create 3 nodes: Something like this:
I want the xml structure like
<detail1>
<resdetails name="spoted dog" id="xyz" pc="xyz"/>
<dishdetails name="bean burger" price="1" />
<dishdetails name="cheese and tomato panini" price="3" />
<dishdetails name="veg salad" price="2" />
</details1>
I cant figure out a way how to achieve the above stated xml structure. Your help is greatly appreciated. Thanks

$query = "SELECT * FROM info_main LEFT JOIN dishes ON info_main.res_id = dishes.res_id";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
$resname = "";
while ($row = #mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
if ($resname!=$row['res_name']) {
//restname isn't populated or doesn't match current so output new headers
echo '<marker> ';
echo '<detail1>';
echo '<resdetails ';
echo 'name="' . parseToXML($row['res_name']) . '" ';
echo 'id="' . parseToXML($row['res_ID']) . '" ';
echo 'pc="' . parseToXML($row['res_pc'] ). '" ';
echo '/>';
}
//this bit needs to always happen
echo '<dishdetails ';
echo 'name="' . parseToXML($row['dishName']) . '" ';
echo 'price="' . parseToXML($row['price']) . '" ';
echo '/>';
if ($resname!=$row['res_name']) {
//restname isn't populated or doesn't match current so output new headers
echo '</detail1>';
echo '</marker>';
}
$resname = $row['res_name']; //set resname to this res_name as this is our check to see if we've already put out required headers for this item that way every change it'll put this back in
}
Something like this (note may need some tidying up)

For that structure just do this:
echo '<marker> ';
echo '<detail1>';
while ($row = #mysql_fetch_assoc($result)){
echo '<dishdetails ';
echo 'name="' . parseToXML($row['dishName']) . '" ';
echo 'price="' . parseToXML($row['price']) . '" ';
echo '/>';
}
echo '</detail1>';
echo '</marker>';
All that is really different, is that I moved a portion of your code out of the while loop.

Atlast i got that working, i used two query's, one getting distinct restarant and other getting dishname. i used loop in the main loop. below is the code.
$query = "SELECT DISTINCT * FROM info_main";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = #mysql_fetch_assoc($result)){
$id=$row['res_id'];
// ADD TO XML DOCUMENT NODE
echo '<marker> ';
echo '<detail1>';
echo '<resdetails ';
echo 'name="' . parseToXML($row['res_name']) . '" ';
echo 'id="' . parseToXML($id) . '" ';
echo 'pc="' . parseToXML($row['res_pc'] ). '" ';
echo '/>';
$dishes = "SELECT * FROM dishes WHERE res_id = '" . $id . "'";
$dish = mysql_query($dishes);
while ($row2 = #mysql_fetch_assoc($dish)){
$id2 = $row2['res_id'];
echo '<dishdetails ';
echo 'name="' . parseToXML($row2['dishName']) . '" ';
echo 'price="' . parseToXML($row2['price']) . '" ';
echo '/>';
}
echo '</detail1>';
echo '</marker>';
}
echo '</markers>';

Related

POST variable breaks the execution

Im trying to generate an xml with the code below, but I get this error message
without the POST, the xml is created normally.
require("conexao.php");
$variable = $_POST['cat1'];
$result_markers = "SELECT * FROM results where `cat` LIKE '
$variable'";
$resultado_markers = mysqli_query($link, $result_markers);
header("Content-type: text/xml");
echo '<markers>';
while ($row_markers = #mysqli_fetch_assoc($resultado_markers)){
echo '<marker ';
echo 'id="' . $row_markers['id'] . '" ';
echo 'name="' . parseToXML($row_markers['name']) . '" ';
echo '/>';
}
echo '</markers>';

How to select all columns except one?

How can I select all columns in my database except the one the user is on?
I want to have all data posted in my xml page, but not the line that shows the current users info
<?php
include_once 'test-dbconnect.php';
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
$sql = "select * from tbl_users WHERE username != '".$row['username']."'";
$result = mysqli_query($DBcon, $sql) or die("Error in Selecting " . mysqli_error($DBcon));
//create an array
$user_data = array();
while($row =mysqli_fetch_assoc($result))
{
$user_data[] = $row;
echo '<marker ';
echo 'name="' . parseToXML($row['username']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'latitude="' . $row['latitude'] . '" ';
echo 'longitude="' . $row['longitude'] . '" ';
echo '/>';
}
//close the db connection
mysqli_close($DBcon);
// End XML file
echo '</markers>';
?>

Sorting nested While() Loops

I'm able to sort the second tier while loop for obvious reasons but I cannot get the first one to sort. I know its cause the "for" loop is incrementing. What I want is alphabetically sort first while loop then the second ASC...any suggestions? Here's my code
function get_content() {
$sql1 = "SELECT * FROM category";
$res1 = mysql_query($sql1) or die(mysql_error());
$total = mysql_num_rows($res1) or die(mysql_error());
for($a = 1; $a <= $total; $a++) {
$sql = "SELECT * FROM weblinks INNER JOIN category ON category_weblinks = id_category WHERE id_category = '$a' AND status_weblinks = 'checked' ORDER BY title_weblinks ASC";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "\n\n\n" . '<div class="post">' . "\n";
echo '<div class="title">' . "\n";
echo '<h2><a name="' . $row['shortcut_category'] . '">' . $row['title_category'] . '</a></h2>' . "\n";
echo '<p><small>Posted by Joe email</small></p>';
echo '</div>' . "\n";
echo '<div class="entry">' . "\n";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "\n" . '<p><b>' .$row['title_weblinks']. '</b><br>' . "\n";
echo $row['description_weblinks']. '<br>' . "\n";
echo 'Link: ' .$row['link_weblinks']. '<br>' . "\n";
echo 'User: ' .$row['username_weblinks']. ' | Password: ' .$row['password_weblinks']. '</p>' . "\n";
}
echo '<p class="links"> Back to Top</p>';
echo '</div>';
echo '</div>';
}
}
}

Error on line 2 at column 10: xmlParseDocTypeDecl : no DOCTYPE name

Just got this error, have no idea why, can anyone help?
error on line 2 at column 10: xmlParseDocTypeDecl : no DOCTYPE name !
I am trying to take information from my database and output it into XML when I run it in the browser.
Here is my code
<?php
include 'header.php';
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
// Select all the rows in the Blog table
$query = "SELECT * FROM Blog WHERE 1";
$result = mysqli_query($con, $query);
if (!$result)
{
die('Invalid query: ' . mysqli_error($con));
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<blog>';
// Iterate through the rows, printing XML nodes for each
while ($row = #mysqli_fetch_assoc($result))
{
// ADD TO XML DOCUMENT NODE
echo '<blogs ';
echo 'ID="' . parseToXML($row['ID']) . '" ';
echo 'subject="' . parseToXML($row['subject']) . '" ';
echo 'content="' . parseToXML($row['content']) . '" ';
echo 'latitude="' . $row['latitude'] . '" ';
echo 'longitude="' . $row['longitude'] . '" ';
echo 'imageName="' . parseToXML($row['imageName']) . '" ';
echo 'datetime="' . parseToXML($row['datetime']) . '" ';
echo '/>';
}
// End XML file
echo '</blog>';
?>
Turns out no DOCTYPE name literally means, I had not named my DOCTYPE.
I had
<!DOCTYPE>
Where I should have had
<!DOCTYPE html>
Normally this would not cause a problem but since I was trying to output XML data everything has to be perfect in the Document Tree.

Extraction of the data from MySQL using PHP

$query = "SELECT * FROM `status_info_private` WHERE `id`=$id ORDER BY `Status_Date` DESC LIMIT 100";
if ($query_run = mysql_query($query)) {
while ($rows = mysql_fetch_array($query_run)) {
echo '<font color="#009900" > ' . $rows['Name'] . ' ' . ' Says :' . '</font><br/>';
echo '<p align="justify> ' . $rows['Private_status'] . '<br/>';
echo '<p align="right">' . $rows['Status_Date'] . '<br/>';
$like = $rows['Like'];
$unlike = $rows['Unlike'];
}
}
I think everything is correct in the piece of code. But still I am unable to get the output under the column titled as "Private_status". The above code is producing everything correctly except the message under cols "Private_status". I have already checked the spelling of the col name & there is no error in that part.
So, Please tell me what exactly is missing ?
first close your <p> tags and then do a print_r to check what is in $rows
..
Also, start using PDO or mysqli
$query = "SELECT * FROM `status_info_private` WHERE `id`=$id ORDER BY `Status_Date` DESC LIMIT 100";
if ($query_run = mysql_query($query)) {
while ($rows = mysql_fetch_array($query_run)) {
echo '<a href="view_profile.php?id=' . $id . '" color="#009900" > ' . $rows['Name'] . ' ' . ' Says :' . '</a><br/>';
echo '<p align="justify"> ' . $rows['Private_status'] . '</p>';
echo '<p align="right">' . $rows['Status_Date'] . '</p>';
$like = $rows['Like'];
$unlike = $rows['Unlike'];
}
}

Categories