I'm sure I'm missing something simple but I just can't seem to figure this one out. I have a dynamic XML file that is too big to display all the information on one web page. So I need a way to split the information across multiple pages. I have used foreach but the pointer is reset each time I loop through.

the following works to display all on one page:

$target = file_get_contents('http://twitter.com/statuses/followers/'.$username.'.xml');

$twitters = new SimpleXMLElement($target);

foreach ($twitters->user as $twit) {
 echo '<div style="width:200px;height:275px;overflow-y:scroll;border:2px black solid;padding:3px;float:left;word-wrap:break-word;">
 <a href="http://www.twitter.com/'.$twit->screen_name.'" ><img class="twitter_followers" src="'. $twit->profile_image_url.'" title="'. $twit->name .'" alt="" border="none" ></a><br>
 <div style="text-align:left;"><b>'.$twit->name.'</b><br>'.$twit->description.'<br><a href="'.$twit->url.'">Web Site</a>
 <br><br>
 <i>Most recent post:</i><br>
 '.$twit->status->text.'
 </div>
 </div>';
 }

however when I try to display only 10 at a time I get the same first 10 due to the counter resetting

$target = file_get_contents('http://twitter.com/statuses/followers/'.$username.'.xml');

$twitters = new SimpleXMLElement($target);
for($i=0;$i<10;$i++){
foreach ($twitters->user as $twit) {
 echo '<div style="width:200px;height:275px;overflow-y:scroll;border:2px black solid;padding:3px;float:left;word-wrap:break-word;">
 <a href="http://www.twitter.com/'.$twit->screen_name.'" ><img class="twitter_followers" src="'. $twit->profile_image_url.'" title="'. $twit->name .'" alt="" border="none" ></a><br>
 <div style="text-align:left;"><b>'.$twit->name.'</b><br>'.$twit->description.'<br><a href="'.$twit->url.'">Web Site</a>
 <br><br>
 <i>Most recent post:</i><br>
 '.$twit->status->text.'
 </div>
 </div>';
 }
}

I don't necesarily need to use WHILE. I am just looking for a way to display the XML data in groups of 10.

Member Avatar for rajarajan2017

do you provide that sample xml file?

<?xml version="1.0" encoding="UTF-8" ?> 
- <users type="array">
- <user>
  <id>42900929</id> 
  <name>Karen Ficarelli</name> 
  <screen_name>Fitness4Her</screen_name> 
  <location>Palm Beach, Florida USA</location> 
  <description>Certified Personal Trainer & Sports Nutritionist specializing in Motivating and Inspiring other Women to achieve their individual Health & Fitness Goals!</description> 
  <profile_image_url>http://a3.twimg.com/profile_images/407110625/Twitter090909_3_normal.jpg</profile_image_url> 
  <url>http://www.fitness4her.com</url> 
  <protected>false</protected> 
  <followers_count>45259</followers_count> 
  <profile_background_color>000000</profile_background_color> 
  <profile_text_color>000000</profile_text_color> 
  <profile_link_color>ff0beb</profile_link_color> 
  <profile_sidebar_fill_color>38dbff</profile_sidebar_fill_color> 
  <profile_sidebar_border_color>000000</profile_sidebar_border_color> 
  <friends_count>45383</friends_count> 
  <created_at>Wed May 27 15:07:50 +0000 2009</created_at> 
  <favourites_count>0</favourites_count> 
  <utc_offset>-18000</utc_offset> 
  <time_zone>Eastern Time (US & Canada)</time_zone> 
  <profile_background_image_url>http://a3.twimg.com/profile_background_images/24695773/fitness_twitter.jpg</profile_background_image_url> 
  <profile_background_tile>false</profile_background_tile> 
  <notifications /> 
  <geo_enabled>false</geo_enabled> 
  <verified>false</verified> 
  <following /> 
  <statuses_count>470</statuses_count> 
  <lang>en</lang> 
  <contributors_enabled>false</contributors_enabled> 
