/    Sign up×
Community /Pin to ProfileBookmark

XML and PHP help

Hi all,

This is my first post ?, and I am new to PHP!

I am looking for some information on how to edit an xml file, and then take the new file and update my mysql database.

My mysql database has different headers than the xml has, so that is why I need to edit the xml file.

Would this be possible?

Thanks in advance!

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 17.2011 — Not entirely sure what you want/need to do, but there's a good chance you'll want to make use of either the [url=http://php.net/simplexml]SimpleXML[/url] or [url=http://php.net/dom]DOM[/url] extension.
Copy linkTweet thisAlerts:
@alknew182authorApr 17.2011 — What I need to do is to take the info off of the xml file and configure it to be imported to my database. I would like this to happen every hour to keep the inventory up to date. Which option above would be my best option?

Thanks!
Copy linkTweet thisAlerts:
@NogDogApr 17.2011 — If you just need to read and parse the XML, probably SimpleXML. If you actually want to manipulate the data and change/overwrite the file, you'll need to use DOM.
Copy linkTweet thisAlerts:
@BelrickApr 17.2011 — It doesnt sound to me like you need to edit the xml file at all.

You have here two choices of where to store your data.

  • 1. Flat file such as in XML format.

  • 2. SQL


  • Why use both?
    Copy linkTweet thisAlerts:
    @CharlesApr 17.2011 — It's not pretty and not for the meek but it is light weight. I would consider using the XML Parser module especially if the XML file is rather large but only for version two.
    Copy linkTweet thisAlerts:
    @alknew182authorApr 18.2011 — It is not so much that I want to use both, but how i get my product data. I have a supplier that offers product information in an xml file. From there I would like to format the categories to be placed in to my shopping cart. So I need to change the headers of the xml file first. Then I need to link the new xml file to the mysql database so my inventory stays up to date.

    At least that is what I am trying to do ?

    Thanks
    Copy linkTweet thisAlerts:
    @NogDogApr 18.2011 — If I understand, then I don't see any need to change the XML file/data, just use the applicable names as needed when referring to the XML fields or the DB fields. A crude version might have something like:
    [code=php]
    $xml = simplexml_load_file($file);
    foreach($xml->item as $item) {
    $name = mysql_real_escape_string($item->name); // field names happen to match
    $price = (float) $item->unit_price; // fields happen to be different
    $qty = (int) $item->quantity; // different again
    $sql = "
    INSERT INTO table_name (name, price, qty)
    VALUES ('$name', $price, $qty)
    ";
    mysql_query($sql);
    }
    [/code]
    Copy linkTweet thisAlerts:
    @alknew182authorApr 18.2011 — Thanks NogDog!

    I think that would work, but I have a few more questions about the above code. So if the category in the xml file was <productname>, but in my mysql database the product name is pName then I can use $productname = (float) $item->pName; ?

    Also another twist what if I wanted to do some math for the price output. So for example the price of a product is $10 but I want to add 25&#37; to that then have it upload into the database how would I go about doing that?

    Thanks again!
    Copy linkTweet thisAlerts:
    @NogDogApr 18.2011 — Frankly, it doesn't even matter what you call the PHP variable name, as long as you use it in the correct position in your SQL value() list. And certainly you can do whatever you want with the data before you use it in your query.
    Copy linkTweet thisAlerts:
    @alknew182authorApr 22.2011 — So I am still having trouble.

    I don't understand how to edit the data before it is placed into the db. I have looked through xml dom, but it is not clicking.

    So for example:

    Here is the xml file:

    <item>

    <price>14.59</price>

    <name>gun holster</name>

    <man>galco</man>

    <Picture>something.img</Picture>

    </item>

    Now I need to change each of the data i each of the fields.

    So for the price I need to take the 14.59 and add 25&#37; to it. So 18.68 is the amount I want to be place into the db.

    For the name I need to add the manufacture (<man>) to the front of the name. So the name would then become galco gun holster.

    For the picture I need to add prodimages/ before the image file. So it would be prodimages/something.img

    So how would I go about doing all or these different changes?

    I was thinking that It might be easier to take the xml file and create a new xml file. That way everything is sorted out before I upload it to the db. Then I could change the names of the new xml file to import to the database.

    Maybe I am thinking about this the wrong way, and hopefully someone can help me ?

    Thanks again!
    Copy linkTweet thisAlerts:
    @BelrickApr 23.2011 — Once again why alter the XML file?

    Are you sure youve thought things logically through?

    Why not take the price from the xml file, increase by 25&#37; then save to the database leaving the XML file as is?

    Same for the manufacturer. Take the two parts, merge together then save to one SQL field.
    Copy linkTweet thisAlerts:
    @alknew182authorApr 25.2011 — The problem is I do not know how to add the 25% or merge them together. The script I am working on will automatically download the xml file then import the information to the db. So what code am I missing to change the fields I want to change?
    ×

    Success!

    Help @alknew182 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...