xml to mysql using php - urgent

Discussion in 'PHP' started by mianriz, Dec 20, 2006.

  1. #1
    Issue is have a "xml" file and I have created mysql database using php

    and fatch data from "xml" file. MY table and all data placed

    accordingly but I am facing problem with one atribute.

    <ToLet Version="1.0" Agent="abc" Password="123" Date="20040813">
     <Property>
      <Refnumber>1.00</Refnumber> 
      <Number>1a</Number> 
      <Street>Madina Town</Street> 
      <Address3>St Johns Wood</Address3> 
      <Address4>London</Address4> 
      <Postcode>NW6 1HY</Postcode> 
      <Area>St Johns Wood</Area> 
      <Type>Conversion</Type> 
      <Beds>2.00</Beds> 
      <Receps>1.00</Receps> 
      <Garage>False</Garage> 
      <Parking>False</Parking> 
      <Garden>False</Garden> 
      <Balcony>True</Balcony> 
      <Floor>G</Floor> 
      <Available>29/02/2004</Available> 
      <Rent Per="Week">200.00</Rent> 
      <Description>Delightful 2 bed flat in sought after location close to 
    
    all amenities</Description> 
      <Comments>Delightful 2 bed flat in sought after location close to all 
    
    amenities</Comments> 
      <Photo1>abc_1_1.jpg</Photo1> 
      <Photo2>abc_1_2.jpg</Photo2> 
      <Photo3>abc_1_3.jpg</Photo3> 
      <Photo4>abc_1_4.jpg</Photo4> 
      <Photo5>abc_1_5.jpg</Photo5> 
      <Photo6>abc_1_6.jpg</Photo6> 
      <Photo7>abc_1_7.jpg</Photo7> 
      <Photo8>abc_1_8.jpg</Photo8> 
      <Photo9>abc_1_9.jpg</Photo9> 
      <Map /> 
      <Movie /> 
      <Url>http://www.rman.co.uk</Url> 
     </Property>
     <Property>
      <Refnumber>2.00</Refnumber> 
      <Number>1b</Number> 
      <Street>Gulburg</Street> 
      <Address3>St Johns Wood</Address3> 
      <Address4>London</Address4> 
      <Postcode>NW8</Postcode> 
      <Area>St Johns Wood</Area> 
      <Type>Conversion</Type> 
      <Beds>2.00</Beds> 
      <Receps>1.00</Receps> 
      <Garage>False</Garage> 
      <Parking>False</Parking> 
      <Garden>False</Garden> 
      <Balcony>False</Balcony> 
      <Floor>1</Floor> 
      <Available>29/09/2004</Available> 
      <Rent Per="Week">250.00</Rent> 
      <Description>Delightful 3 bed flat in new conversion. Close to all 
    
    amenities</Description> 
      <Comments>Delightful 3 bed flat in new conversion. Close to all 
    
    amenities</Comments> 
      <Photo1>abc_2_1.jpg</Photo1> 
      <Photo2>abc_2_2.jpg</Photo2> 
      <Photo3>abc_2_3.jpg</Photo3> 
      <Photo4>abc_2_4.jpg</Photo4> 
      <Photo5>abc_2_5.jpg</Photo5> 
      <Photo6>abc_2_6.jpg</Photo6> 
      <Photo7>abc_2_7.jpg</Photo7> 
      <Photo8>abc_2_8.jpg</Photo8> 
      <Photo9>abc_2_9.jpg</Photo9> 
      <Map /> 
      <Movie /> 
      <Url /> 
     </Property>
    </ToLet>
    PHP:
    In this file all atribute readed because every thing same but one

    atribute <Rent Per="Week">200.00</Rent> in different method and

    <Rent Per="Week"> contian two conditions one is Rent per Week and other

    will Rent per month.
    <Rent Per="Week">200.00</Rent> in this lines we fatch 200.00 in our

    databse but we also want to fatch "week" in our database.

    We have write a php file to fatch data from XML to mySQL wich working

    fine instead of <Rent Per="Week"> issue.

    My PHP file code is as:

    <?php
    define("DB_HOST","localhost",TRUE); // database server name
    define("DB_USER","root",TRUE); // database user
    define("DB_PASS","1234",TRUE); // database user's password
    define("DB_NAME","xml",TRUE); // database name
    
    $TAG = "EXTRA";
    $id = 0;
    //Initialize the XML parser
    $parser=xml_parser_create();
    
    //Function to use at the start of an element
    function start($parser,$element_name,$element_attrs)
    {
    	global $TAG; 
    	
    	switch($element_name)
    	{
        	case "PROPERTY":
    		echo "<br />[Property]";
    		$TAG = "PROPERTY";
    		break;    	
    		case "REFNUMBER":
    		$TAG = "REFNUMBER";
    		break;		
    		case "NUMBER":
    		$TAG = "NUMBER";
    		break;    	
    		case "STREET":
    		$TAG = "STREET";
    		break;		
    		case "ADDRESS3":
    		$TAG = "ADDRESS3";
    		break;    	
    		case "ADDRESS4":
    		$TAG = "ADDRESS4";
    		break;		
    		case "POSTCODE":
    		$TAG = "POSTCODE";
    		break;    	
    		case "AREA":
    		$TAG = "AREA";
    		break;		
    		case "TYPE":
    		$TAG = "TYPE";
    		break;    	
    		case "BEDS":
    		$TAG = "BEDS";
    		break;		
    		case "RECEPS":
    		$TAG = "RECEPS";
    		break;    	
    		case "GARAGE":
    		$TAG = "GARAGE";
    		break;		
    		case "PARKING":
    		$TAG = "PARKING";
    		break;    	
    		case "GARDEN":
    		$TAG = "GARDEN";
    		break;		
    		case "BALCONY":
    		$TAG = "BALCONY";
    		break;
    		case "FLOOR":
    		$TAG = "FLOOR";
    		break;		
    		case "AVAILABLE":
    		$TAG = "AVAILABLE";
    		break;
    		case "RENT":
    		$TAG = "RENT";
    		break;		
    		case "DESCRIPTION":
    		$TAG = "DESCRIPTION";
    		break;
    		case "COMMENTS":
    		$TAG = "COMMENTS";
    		break;		
    		case "PHOTO1":
    		$TAG = "PHOTO1";
    		break;
    		case "PHOTO2":
    		$TAG = "PHOTO2";
    		break;		
    		case "PHOTO3":
    		$TAG = "PHOTO3";
    		break;
    		case "PHOTO4":
    		$TAG = "PHOTO4";
    		break;		
    		case "PHOTO5":
    		$TAG = "PHOTO5";
    		break;
    		case "PHOTO6":
    		$TAG = "PHOTO6";
    		break;		
    		case "PHOTO7":
    		$TAG = "PHOTO7";
    		break;
    		case "PHOTO8":
    		$TAG = "PHOTO8";
    		break;		
    		case "PHOTO9":
    		$TAG = "PHOTO9";
    		break;
    		case "MAP":
    		$TAG = "MAP";
    		break;		
    		case "MOVIE":
    		$TAG = "MOVIE";
    		break;
    		case "URL":
    		$TAG = "URL";
    		break;		
    		
    		default:
    		$TAG = "EXTRA";
        }
    }
    
    //Function to use at the end of an element
    function stop($parser,$element_name)
     {
     	 echo "";
      
     }
    
    //Function to use when finding character data
    function char($parser,$data)
    {
    	global $TAG; 
    	global $id;
    
    	require_once('mysql.class.php');
    
    	if( !isset($db_obj) ) {
    		$db_obj = new db; 
    	}
    		
    	$db_conect = $db_obj->connect();
    
    	if( $TAG != "EXTRA") {
    		
    		
    		echo "<br />$data <font color=#006600 
    
    size=2>[OK]</font>";
    		
    		
    		if($TAG == "PROPERTY") {
    			
    			$sql = $db_obj->query("select max(id) as id 
    
    from property ",$db_conect);
    			if( $rs = $db_obj->fetch_array($sql) ) {
    				$id = $rs['id'];
    			}
    			$id = $id + 1;
    		
    			$db_obj->query("insert into property (id) 
    
    values('$id') ",$db_conect);
    			
    		}
    		else if( $TAG == "REFNUMBER" || $TAG == "NUMBER" || 
    
    $TAG == "STREET" || $TAG == "ADDRESS3" || $TAG == "ADDRESS4" || 
    		$TAG == "POSTCODE" || $TAG == "AREA" || $TAG == "TYPE" 
    
    || $TAG == "BEDS" || $TAG == "RECEPS" || $TAG == "GARAGE" || 
    		$TAG == "PARKING" || $TAG == "GARDEN" || $TAG == 
    
    "BALCONY" || $TAG == "FLOOR" || $TAG == "AVAILABLE" || $TAG == "RENT"
    		|| $TAG == "DESCRIPTION" || $TAG == "COMMENTS" || $TAG 
    
    == "PHOTO1" || $TAG == "PHOTO2" || $TAG == "PHOTO3" || 
    		$TAG == "PHOTO4" || $TAG == "PHOTO5" || $TAG == 
    
    "PHOTO6" || $TAG == "PHOTO7" || $TAG == "PHOTO8" || $TAG == "PHOTO9" 
    		|| $TAG == "MAP" || $TAG == "MOVIE" || $TAG == "URL" ) 
    
    {
    		
    			$db_obj->query("update property set 
    
    $TAG='$data' where id='$id' ",$db_conect);
    		}
    		
    		$TAG = "EXTRA";
    		
    	}
    	
    }
    
    
    //Specify element handler
    xml_set_element_handler($parser,"start","stop");
    
    //Specify data handler
    xml_set_character_data_handler($parser,"char");
    
    //Open XML file
    $fp=fopen("rentman.xml","r");
    
    //Read data
    while ($data=fread($fp,4096))
      {
      xml_parse($parser,$data,feof($fp)) or 
      die (sprintf("XML Error: %s at line %d", 
      xml_error_string(xml_get_error_code($parser)),
      xml_get_current_line_number($parser)));
      }
    
    //Free the XML parser
    xml_parser_free($parser);
    
    ?>
    PHP:
    I also use a class for this purpose as below:

    <?php
    
    class db {
    
    
    	function connect() {
    		$die = false;
    		$link = @mysql_connect(DB_HOST, DB_USER,DB_PASS) or 
    
    ($die = true);
    				
    		if($die){
    					
    			echo '<h3>Database connection error!!!</h3>';
    			
    			echo 'A connection to the Database could not be 
    
    established.<br />';
    			echo 'Please check your username, password, 
    
    database name and host.<br />';
    			echo 'Also make sure <i>mysql.class.php</i> is 
    
    rightly configured!<br /><br />';
    			
    			exit();
    		}
    		
    		mysql_select_db(DB_NAME);
    		
    		return $link;
    	}
    
    	// Returns an array that corresponds to the fetched row
    	function fetch_array($query) {
    		$query = mysql_fetch_array($query,MYSQL_ASSOC);
    		return $query;
    	} 
    	// To Execute Sql Query
    	function query($sql,$res) {
    		$query = mysql_query($sql) or die(mysql_error());
    		return $query;
    	}
    	// Retrieves the number of rows from a result set
    	function num_rows($query) {
    		$query = mysql_num_rows($query);
    		return $query;
    	}
    	// Get the number of affected rows by the last INSERT, UPDATE 
    
    or DELETE query
    	function affected_rows($query) {
    		$query =   mysql_affected_rows();
    		return $query;
    	}
    	
    }
    ?>
    PHP:
    Now in this situation I also want to fatch data of this attribute <Rent

    Per="Week"> in my databse. In thi I want to store "week" in my databse.

    For this purpose please write any quote within my quote and let me know

    changes... I am waiting your reply
     
    mianriz, Dec 20, 2006 IP