- <status>
  <created_at>Fri Apr 16 01:46:59 +0000 2010</created_at> 
  <id>12257265084</id> 
  <text>Never Count Calories Recipes – Part 2 http://bit.ly/ch57Hl</text> 
  <source><a href="http://www.tweetdeck.com" rel="nofollow">TweetDeck</a></source> 
  <truncated>false</truncated> 
  <in_reply_to_status_id /> 
  <in_reply_to_user_id /> 
  <favorited>false</favorited> 
  <in_reply_to_screen_name /> 
  <geo /> 
  <coordinates /> 
  <place /> 
  <contributors /> 
  </status>
  </user>
- <user>
  <id>19954777</id> 
  <name>Leslie Mendenhall</name> 
  <screen_name>LeslieMendenhal</screen_name> 
  <location>Palm Beach County, Florida</location> 
  <description>Mom-two girls;Owner-Integrity Land Title;Women's Prosperity Network Leader;love to help others fulfill their dreams;www.womensprosperitynetwork.com/PalmBeach</description> 
  <profile_image_url>http://a1.twimg.com/profile_images/183850092/10-19-06_leslie_normal.jpg</profile_image_url> 
  <url>http://www.integritylandtitle.com</url> 
  <protected>false</protected> 
  <followers_count>724</followers_count> 
  <profile_background_color>EBEBEB</profile_background_color> 
  <profile_text_color>333333</profile_text_color> 
  <profile_link_color>990000</profile_link_color> 
  <profile_sidebar_fill_color>F3F3F3</profile_sidebar_fill_color> 
  <profile_sidebar_border_color>DFDFDF</profile_sidebar_border_color> 
  <friends_count>805</friends_count> 
  <created_at>Tue Feb 03 04:45:11 +0000 2009</created_at> 
  <favourites_count>2</favourites_count> 
  <utc_offset>-18000</utc_offset> 
  <time_zone>Eastern Time (US & Canada)</time_zone> 
  <profile_background_image_url>http://s.twimg.com/a/1270853725/images/themes/theme7/bg.gif</profile_background_image_url> 
  <profile_background_tile>false</profile_background_tile> 
  <notifications /> 
  <geo_enabled>false</geo_enabled> 
  <verified>false</verified> 
  <following /> 
  <statuses_count>113</statuses_count> 
  <lang>en</lang> 
  <contributors_enabled>false</contributors_enabled> 
- <status>
  <created_at>Mon Mar 15 03:31:05 +0000 2010</created_at> 
  <id>10500208884</id> 
  <text>Come Join us! Take the Steps to Homebuyer Success Workshop! No Cost -- http://short.to/1jup3</text> 
  <source><a href="http://apiwiki.twitter.com/" rel="nofollow">API</a></source> 
  <truncated>false</truncated> 
  <in_reply_to_status_id /> 
  <in_reply_to_user_id /> 
  <favorited>false</favorited> 
  <in_reply_to_screen_name /> 
  <geo /> 
  <coordinates /> 
  <place /> 
  <contributors /> 
  </status>
  </user>
  </users>
Member Avatar for rajarajan2017
for($i=0;$i<10;$i++){foreach ($twitters->user as $twit) { echo '<div style="width:200px;height:275px;overflow-y:scroll;border:2px black solid;padding:3px;float:left;word-wrap:break-word;"> <a href="http://www.twitter.com/'.$twit->screen_name.'" ><img class="twitter_followers" src="'. $twit->profile_image_url.'" title="'. $twit->name .'" alt="" border="none" ></a><br> <div style="text-align:left;"><b>'.$twit->name.'</b><br>'.$twit->description.'<br><a href="'.$twit->url.'">Web Site</a> <br><br> <i>Most recent post:</i><br> '.$twit->status->text.' </div> </div>'; }

For each... statement retrieves all the nodes from your xml, so after completed you call the loop for second, third, fourth so on...

Do it vice versa, ie.

1) Remove the loop outside (ex: for i=0;i<10)
2) Initalize a varibale i=0; (outside of for each)
3) Within for..each after the { increment the i
i++;
if (i>10) { exit the for each loop
4) Now the loop get truncated at the specific node
5) and on next page use the for..each statement
and start to print from i=11;

