Jump to content

syn3rgy

Member
  • Posts

    5
  • Joined

  • Last visited

Posts posted by syn3rgy

  1. For free why not just try it? I am new to PHP but the first thing I did was download Eclipse and load the color theme plug-in and the proj-x transfer plug-in. So now it looks like Notepad++ but I get code templates (like auto-completion), syntax checking, and with a key stroke I can upload my files to the FTP server. Its also pretty cool to have a function or class in a referenced include and when you instantiate it in, it will find, auto-complete and show you the parameters. Its like having a perfect memory. For OOP I can't imagine not having that sort of functionality.

  2. I cant seem to get preg_replace to work on an array that contains two strings. var_dump of the array $catarray looks like this:

     

    
    array(2) { [0]=> string(407) "category1\ncategory2\ncategory3\ncategory4" [1]=> string(1026) "category1\ncategory2\ncategory3\ncategory4\ncategory5" }
    
    

     

    I am trying to eliminate the line breaks "\n" and replace them with ",". The code I wrote to try this is:

     

    
    $test = preg_replace("(\n)",",",$catarray);
    var_dump($test);
    
    

     

    The dump looks exactly the same as the before I ran the preg_replace. If I run the code on a simple string like "category1\ncategory2\ncategory3\ncategory4" it works as expected. From what I understand about preg_replace it works on arrays, what am I missing?

     

    Again any help would be great, I have exhausted my Google skills on this one.

     

    Thanks for the time,

    Benjamin

  3. Wow doh!:rolleyes:

     

    I have no idea why I didn't think of laying out the array that way.

     

    This is what I have now:

     

    $file = "rsform.xml";
    
    $xml = simplexml_load_file($file);
    
    $i = 0;
    
    //get data from XML tags
    
    foreach($xml->submissions->submission as $item)
    {
    //store those in array for later
    $basic[$i]["name"] = (string)$item->name;
    $basic[$i]["email"] = (string)$item->email;
    $basic[$i]["address"] = (string)$item->address;
    $basic[$i]["city"] = (string)$item->city;
    $basic[$i]["state"] = (string)$item->state;
    $basic[$i]["zip"] = (string)$item->zip;
    $basic[$i]["phone"] = (string)$item->phone;
    $basic[$i]["fax"] = (string)$item->fax;
    $basic[$i]["website"] = (string)$item->website;
    
    //increment the index
    ++$i;
    
    }
    var_dump($basic);
    

     

    Which of course dumps an array with an array inside at index 0 and 1.

     

    array(2) { [0]=> array(9) { ["name"]=> string(8) "Joe Mama" ["email"]=> string(14) "enter@here.com" ["address"]=> string(10) "1234 there" ["city"]=> string(10) "Thereville" ["state"]=> string(2) "IL" ["zip"]=> string(5) "55566" ["phone"]=> string(18) "555-555-1234 x1234" ["fax"]=> string(12) "555-555-5678" ["website"]=> string(7) "http://" } [1]=> array(9) { ["name"]=> string(9) "Test Item" ["email"]=> string(14) "enter@here.com" ["address"]=> string(10) "1234 there" ["city"]=> string(10) "Thereville" ["state"]=> string(2) "IL" ["zip"]=> string(5) "55566" ["phone"]=> string(18) "555-555-1234 x1234" ["fax"]=> string(12) "555-555-5678" ["website"]=> string(7) "http://" } }

     

    Again thanks, I will continue on my script and post back if I need more help. A week into PHP and its already beating me up!

     

    The solution is to use a 2D array -- an array that contains multiple arrays. For example, the first set of values would be:

     

    $basic[0]["name"]

    $basic[0]["email"]

    ...etc

     

    and the second record would be

     

    $basic[1]["name"]

    $basic[1]["email"]

    ...etc

     

    You would need to create a temporary variable that started at "0" and 1 was added to the value every time PHP loops through the foreach array (I'll call it $i for the sake of this example.) With that in place, you could do something like this:

     

    $basic[$i]["name"] = (string)$item->name;

    $basic[$i]["email"] = (string)$item->email;

    ...etc

     

    Do a Google search or two on "2d arrays" if you're having trouble. I can put together a basic example a bit later today when I have the time.

     

    This site has a bit more about 2D arrays (near the bottom of the page): http://www.learnphp-tutorial.com/Arrays.cfm

  4. As I have been going through the tutorials, I wanted to try and use what I'm learning.I came across the need to export values from an XML file to a CSV file. Looking at examples in the PHP manual and trying to use what I have been learning I have started the script.

     

    The XML layout looks like this:

     

    <?xml version="1.0" encoding="utf-8"?>
    <form>
    <title><![CDATA[submit Directory Profile Information]]></title>
    <submissions>
    <submission>
    <name><![CDATA[Joe Mama]]></name>
    <email><![CDATA[enter@here.com]]></email>
    <address><![CDATA[1234 there]]></address>
    <city><![CDATA[Thereville]]></city>
    <state><![CDATA[iL]]></state>
    <zip>55566</zip>
    <phone><![CDATA[555-555-1234 x1234]]></phone>
    <fax><![CDATA[555-555-5678]]></fax>
    <website><![CDATA[http://]]></website>
    </submission>
    <submission>
    <name><![CDATA[Test Item]]></name>
    <email><![CDATA[enter@here.com]]></email>
    <address><![CDATA[1234 there]]></address>
    <city><![CDATA[Thereville]]></city>
    <state><![CDATA[iL]]></state>
    <zip>55566</zip>
    <phone><![CDATA[555-555-1234 x1234]]></phone>
    <fax><![CDATA[555-555-5678]]></fax>
    <website><![CDATA[http://]]></website>
    </submission>
    </submissions>
    </form>
    

     

    The beginning of my code looks like this:

     

    
    $file = "mystuff.xml";
    
    $xml = simplexml_load_file($file);
    
    //get data from XML tags
    
    foreach($xml->submissions->submission as $item)
    {
    //store those in array
    $basic = array(
    	"name" => (string)$item->name,
    	"email" => (string)$item->email,
    	"address" => (string)$item->address,
    	"city" => (string)$item->city,
    	"state" => (string)$item->state,
    	"zip" => (string)$item->zip,
    	"phone" => (string)$item->phone,
    	"fax" => (string)$item->fax,
    	"website" => (string)$item->website
    );
    
    

     

    Not very far and my first problem appears. I understand why it happens I just don't know how to fix it. For every foreach loop the result will just overwrite the the first one. So if I vardump outside the loop you get one submission, not both. I'm stuck because I can't figure out how to get the results from each loop outside the loop, if that makes sense. Either its not very easy to do, or I am just missing something simple, either way if I can get some help with that I can move one to the next part.

     

    Thanks for the time,

    benjmain

  5. Hi All,

     

    I've watched the tutorial which shows how we can return the first 5 records from XML data. It all makes sense, however, what I would like to do is return specific data. See attachment...

     

    phonebook.xml

     

    I would like to return the record where <name></name> = Stan including all other elements relating to the record (number, address and email), But how would I code this in simplexml?

     

    I assume it will be similar to the tutorial with some kind of arguement but I am well and truely stuck here...

     

    Thanks

     

    Andy

     

    First off let me tell you I am an absolute PHP beginner. I have a similar need and just so happen to be also working off the same tutorial. In your example, to get the information in the tags you can do this:

     

    <?php
    
    $file = "phonebook.xml";
    
    $xml = simplexml_load_file($file);
    
    //get data from XML tags and store in array
    
    foreach($xml->note as $item) {
    
    $info = array(
    	"name" => $item->name,
    	"number" => $item->number,
    	"address" => $item->address,
    	"email" => $item->email
    );
    
    //simple echo out
    echo "<br />";
    print $info["name"];
    echo "<br />";
    print $info["number"];
    echo "<br />";
    print $info["address"];
    echo "<br />";
    print $info["email"];
    echo "<br />";
    
    }
    ?>
    

     

    If you want to test with this, just put your php and xml file in the same directory and run it. That's at least one way to access the data in the tags, after you get it I'm not sure how to deal with it. I wont hijack your thread, so I will start another one with my question. Hope this helps.

×
×
  • Create New...