Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

please help with 'for' loop

Featured Replies

This script is meant to pull 4 <content> tags from an external page and echo the values. However, it seems to echo all of the <content> tag values but I don't understand why my counter loop is not working.

 

<?php

		$reader = new XMLReader;
		$res = $reader->open('http://www.blogger.com/feeds/17146895/posts/default');
		$entry = false;


for ($i=1; $i<=4; $i++){

while ($reader->read()) {
		if ($reader->name == 'entry') {
 			   if ($reader->nodeType == XMLReader::ELEMENT) {
 				  $entry = true;
   			 } else {
   	 			$entry = false;
 				   }
  		 }




		if ($entry && ($reader->name == 'content')) {

				$reader->read();
   			 if (($reader->name == '#text') && $reader->hasValue) {
   					echo '<p>' . $reader->value . '</p>';

			 }


		}	
		} 

}

?>

Because the contents of the for is pulling out the whole lot, and then you're just repeating that 4 times.

 

Without more info on the XMLReader class you're using, I can't advise how to fix.

  • Author

What's the XMLReader class? Sorry - I'm a noob!

 

Because the contents of the for is pulling out the whole lot, and then you're just repeating that 4 times.

 

Without more info on the XMLReader class you're using, I can't advise how to fix.

  • Author

this is my solution, which seems to work nicely

 

$reader = new XMLReader;
$res = $reader->open('http://www.blogger.com/feeds/17146895/posts/default');
$entry = false;
$i=1;



	while ($reader->read() && $i<=5) {
			   if ($reader->name == 'entry') {
 				  if ($reader->nodeType == XMLReader::ELEMENT) {
 						 $entry = true;
 						   $i++;
   					 } else {
   	 					$entry = false;
 					   }
  					 }




					if ($entry && ($reader->name == 'content')) {

							$reader->read();
   							 if (($reader->name == '#text') && $reader->hasValue) {
							echo '<p>' . $reader->value . '</p>';

   							 }



			}
	}

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.