Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

php variable to xml

Posted on 2011-03-02
7
Medium Priority
?
438 Views
Last Modified: 2012-05-11
I need to pass a variable in a link where I can search for a matching node (by id) in an xml file and display it
0
Comment
Question by:gvilla23
  • 4
  • 3
7 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35018723
OK, what is the question?  

To pass a variable in a link you would find the variable in $_GET.  To find a node in an XML file, you would probably use the SimpleXML class to create an object and use an iterator like foreach to walk the object, comparing the XML information to the value you found in $_GET.

Do you have any experience in PHP programming?
0
 

Author Comment

by:gvilla23
ID: 35019192
How would i compare the XML information to the value in $_GET, in a foreach loop
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35019237
Do you have some XML to test with?  If so, please post a small sample and tell me what you want to find inside the XML.  I'll show you the code.

Do you have experience in PHP programming?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:gvilla23
ID: 35019631
Thanks, I want to be able to grab the node by the id, from  somthing like  
<a href="somelink.com?id=1">

I have a little PHP experience


<?xml version="1.0" encoding="ISO-8859-1"?>

<popups>
<popup>
<id>1</id>
<title>SQUARE ENIX MEMBERS REWARDS 2009</title>
<img_loc>images/se_member/se_members_popup_2.jpg</img_loc>
<client>SQUARE ENIX</client>
<involvement>DESIGN, FROM CONCEPT TO FINAL PACKAGING</involvement>
</popup>

<popup>
<id>2</id>
<title>ADENALIN MISFITS</title>
<img_loc>images/adrenaline_misfits/adrenaline_misfits_popup_1.jpg</img_loc>
<client>KONAMI</client>
<involvement>KEYART DESIGN</involvement>
</popup>

</popups>
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 35019991
Thanks for posting the XML.  Here is the code.  You can see it in action at http://www.laprbass.com/RAY_temp_gvilla23.php
<?php // RAY_temp_gvilla23.php
error_reporting(E_ALL);
echo "<pre>";



// FROM THE POST AT EE
$xml = <<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>

<popups>
<popup>
<id>1</id>
<title>SQUARE ENIX MEMBERS REWARDS 2009</title>
<img_loc>images/se_member/se_members_popup_2.jpg</img_loc>
<client>SQUARE ENIX</client>
<involvement>DESIGN, FROM CONCEPT TO FINAL PACKAGING</involvement>
</popup>

<popup>
<id>2</id>
<title>ADENALIN MISFITS</title>
<img_loc>images/adrenaline_misfits/adrenaline_misfits_popup_1.jpg</img_loc>
<client>KONAMI</client>
<involvement>KEYART DESIGN</involvement>
</popup>

</popups>
XML;



// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO VISUALIZE THE OBJECT
// var_dump($obj);

// IF WE HAVE A GET ARGUMENT
if (!empty($_GET["id"]))
{
    // USE ONLY NUMBERS
    $id = preg_replace('#[^0-9]#', NULL, $_GET["id"]);

    // ITERATE OVER THE POPUP OBJECTS
    foreach ($obj->popup as $popup)
    {
        // FIND THE MATCHING ID(S)
        if ($popup->id == $id)
        {
            $t = (string)$popup->title;
            $c = (string)$popup->client;
            echo PHP_EOL . $t . ' ' . $c;
        }
    }
}

// PUT UP THE FORM TO RECEIVE THE ID WE WANT TO SEARCH
?>
<form>
ENTER THE ID NUMBER:
<input name="id" />
<input type="submit" />
</form>

Open in new window

0
 

Author Comment

by:gvilla23
ID: 35020334
That is purfect =), one comment, how would I load the xml externally
 for example   $objDOM->load("test.xml"); and not include it on the page.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35020402
You can use this function to load an external XML file http://php.net/manual/en/function.simplexml-load-file.php

Thanks for the points, ~Ray
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question