Page 1 of 1
if statement in DOM
Posted: Sat Jul 03, 2010 9:09 pm
by GoneNuts
Code: Select all
<?php
if(!$dbconnect = mysql_connect('localhost', 'root', 'root')) {
echo "Connection failed to the host 'localhost'.";
exit;
} // if
if (!mysql_select_db('restaurants')) {
echo "Cannot connect to database 'test'";
exit;
} // if
$table_id = 'AB';
$dbresult = mysql_query('SELECT `CompanyName`,`Address`, `Region`,`Phone` , `Latitude` , `Longitude` FROM `AB`');
$doc = new DomDocument('1.0');
$root = $doc->createElement('Restaurants');
$root = $doc->appendChild($root);
while($row = mysql_fetch_assoc($dbresult)){
$occ = $doc->createElement('Restaurant');
$occ = $root->appendChild($occ);
$occ->setAttribute('Region', $row['Region']);
$occ->setAttribute('Latitude', $row['Latitude']);
$occ->setAttribute('Longitude', $row['Longitude']);
foreach ($row as $fieldname => $fieldvalue){
if($fieldname != 'Region' || $fieldname != 'Latitude' || $fieldname != 'Longitude'){
$child = $doc->createElement($fieldname);
$child = $occ->appendChild($child);}
$value = $doc->createTextNode($fieldvalue);
$value = $child->appendChild($value);
}
}
$xml_string = $doc->saveXML();
echo $xml_string;
?>
if($fieldname != 'Region' || $fieldname != 'Latitude' || $fieldname != 'Longitude'){
What I trying to achieve is excluding the some of the data from the sql query so that I can add them as attributes please can someone help the data I am trying to exclude will not exclude from the elements.
Re: if statement in DOM
Posted: Sun Jul 04, 2010 8:02 am
by Weirdan
Code: Select all
if (!in_array($fieldname, array('Region', 'Latitude', 'Longitude'))) {
//...
}
Re: if statement in DOM
Posted: Sun Jul 04, 2010 1:16 pm
by GoneNuts
Thank You Very Much For Your Help!
Re: if statement in DOM
Posted: Mon Jul 05, 2010 6:35 pm
by GoneNuts
Hi I just noticed that the if statement doesn't allow the sections Region, Longitude, Latitude. Which is what I wanted but unfortunately that data from these fields are still entered but they have been inputted into other section tags.
<Restaurant Region="Aberdeen C" Latitude="57.1467635" Longitude="-2.105849"><CompanyName>The Stage Door Restaurant</CompanyName><Address>26 North Silver Street, Aberdeen, Aberdeen City AB10 1RL, United KingdomAberdeen C</Address><Phone>01224 642 11157.1467635-2.105849</Phone></Restaurant>
You can see Aberdeen C is entered right after the address and the longitude and latitude is entered after the phone number.
Re: if statement in DOM
Posted: Thu Jul 08, 2010 4:01 am
by GoneNuts
Dose anyone know how to stop these entries from being entered into my xml ?
Re: if statement in DOM
Posted: Thu Jul 08, 2010 9:50 am
by GoneNuts
Code: Select all
$table_id = 'AB';
$dbresult = mysql_query('SELECT `CompanyName`,`Address`, `Region`,`Phone` , `Latitude` , `Longitude` FROM `AB`');
$doc = new DomDocument('1.0');
$root = $doc->createElement('Restaurants');
$root = $doc->appendChild($root);
while($row = mysql_fetch_assoc($dbresult)){
$occ = $doc->createElement('Restaurant');
$occ = $root->appendChild($occ);
$occ->setAttribute('Region', $row['Region']);
$occ->setAttribute('Latitude', $row['Latitude']);
$occ->setAttribute('Longitude', $row['Longitude']);
foreach ($row as $fieldname => $fieldvalue){
if (!in_array($fieldname, array('Region', 'Latitude', 'Longitude'))) {
$child = $doc->createElement($fieldname);
$child = $occ->appendChild($child);}
if (!in_array($fieldname, array('Region', 'Latitude', 'Longitude'))) {
$value = $doc->createTextNode($fieldvalue);
$value = $child->appendChild($value);}
}
}
sorted it had to the if statement for fieldname and fieldvalue. Dose anyone have a better way of doing this?