if statement in DOM

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GoneNuts
Forum Newbie
Posts: 8
Joined: Sat May 24, 2008 9:18 am

if statement in DOM

Post 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.
Last edited by Benjamin on Sat Jul 03, 2010 9:46 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: if statement in DOM

Post by Weirdan »

Code: Select all

if (!in_array($fieldname, array('Region', 'Latitude', 'Longitude'))) {
   //...
}
GoneNuts
Forum Newbie
Posts: 8
Joined: Sat May 24, 2008 9:18 am

Re: if statement in DOM

Post by GoneNuts »

Thank You Very Much For Your Help!
GoneNuts
Forum Newbie
Posts: 8
Joined: Sat May 24, 2008 9:18 am

Re: if statement in DOM

Post 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.
GoneNuts
Forum Newbie
Posts: 8
Joined: Sat May 24, 2008 9:18 am

Re: if statement in DOM

Post by GoneNuts »

Dose anyone know how to stop these entries from being entered into my xml ?
GoneNuts
Forum Newbie
Posts: 8
Joined: Sat May 24, 2008 9:18 am

Re: if statement in DOM

Post 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?
Last edited by Weirdan on Thu Jul 08, 2010 9:52 am, edited 1 time in total.
Reason: added [syntax] tags
Post Reply