Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By mlseim

Thread: Need help with XML and PHP.

  1. #1
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Exclamation Need help with XML and PHP.

    searching using php.
    I have an xml file with these data.
    Code:
    <entries>
        
        <!-- Begin New York -->
        <entry>
            <id>1</id>
    		<title>418 Central Prk West</title>
    		<imageUrl>/sliderFeatured320x190_001.jpg</imageUrl>
            <imageThumbUrl>/listThumbApartments50x50_001.jpg</imageThumbUrl>
            <price>4,427</price>
            <address>418 Central Prk West, 105, Manhattan</address>
            <reviews>If you are searching for an apartment in Clementon or relocating to Blackwood, then come to Cherrywood and enjoy the best apartments in Clementon, NJ.</reviews>
            <latitude>40.795457</latitude>
    		<longitude>-73.962151</longitude>
            <featured>1</featured>
            <telNo>(877) 957-5246</telNo>
            <checkInTime>Today 3:00 am - 5:00 pm</checkInTime>
            <date>January 22, 2013</date>
            <categoryId></categoryId>
            <zipCode>10025</zipCode>
            <noOfRooms>3</noOfRooms>
            <bathrooms>2</bathrooms>
            <apartmentType>Condominium</apartmentType>
            <rating>1</rating>
        </entry>
        
        <entry>
            <id>2</id>
    		<title>66 E. 11 St.</title>
    		<imageUrl>/sliderFeatured320x190_002.jpg</imageUrl>
            <imageThumbUrl>/listThumbApartments50x50_002.jpg</imageThumbUrl>
            <price>4,591</price>
            <address>66 E. 11 St., Manhattan</address>
            <reviews>When we first walked to the apartments it was completely filthy...on the outside there was an old broken down vehicle next to our place and the door had ...</reviews>
            <latitude>40.733041</latitude>
    		<longitude>-73.992181</longitude>
            <featured>1</featured>
            <telNo>(310) 272-8002</telNo>
            <checkInTime>Today 4:00 am - 5:00 pm</checkInTime>
            <date>Febuary 26,2013</date>
            <categoryId></categoryId>
            <zipCode>10002</zipCode>
            <noOfRooms>20</noOfRooms>
            <bathrooms>10</bathrooms>
            <apartmentType>Condominium</apartmentType>
            <rating>2</rating>
        </entry>
    I just want to search for ID using value and then echo all the other elements. i am waiting for reply

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,546
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    With the script below, copy and upload as "test.php" or whatever you wish to call it.

    Put your actual XML URL in the script so it searches your XML file, and you can add other fields to output any way you wish, and using your own CSS layout.

    Here is the test script:
    Code:
    <?php  
    //
    
    if($_POST['id']){
    $id=$_POST['id'];
    }
    
    if($_POST['id']){
    // Enter the URL for your XML file or feed ...
    $feed_url = "http://www.yoursite.com/homeListings.xml";
    
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL,"$feed_url"); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0); 
    $xmlData = curl_exec($curl); 
    curl_close($curl); 
    
    $xml = simplexml_load_string($xmlData); 
    
    // Search for ID and display anything you wish about it.
    foreach ($xml->entry as $item) {
    	if($item->id == $id){
    	echo" 
    	<b>{$item->id}</b><br />
    	{$item->address}<br />
    	{$item->reviews}<br />
    	<br /><br /> 
    	"; 
    	}
    }
    }
    ?> 
    <html>
    <body>
    <form action="" method="post">
    Enter ID: <input type="text" name="id"> &nbsp;
    <input type="submit" name="submit" value="FIND ID">
    </form>
    </body>
    </html>
    samkhowaja likes this.

  • Users who have thanked mlseim for this post:

    samkhowaja (05-01-2014)

  • #3
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    5
    Thanked 0 Times in 0 Posts
    thanks for the help and can you tell me, how to change data in xml using the same situation search for id and change every data from the inputs.

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,546
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    I don't understand the question. Can you ask it in a different way or give an example?

  • Users who have thanked mlseim for this post:

    samkhowaja (05-09-2014)

  • #5
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I just want to modify the xml node for example I have to change value of title <title>418 Central Prk West</title> on the click of button...

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,546
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    Do you own the XML file? By that I mean, you have created it yourself, or have a database that is used to create the XML file?

    You want to modify an XML tag ... but for what reason? Your nodes each have a unique ID. Would it be easier to duplicate a node, allow someone to change anything within that node and swap the modified node with the original?

    But if you are generating the XML file from a MySQL database, that changes everything. It would be way easier to work with the MySQL database than the XML file. The fact you wish to modify the XML files tells me that you are storing it someplace on your website? Where does the XML file come from?

    And discuss the purpose of your XML file. If you were to use a MySQL database for all of your information, an XML file can be dynamically created from that database at any time. If you are providing an XML file as a feed for someone else (or another website), again, using a MySQL database is the best way to go (in my opinion).

    EDIT:
    And along with a dynamically created XML file, it could also do JSON, HTML and PDF output as well.

    .
    Last edited by mlseim; 05-02-2014 at 07:46 PM.

  • Users who have thanked mlseim for this post:

    samkhowaja (05-09-2014)

  • #7
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    5
    Thanked 0 Times in 0 Posts
    this is not created from sql or anything... i am just creating a admin panel to modify the data in xml, whenever needed..

    I am storing it on hosting with website. and i don't want to modify tag, i want to adjust values the tag is storing.

  • #8
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    5
    Thanked 0 Times in 0 Posts
    this is not created from sql or anything... i am just creating a admin panel to modify the data in xml, whenever needed..

    I am storing it on hosting with website. and i don't want to modify tag, i want to adjust values the tag is storing.

  • #9
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,546
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    Below is an example script of how to change one value in a specific node. In your case, you'll have to tell it which ID (node), which tag, and the new value.

    There are so many other things to consider here, I'm only listing the 'proof of concept'. It finds the correct node and changes the "reviews" to the new text. It then resaves it back, or to a different XML copy if you wish.

    You have to consider validation on all data being written (form validation), sanitizing and removing odd characters in the strings, and pretty much a whole script to be able to change any of the values. I don't have time to script all of that.

    Here is the 'proof of concept' (and I tested this on my own website) ...

    PHP Code:
    <?php  
    //

    if($_POST['id']){
    $id=$_POST['id'];
    }

    if(
    $_POST['id']){
        
    $review=$_POST['review'];
        
    // Enter your XML file  ...
    $xml_file="id.xml";

    $xmlData file_get_contents($xml_file);
    $xml = new SimpleXMLElement($xmlData);
    // here, you want to find the 'id' of your node and only change the value within that node.
    foreach( $xml->xpath("//id[.='".$id."']/parent::*") as $item ) {
      
    // in my example, I'm only changing the 'reviews' value ...
      
    $item->reviews $review;
    }

    // Save the changes
    $dom = new DOMDocument('1.0'); 
    $dom->preserveWhiteSpace false
    $dom->formatOutput true
    $dom->loadXML($xml->asXML()); 
    //echo $dom->saveXML();
    $dom->save($xml_file);

    }
    ?> 

    <html>
    <body>
    <form action="" method="post">
    Enter ID: <input type="text" name="id"> <br />
    Enter New Review: <input type="text" name="review"> <br />
    <input type="submit" name="submit" value="CHANGE IT">
    </form>
    </body>
    </html>
    Last edited by mlseim; 05-04-2014 at 08:13 PM.

  • Users who have thanked mlseim for this post:

    samkhowaja (05-09-2014)

  • #10
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    5
    Thanked 0 Times in 0 Posts
    thanks a lot bro.. It's working great. can you help with removal of entry. I want to remove complete entry where ID = 2.
    this one.
    <entry>
    <id>2</id>
    <title>66 E. 11 St.</title>
    <imageUrl>/sliderFeatured320x190_002.jpg</imageUrl>
    <imageThumbUrl>/listThumbApartments50x50_002.jpg</imageThumbUrl>
    <price>4,591</price>
    <address>66 E. 11 St., Manhattan</address>
    <reviews>When we first walked to the apartments it was completely filthy...on the outside there was an old broken down vehicle next to our place and the door had ...</reviews>
    <latitude>40.733041</latitude>
    <longitude>-73.992181</longitude>
    <featured>1</featured>
    <telNo>(310) 272-8002</telNo>
    <checkInTime>Today 4:00 am - 5:00 pm</checkInTime>
    <date>Febuary 26,2013</date>
    <categoryId></categoryId>
    <zipCode>10002</zipCode>
    <noOfRooms>20</noOfRooms>
    <bathrooms>10</bathrooms>
    <apartmentType>Condominium</apartmentType>
    <rating>2</rating>
    </entry>
    Thanks for the help you almost solved every problem

  • #11
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,546
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    Below is an example script of deleting a node by ID ...

    PHP Code:
    <?php
    //

    if($_POST['id']){
    $id=$_POST['id'];
    }

    if(
    $_POST['id']){
        
    // Enter your XML file  ...
    $xml_file="id.xml";
    $xml simplexml_load_file($xml_file);

    $ids=$xml->xpath("//id[.='".$id."']/parent::*");
    foreach(
    $ids as $item){
        if (
    $item->id == $id){
            
    $domRef dom_import_simplexml($item->id);
            
    $domRef->parentNode->parentNode-> removeChild($domRef->parentNode);
            
    $dom = new DOMDocument('1.0');
            
    $dom->preserveWhiteSpace false;
            
    $dom->formatOutput true;
            
    $dom->loadXML($xml->asXML());
            
    $dom->save($xml_file);
            break;
        }
    }
    // foreach
    }// if post id
    ?> 

    <html>
    <body>
    <form action="" method="post">
    Enter ID to DELETE: <input type="text" name="id"> &nbsp;
    <input type="submit" name="submit" value="DELETE IT">
    </form>
    </body>
    </html>
    Last edited by mlseim; 05-10-2014 at 03:14 AM.

  • Users who have thanked mlseim for this post:

    samkhowaja (05-10-2014)

  • #12
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    5
    Thanked 0 Times in 0 Posts
    <a href="javascript:void(0);" onclick="boxAlert();"><b>remove</b></a></li>
    I am using msgbox using javascript function for confirmation, while removing entry.
    function boxAlert() {
    var x;
    if (confirm("Are you sure you want to remove this entry!") == true) {

    "here i want to use post method and send data to the result.php where removal code will be executed"

    } else {
    x = "You pressed Cancel!";
    }
    document.getElementById("demo").innerHTML = x;
    }
    </script>
    I want this to execute on the next page. when the ok button is clicked

    <?php
    //

    if($_POST['id']){
    $id=$_POST['id'];
    }

    if($_POST['id']){

    // Enter your XML file ...
    $xml_file="id.xml";
    $xml = simplexml_load_file($xml_file);

    $ids=$xml->xpath("//id[.='".$id."']/parent::*");
    foreach($ids as $item){
    if ($item->id == $id){
    $domRef = dom_import_simplexml($item->id);
    $domRef->parentNode->parentNode-> removeChild($domRef->parentNode);
    $dom = new DOMDocument('1.0');
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
    $dom->loadXML($xml->asXML());
    $dom->save($xml_file);
    break;
    }
    }// foreach
    }// if post id
    ?>
    any method to do this.. will be appreciated.

    2nd problem.
    I also want to add new entry to the xml by checking the last ID if it is 25 then for the new entry ID will be 26.. can you help me with that too ?

  • #13
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,546
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    If the confirmation is good, javascript has to post to the PHP script ... giving it the ID. The PHP script can return with some sort of status.

    I think I need to know more about your javascript, especially the part where they first enter the ID (or click on it) and it then displays the confirmation.

    On the delete part ... if you delete a node, you'll have some skips between ID numbers. Do you never want to re-use and ID number, or would you like to insert a new node into the same place as a deleted node, re-using the ID number?

    And just so you know ... I don't mind helping out with the scripts. I'm actually learning it as I go. Now, I'm getting into things that require me to duplicate all of your scripts in order to test my scripts. I can't just make an example of deleting a node because now there's all kinds of javascripting confirmation and web pages involved. I'm not going to have time to duplicate your website so I can test what I do. I hate to post something that isn't tested ... I'm not that good of a programmer. In fact, everything I've been posting so far has been scripting I've found using Google. I'm learning new things every day.
    Last edited by mlseim; 05-10-2014 at 11:07 PM.

  • #14
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Here is the result.php.. after search i am getting to this.. and I used edit and remove button. On remove i am using function.
    Code:
    http://www.mediafire.com/view/8yda82orz8du24r/result.php
    it's ok to skip deleted id..
    Last edited by samkhowaja; 05-11-2014 at 01:56 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •