Jump to content

Infinite Loop in Glob Function


ShoeLace1291

Recommended Posts

I have a page that gets all the php files in a certain folder and displays them.  It's a foreach loop, but for some reason I'm getting an infinite loop.  This is my code:

 

<?php

echo "<table align='center' cellspacing='2' cellpadding='2' border='0'>
<tr>";

$rows = 0;

foreach(glob('forums/*.php') as $szFilename){

$rows = $rows + 1;



	echo "<td style='text-align: center;'><img src='images/icon_phpfile.png' alt='PHP File'><br>$szFilename</td>";

	if($rows == {

	 echo "</tr><tr>";

}

}

echo "</tr></table>";

?>

Link to comment
Share on other sites

Should be a problem here.... Try:

 

<?php

echo "<table align='center' cellspacing='2' cellpadding='2' border='0'>
<tr>";

$rows = 0;
$files = glob('forums/*.php');

foreach($files as $szFilename){

$rows = $rows + 1;



	echo "<td style='text-align: center;'><img src='images/icon_phpfile.png' alt='PHP File'><br>$szFilename</td>";

	if($rows == {

	 echo "</tr><tr>";

}

}

echo "</tr></table>";

?>

 

 

Orio

Link to comment
Share on other sites

I noticed two problems with your code.

1) You should increment the counter after formating the line

2) You should test if the remainder of the $rows / 8 is 0

 

Try:

<?php

echo "<table align='center' cellspacing='2' cellpadding='2' border='0'>
<tr>";

$rows = 0;

foreach(glob('*.php') as $szFilename){
                echo "<td style='text-align: center;'><img src='images/icon_phpfile.png' alt='PHP File'><br>$szFilename</td>";
                $rows++;
                if($rows % 8 == 0)
                        echo "</tr><tr>";

}

echo "</tr></table>";

?>

 

Ken

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.