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:


<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 -

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


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

Post the code you have right now, the one that prints all images on one line

You need to show us an XML sample with more than one grouping of the images in situ.

Maybe this will help.


<?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
*/

SimpleXML & XPath


$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
*/

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.


<?
	$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

 <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

<mainnode> and ends with </mainnode>

while ur xml starts with

<user> and ends with </user>

Secondly My code does not use the

<user> 

tag … it starts with userimages directly…

any help?

Thanks

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


<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


<?
	$objDOM = new DOMDocument();
  	$objDOM->load("myxmlfile.xml"); 

	$params = $objDOM->getElementsByTagName("image");
	foreach( $params as $picture )
  	{
    		$theurl = $picture -> getAttribute('url');
		echo "$theurl ";
	}
?>


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.

Try changing from the short <? php tag to the full <?php tag

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


<?
	$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

You’ve had the answer already. :wink:

Does this look similar?


$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 />';
}

Hi Anthony

Perfect - works this time round

Seems i was missing something last time

Thanks for all ur help