Solved

php variable to xml

Posted on 2011-03-02
7
431 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This video teaches users how to migrate an existing Wordpress website to a new domain.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 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

22 Experts available now in Live!

Get 1:1 Help Now