PHP in URL to locate specific node in XML db - php

i'm totally new to XML, but am trying it out on a site of mine to iterate through a very easy db. what i can't figure out is how to post a link outside of the site to a specific node in the xml database. the site in question is (http://thenewsomething.com)
please forgive any obvious ignorance as this is my first attempt with xml. what i'd like to do is post a url elsewhere that will link to thenewsomething.com with one of the specific nodes. for instance, if i give each node an id, can i do something like
http://thenewsomething.com?id=5
? thanks for your help in advance!

Sure. In PHP, you can look at $_GET['id'] to read out the ID passed to your script by the URL. (Make sure you escape the input by using something like addslashes). If your script is called index.php and someone browses to index.php?id=5, you can do this:
<?php
$id = addslashes($_GET['id']);
#use code tailored to your specific database to fetch the node with this ID
#display it
?>
You can look into something like SimpleXML and XPath to search within your XML database. For a quick introduction to XPath, consider:
http://www.w3schools.com/xpath/xpath_syntax.asp
If you post details of your XML schema, we may be able to help more.

Related

Extract from DB to XML

I have made a website where users can choose some regions on Google Maps, and save them into a database with a name an a description. What I need now is to be able to take the saved entries from my DB and show them also to an android app I have made with google maps. I am new to all these and from my research I understand that I should extract my data from the DB to an XML file and then parse them on the phone. For doing the first part I should use the following code, if I am not mistaken:
SELECT column1, column2,..
FROM tablename
FOR XML PATH;
What I still dont get is where should I put that piece of code? In a new file? All by itself? And with that code the XML file will be created, or should I do something else too?
Thank you
Well since noone answered my question and I did find the solution I'll post it here.
The answer is via PHP, XMLWriter. There is a question here that helped but I wasnt able to find from the beginning since I didnt know what to search for.

Tagging system similar to the one used in this site

i need to implement a tagging system looking exactly like the one we are using now. I'm new to the web developing so i'm having a few problems and i would like to start from an example but i can't find one.
In particular i would need the frontend part that, i suppose, parse an xml received from the server and print it like those cool squares with the tag in it. I don't need something exactly like this, an example of something similar would suffice.
why do you use or supposed this data to be sent as xml ? do u save it in xml in the begining ? i suppose you should save your data in database . and create a table for every unique tag and then create a bridge bettween the posts table and the tag table containing every post with every tag linked to it .
Have you looked at the Tag plugin by CakeDC? Might save you some time.

How can I search using PHP through an XML file and display the results?

I am trying to build a very simple price comparison script.
Until now, I wrote a code that gets some product xml feeds from shops and with the help of XSLT I create a single-global xml of all those input XMLs. I use the XSLT because the shops have different names for elements.
Now I want to take it one step further and I want to create a search form that will display me the products let's say I have the term "laptop".
I know how to create a form, but I need a coding guidance to understand how to make it to search in my XML file (products.xml) and display let's say the
Thank you
You might want to check out http://php.net/manual/en/class.xmlreader.php
Using that it is pretty easy to navigate through an XML file and grab all the info you need.
EDIT:
On second thought, http://php.net/manual/en/book.simplexml.php is a MUCH simpler way to achieve what you're trying to do. Hence the name, I guess ;)
You can use SimpleXML library to parse your xml file. In my opinion SimpleXML is easier to use than xmlreader. Though SimpleXML is introduced on php5.

Upload XML feed to mysql using PHP

I have an XML feed coming in:
<?xml version="1.0" encoding="UTF-8"?><product>
<name>John</name>
<contact_email>john#johnson.com</contact_email>
<contact_telephone>01234 567</contact_telephone>
<url>www.johnsone.com.com</url></product>
I need to get this loaded to MySQL using php - have seen a few examples but all of them take a file saved locally.
My feed is taken from the internet so changes all of the time. Does anybody have any suggestions where to start?
Thanks
First you'll need to define a data model. Then you'll need an xml parser to parse the feed, extract the data and populate your data model. Then you'll need to pass your model object to a DAO which writes the data to your database.
If it's taken from the internet you can just do
<?php
$feedData = file_get_contents('http://mywebsite/myfeed.xml');
?>
if you want to parse the data and store then
create a table for product
parse the xml and get the fields
insert into db or update existing data
so, what's your problem?
To be a bit more specific:
You'll obviously need to set up a table and database structure. I'm going to assume you have, or at least can figure out how to, set this up, and how to write to a database. If not, there are plenty of tutorials on that that should be plenty helpful. You'll need to use PHP's built-in MySQL library.
For parsing the XML you will probably want to use SimpleXML. It's not clear how your feed is coming in, but SimpleXML has the simplexml_load_string function that will let you pass it a string containing an XML document, however you get it, for parsing.
From there, you can just take the parsed XML and write it to your database. Any examples that use SimpleXML with a local file should be pretty easy to adapt using simplexml_load_string instead of simplexml_load_file, and doing whatever you're already (presumably) doing to get the data from this feed.

read an xml file into database

I want to store the contents of an xml file in the database. Is there an easy way to do it?
Can i write some script that can do the task for me?
The schema of the XML file looks like this:
<schedule start="20100727120000 +0530" stop="20100727160000 +0530" ch_id="0210.CHNAME.in">
<title>Title_info</title>
<date>20100727</date>
<category>cat_02</category>
</schedule>
One thing to note is:
How do I read the start time? I need the time +0530 added to the time?
Thank you so much.
You'll probably want to create a table called schedules that matches your data, then read the contents of the XML file with an XML parser of your choice. SimpleXML might be the right tool for this job.
As for the dates, I recommend you try using the function date_parse_from_format().
look up simple_xml on the php page - off hand I'm not too hot on it, but basically you will end up with a loop which will add your data to an object eg:
$xml
and you will be able to call tags as such $xml->schedule->title $xml->schedule->date and $xml->schedule->category and you will be able to call attributes as such $xml->schedule[start] but you might wanna check that.
I had to do this recently for a client, and this was the best way I could find. The attributes may be tricky - I can't quite remember but you might have to look into namespaces and such... anyway, find simple_xml and you're on the right tracks.

Categories