SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict
    Join Date
    Oct 2008
    Location
    Virtual World is my location
    Posts
    306
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question PHP & XML Problem

    Hello...

    I have been trying to fix a problem for the last 2 days...but have been unsuccessful.

    Here is what I need.

    I have an XML file:

    Code:
    <mainnode id="1A" offid="X13">
    
    <data>
    	<Sub1></Sub1>
      	<Sub2></Sub2>
    	<Sub3>
      		<SubSub1></SubSub1>
    		<SubSub2></SubSub2>
    		<SubSub3></SubSub3>
    		<SubSub4></SubSub4>
    	</Sub3>
    </data>
    <otherinfo></otherinfo>
    <userimages total="5">
    	<image url="theurl.com/node1image1.jpg" alttxt="imagealt" value="1" />
    	<image url="theurl.com/node1image2.jpg" alttxt="imagealt" value="1" />
    	<image url="theurl.com/node1image3.jpg" alttxt="imagealt" value="1" />
    	<image url="theurl.com/node1image4.jpg" alttxt="imagealt" value="1" />
    	<image url="theurl.com/node1image5.jpg" alttxt="imagealt" value="1" />
    </userimages>
    
    <generalinfo></generalinfo>
    </mainnode>
    Now using PHP for the entire XML file which would have say 100 odd items, run a loop and print only the image URLs for each mainnode.

    currently when I run the loop.. all the images are printed on a single line (for all 100 items.

    in this way -
    Code:
    node1image1.jpg node1image2.jpg node1image3.jpg node1image4.jpg node1image5.jpg node2image1.jpg node2image2.jpg node2image3.jpg node3image1.jpg node3image2.jpgnode3image3.jpgnode3image4.jpg
    what i want is say for first item there are 5 images so after 5 the loop breaks and on new line the next node images start.

    the printing should be like

    Code:
    node1image1.jpg node1image2.jpg node1image3.jpg node1image4.jpg node1image5.jpg 
    
    node2image1.jpg node2image2.jpg node2image3.jpg 
    
    node3image1.jpg node3image2.jpgnode3image3.jpgnode3image4.jpg
    Any ideas how it can be done.

    Thanks in advance
    RecipesNext.com - recipes, kitchen queries, videos & more...
    BargainNext.com - coupons / deals / bargains / offers & more...

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,396
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Post the code you have right now, the one that prints all images on one line

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You need to show us an XML sample with more than one grouping of the images in situ.

    Maybe this will help.
    PHP Code:
    <?php
    $string 
    '<mainnode id="1A" offid="X13">
        <user>
            <userimages total="5">
                <image url="theurl.com/node1image1.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image2.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image3.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image4.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image5.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image6.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image7.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image8.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image9.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image10.jpg" alttxt="imagealt" value="1" />
            </userimages>
        </user>
        <user>
            <userimages total="5">
                <image url="theurl.com/node1image1.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image2.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image3.jpg" alttxt="imagealt" value="1" />
            </userimages>
        </user>
        <user>
            <userimages total="5">
                <image url="theurl.com/node1image1.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image2.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image3.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image4.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image5.jpg" alttxt="imagealt" value="1" />
            </userimages>
        </user>
    </mainnode>'
    ;

    $xml = new SimpleXMLElement($string);
    foreach(
    $xml->user as $user){
        for(
    $i 0$i 5$i++){
            if(
    false === isset($user->userimages->image[$i])){
                break;
            }
            echo 
    $user->userimages->image[$i]['url'];
        }
        echo 
    '<br />';
    }
    /*
        theurl.com/node1image1.jpgtheurl.com/node1image2.jpgtheurl.com/node1image3.jpgtheurl.com/node1image4.jpgtheurl.com/node1image5.jpg
        theurl.com/node1image1.jpgtheurl.com/node1image2.jpgtheurl.com/node1image3.jpg
        theurl.com/node1image1.jpgtheurl.com/node1image2.jpgtheurl.com/node1image3.jpgtheurl.com/node1image4.jpgtheurl.com/node1image5.jpg
    */
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    SimpleXML & XPath
    PHP Code:
    $xml = new SimpleXMLElement($string);
    foreach(
    $xml->user as $user){
        foreach(
    $user->xpath('userimages/image[position() <= 5]/@url') as $url){
            echo 
    $url;
        }
        echo 
    '<br />';
    }
    /*
        theurl.com/node1image1.jpgtheurl.com/node1image2.jpgtheurl.com/node1image3.jpgtheurl.com/node1image4.jpgtheurl.com/node1image5.jpg
        theurl.com/node1image1.jpgtheurl.com/node1image2.jpgtheurl.com/node1image3.jpg
        theurl.com/node1image1.jpgtheurl.com/node1image2.jpgtheurl.com/node1image3.jpgtheurl.com/node1image4.jpgtheurl.com/node1image5.jpg
    */ 
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Addict
    Join Date
    Oct 2008
    Location
    Virtual World is my location
    Posts
    306
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question

    Hi Anthony,

    Thank you very much for your reply. But, its not working with my XML file.

    This is my current script, where I am able to get all images on a single line.

    Code:
    <?
    	$objDOM = new DOMDocument();
      	$objDOM->load("myxmlfile.xml"); 
    
    	$params = $objDOM->getElementsByTagName("image");
    	foreach( $params as $picture )
      	{
        		$theurl = $picture -> getAttribute('url');
    		echo "$theurl ";
    	}
    ?>
    now I could also get the images that should be there for that node.

    if you see my XML file which I have posted

    Code:
     <userimages total="5">
    Hence when the loop starts I could get the total images in that particular <userimages> section and break my statement, when that total is reached.

    Thats where I am stuck. I am not able to pass the total and apply a break statement where by it ends the function there and then again searches in the XML for the next <userimages> tag..in a loop

    Finally there is one more difference in the file you have created and the file I have.

    My data starts with
    Code:
    <mainnode> and ends with </mainnode>
    while ur xml starts with
    Code:
    <user> and ends with </user>

    Secondly My code does not use the
    Code:
    <user>
    tag ... it starts with userimages directly...

    any help?

    Thanks

    Quote Originally Posted by AnthonySterling View Post
    SimpleXML & XPath
    PHP Code:
    $xml = new SimpleXMLElement($string);
    foreach(
    $xml->user as $user){
        foreach(
    $user->xpath('userimages/image[position() <= 5]/@url') as $url){
            echo 
    $url;
        }
        echo 
    '<br />';
    }
    /*
        theurl.com/node1image1.jpgtheurl.com/node1image2.jpgtheurl.com/node1image3.jpgtheurl.com/node1image4.jpgtheurl.com/node1image5.jpg
        theurl.com/node1image1.jpgtheurl.com/node1image2.jpgtheurl.com/node1image3.jpg
        theurl.com/node1image1.jpgtheurl.com/node1image2.jpgtheurl.com/node1image3.jpgtheurl.com/node1image4.jpgtheurl.com/node1image5.jpg
    */ 
    RecipesNext.com - recipes, kitchen queries, videos & more...
    BargainNext.com - coupons / deals / bargains / offers & more...

  6. #6
    SitePoint Addict
    Join Date
    Oct 2008
    Location
    Virtual World is my location
    Posts
    306
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Exclamation

    Hello

    Just so that all get an idea of how the xml file comes up and the php file is... if anyone could take it from there

    the myxmlfile.xml is
    Code:
    <feedstart>
    <mainnode id="1A" offid="X13">
    <data>
    	<Sub1></Sub1>
      	<Sub2></Sub2>
    	<Sub3>
      		<SubSub1></SubSub1>
    		<SubSub2></SubSub2>
    		<SubSub3></SubSub3>
    		<SubSub4></SubSub4>
    	</Sub3>
    </data>
    <otherinfo></otherinfo>
    <userimages total="5">
    	<image url="theurl.com/node1image1.jpg" alttxt="imagealt" value="1" />
    	<image url="theurl.com/node1image2.jpg" alttxt="imagealt" value="2" />
    	<image url="theurl.com/node1image3.jpg" alttxt="imagealt" value="3" />
    	<image url="theurl.com/node1image4.jpg" alttxt="imagealt" value="4" />
    	<image url="theurl.com/node1image5.jpg" alttxt="imagealt" value="5" />
    </userimages>
    
    <generalinfo></generalinfo>
    </mainnode>
    
    <mainnode id="2A" offid="X14">
    
    <data>
    	<Sub1></Sub1>
      	<Sub2></Sub2>
    	<Sub3>
      		<SubSub1></SubSub1>
    		<SubSub2></SubSub2>
    		<SubSub3></SubSub3>
    		<SubSub4></SubSub4>
    	</Sub3>
    </data>
    <otherinfo></otherinfo>
    <userimages total="3">
    	<image url="theurl.com/node2image1.jpg" alttxt="imagealt" value="1" />
    	<image url="theurl.com/node2image2.jpg" alttxt="imagealt" value="3" />
    	<image url="theurl.com/node2image3.jpg" alttxt="imagealt" value="3" />
    </userimages>
    
    <generalinfo></generalinfo>
    </mainnode>
    
    <mainnode id="3A" offid="X15">
    
    <data>
    	<Sub1></Sub1>
      	<Sub2></Sub2>
    	<Sub3>
      		<SubSub1></SubSub1>
    		<SubSub2></SubSub2>
    		<SubSub3></SubSub3>
    		<SubSub4></SubSub4>
    	</Sub3>
    </data>
    <otherinfo></otherinfo>
    <userimages total="4">
    	<image url="theurl.com/node3image1.jpg" alttxt="imagealt" value="1" />
    	<image url="theurl.com/node3image2.jpg" alttxt="imagealt" value="2" />
    	<image url="theurl.com/node3image3.jpg" alttxt="imagealt" value="3" />
    	<image url="theurl.com/node3image4.jpg" alttxt="imagealt" value="4" />
    </userimages>
    
    <generalinfo></generalinfo>
    </mainnode>
    </feedstart>
    the php file is

    Code:
    <?
    	$objDOM = new DOMDocument();
      	$objDOM->load("myxmlfile.xml"); 
    
    	$params = $objDOM->getElementsByTagName("image");
    	foreach( $params as $picture )
      	{
        		$theurl = $picture -> getAttribute('url');
    		echo "$theurl ";
    	}
    ?>
    RecipesNext.com - recipes, kitchen queries, videos & more...
    BargainNext.com - coupons / deals / bargains / offers & more...

  7. #7
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,396
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Can't you get the 'userimages' tags first, and loop through them, and then on each 'foreach' get the 'images' of that 'userimages', loop through them and display them?
    So you'd have two nested loops, and everytime you leave the inner (images) loop, you print a <br /> tag to go to a new line.

  8. #8
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,903
    Mentioned
    93 Post(s)
    Tagged
    0 Thread(s)
    Try changing from the short <? php tag to the full <?php tag
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  9. #9
    SitePoint Addict
    Join Date
    Oct 2008
    Location
    Virtual World is my location
    Posts
    306
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question

    Hi
    Thanks for your reply. I did try that, but for the first instance it does give me the userimages number, but then the statement does not break... it completes the entire loop of all images and then breaks.

    this code which I had done gives me the number of images for the first instance and then if you see..loops the xml and prints all values thrice..because of for each

    Any ideas how I could apply a break when the first instance of images is complete and the second is to begin

    Code:
    <?
    	$objDOM = new DOMDocument();
      	$objDOM->load("myxmlfile.xml"); 
    $gettotalim = $objDOM->getElementsByTagName("userimages");
     foreach( $gettotalim as $gettotalims )
      {
      $gettotalims  = intval($gettotalims -> getAttribute('total'));
      echo $gettotalims;
      
    	$params = $objDOM->getElementsByTagName("image");
    	foreach( $params as $picture )
      	{
        		$theurl = $picture -> getAttribute('url');
    		echo "$theurl ";
    	}
      }
    ?>

    Could you let me know how this could be done in my code

    Thanks


    Quote Originally Posted by guido2004 View Post
    Can't you get the 'userimages' tags first, and loop through them, and then on each 'foreach' get the 'images' of that 'userimages', loop through them and display them?
    So you'd have two nested loops, and everytime you leave the inner (images) loop, you print a <br /> tag to go to a new line.
    RecipesNext.com - recipes, kitchen queries, videos & more...
    BargainNext.com - coupons / deals / bargains / offers & more...

  10. #10
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You've had the answer already.

    Does this look similar?

    PHP Code:
    $string '<feedstart>
        <mainnode>
            <userimages total="5">
                <image url="theurl.com/node1image1.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image2.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image3.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image4.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image5.jpg" alttxt="imagealt" value="1" />
            </userimages>
        </mainnode>
        <mainnode>
            <userimages total="5">
                <image url="theurl.com/node1image1.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image2.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image3.jpg" alttxt="imagealt" value="1" />
            </userimages>
        </mainnode>
        <mainnode>
            <userimages total="5">
                <image url="theurl.com/node1image1.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image2.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image3.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image4.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image5.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image6.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image7.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image8.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image9.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image10.jpg" alttxt="imagealt" value="1" />
            </userimages>
        </mainnode>
    </feedstart>'
    ;

    $xml = new SimpleXMLElement($string);
    foreach(
    $xml->mainnode as $node){
        foreach(
    $node->xpath('userimages/image[position() <= 5]/@url') as $url){
            echo 
    $url;
        }
        echo 
    '<br />';

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  11. #11
    SitePoint Addict
    Join Date
    Oct 2008
    Location
    Virtual World is my location
    Posts
    306
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Hi Anthony

    Perfect - works this time round

    Seems i was missing something last time

    Thanks for all ur help

    Quote Originally Posted by AnthonySterling View Post
    You've had the answer already.

    Does this look similar?

    PHP Code:
    $string '<feedstart>
        <mainnode>
            <userimages total="5">
                <image url="theurl.com/node1image1.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image2.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image3.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image4.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image5.jpg" alttxt="imagealt" value="1" />
            </userimages>
        </mainnode>
        <mainnode>
            <userimages total="5">
                <image url="theurl.com/node1image1.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image2.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image3.jpg" alttxt="imagealt" value="1" />
            </userimages>
        </mainnode>
        <mainnode>
            <userimages total="5">
                <image url="theurl.com/node1image1.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image2.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image3.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image4.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image5.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image6.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image7.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image8.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image9.jpg" alttxt="imagealt" value="1" />
                <image url="theurl.com/node1image10.jpg" alttxt="imagealt" value="1" />
            </userimages>
        </mainnode>
    </feedstart>'
    ;

    $xml = new SimpleXMLElement($string);
    foreach(
    $xml->mainnode as $node){
        foreach(
    $node->xpath('userimages/image[position() <= 5]/@url') as $url){
            echo 
    $url;
        }
        echo 
    '<br />';

    RecipesNext.com - recipes, kitchen queries, videos & more...
    BargainNext.com - coupons / deals / bargains / offers & more...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •