Solved

php variable to xml

Posted on 2011-03-02
7
432 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 108

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 108

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 108

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 108

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

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.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now