Solved

php variable to xml

Posted on 2011-03-02
7
433 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 109

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 109

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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 109

Accepted Solution

by:
Ray Paseur earned 500 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 109

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

776 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