you get it!

Member Avatar for rajarajan2017

4000 lines!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! If it possible please remove that, it slow down the computer and give it a try for the solution from the above thread

4000 lines!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! If it possible please remove that, it slow down the computer and give it a try for the solution from the above thread

I didn't notice how long it was. I have removed most of it.

I understand your code, however I have tried that, and when you exit the foreach the pointer gets reset to 0 so it starts all over from the beginning instead of node 11. At least that was my experience. That's why I was asking for a solution other than foreach. Possibly a WHILE loop.

Maybe I missed something. How would I restart the foreach loop without resetting the pointer once I do the exit?

Member Avatar for rajarajan2017

whether you need to achieve the thing within the same loop? then where do you going to write next 10 nodes for differnt page?

this is what I am trying to accomplish:

$following = count($twitters);
$pages = $following / 10;
$i=0;
for($p=0;$p<$pages;$p++){
// assign a div id to each page
echo '<div id="p'.$p.'">';
for($i=0;$i<10;$i++){
// loop through 10 users
echo '<div style="width:200px;height:275px;overflow-y:scroll;border:2px black solid;padding:3px;float:left;word-wrap:break-word;">
 <a href="http://www.twitter.com/'.$twit->screen_name.'" ><img class="twitter_followers" src="'. $twit->profile_image_url.'" title="'. $twit->name .'" alt="" border="none" ></a><br>
 <div style="text-align:left;"><b>'.$twit->name.'</b><br>'.$twit->description.'<br><a href="'.$twit->url.'">Web Site</a>
 <br><br>
 <i>Most recent post:</i><br>
 '.$twit->status->text.'
 </div>
 </div>';
}
echo '</div>';
}
//then later in the code display page number links and display or hide the groups of 10 users on click

the problem I am having is picking up the loop where it left off. e.g. 11, 21, 31, etc.

maybe it would help to see what the page currently looks like.
http://www.myecservice.com/twitter.php

Member Avatar for rajarajan2017

Method II:

1) Create an array named as splittedStr

nodeCounter=-1;
 stepValue=10;
 checkCounter=10;
 startFrom=2;
 arrCounter=0;
 tenNodes="";

 foreach ($twitters->user as $twit){
	nodeCounter++;
 	if (nodeCounter >= checkCounter {
	   startFrom++;
	   checkCounter=stepValue*starFrom;
	   splittedstr[arrCounter]=tenNodes;
	   arrCounter++;
	   tenNodes="";
	}
	tenNodes.="Whatever you going to print within echo stored as a string';
}

So for every 10 nodes we concentenated all in tenNodes and stored in splittedstr[0]=tenNodes,splittedstr[1]=tenNodes
finally use a different loop and print the data from the array values in differnt pages.

Member Avatar for rajarajan2017

initalize startFrom=1

Thank you so much for your help. I didn't use your code exactly but it did lead me in the right direction and is very similar to what I used.

$i=0;
$splitted = array();

foreach ($twitters->user as $twit) {
$splitted[$i] = $twit;
$i++;
}
$f=0;

for($p=0;$p<$pages;$p++){
// assign a div id to each page
echo '<div id="p'.$p.'"style="padding:10px;">';
for($s=0;$s<10;$s++){
// loop through 10 users
echo '<div style="width:200px;height:275px;overflow-y:scroll;border:2px black solid;padding:3px;float:left;word-wrap:break-word;">
 <a href="http://www.twitter.com/'.$splitted[$f]->screen_name.'" ><img class="twitter_followers" src="'. $splitted[$f]->profile_image_url.'" title="'. $splitted[$f]->name .'" alt="" border="none" ></a><br>
 <div style="text-align:left;"><b>'.$splitted[$f]->name.'</b><br>'.$splitted[$f]->description.'<br><a href="'.$splitted[$f]->url.'">Web Site</a>
 <br><br>
 <i>Most recent post:</i><br>
 '.$splitted[$f]->status->text.'
 </div>
 </div>';
$f++;
}
echo '</div>';
}
Member Avatar for rajarajan2017

Nice and Cool!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.