PDA

View Full Version : PHP can't count


mouse
05-03-2003, 01:28 AM
Hi there,
I use the following on this page here (http://silverpaw3d.com/smilies/), it takes images from a directory and puts them in two columns with a copy url link.

If you visit the page here (http://silverpaw3d.com/smilies/), you'll see that some aren't ordered. This is because the script isn't incrementing the value of 'Copyit' as it should, basically it's missing numbers out. The numbers 2, 68 and 80. It appears to miss the same ones each time.
:confused:<?

$path = "/path/";

$dir_handle = @opendir($path) or die("Unable to open $path");

print "Some irrelevant html";

print <table> \n";

while ($file = readdir($dir_handle)) {

$Copyit++;

if(ereg(".gif",$file)){

if($Copyit % 2 == 0){

print "<tr><td>Left column html</td> \n";

}else{

print "<td>Right column html</td></tr> \\n";

}}}

closedir($dir_handle);

print "</table>";


?> btw the forum is eating my backslashes :D

Phantom
05-03-2003, 02:47 AM
while ($file = readdir($dir_handle)) {

Either a) you want it to assign $file, thus giving you an infinte loop, or b) You forgot an '=', thus giving you an infinite loop :-)

(only thing I've found, and probably isn't your problem - who knows? I always seem to fix my problems by changing little things like that)

Spookster
05-03-2003, 04:20 AM
Originally posted by Phantom
while ($file = readdir($dir_handle)) {

Either a) you want it to assign $file, thus giving you an infinte loop, or b) You forgot an '=', thus giving you an infinite loop :-)

(only thing I've found, and probably isn't your problem - who knows? I always seem to fix my problems by changing little things like that)

It is correct. This expression:

$file = readdir($dir_handle)

will either evaluate to true or false after assigning a value to $file. True if successful, false otherwise and thus ending the loop.

Spookster
05-03-2003, 04:22 AM
Originally posted by mouse
Hi there,
I use the following on this page here (http://silverpaw3d.com/smilies/), it takes images from a directory and puts them in two columns with a copy url link.

If you visit the page here (http://silverpaw3d.com/smilies/), you'll see that some aren't ordered. This is because the script isn't incrementing the value of 'Copyit' as it should, basically it's missing numbers out. The numbers 2, 68 and 80. It appears to miss the same ones each time.
:confused:<?

$path = "/path/";

$dir_handle = @opendir($path) or die("Unable to open $path");

print "Some irrelevant html";

print <table> \n";

while ($file = readdir($dir_handle)) {

$Copyit++;

if(ereg(".gif",$file)){

if($Copyit % 2 == 0){

print "<tr><td>Left column html</td> \n";

}else{

print "<td>Right column html</td></tr> \\n";

}}}

closedir($dir_handle);

print "</table>";


?> btw the forum is eating my backslashes :D

I see you incrementing $Copyit but I don't see where you initialized it. And why are you incrementing before you even use it once? Explain your logic of what you are doing and what do the filenames look like?

mouse
05-03-2003, 04:40 AM
The value of $Copyit is incremented and a layer is given that value as it's ID, this ID needs to be unique as a javascript copies the content of the DIV to the clipboard. The file names are pretty random, no naming rules are being used.

Spookster
05-03-2003, 05:57 AM
Originally posted by mouse
The value of $Copyit is incremented and a layer is given that value as it's ID, this ID needs to be unique as a javascript copies the content of the DIV to the clipboard. The file names are pretty random, no naming rules are being used.

I see that $Copyit is being incremented however where is it being initialized at? What is its initial value?

Ökii
05-03-2003, 08:54 AM
shouldn't $copyIt++ be inside the if(gif) brace?

as it stands it would increment for finding index.html, for '..' and '.' and '.htaccess' etc - ie every file would increment it, whereas you only want gifs to increment it

// no $copyit++ here

if(....... 'gif')
{
echo (++$Copyit % 2 == 0) ? "<tr><td>Left column html</td> n" : "<td>Right column html</td></tr> n" ;
}

...........
explaination of ternery clause

echo (test) ? 'true' : 'false' ;
so
echo ( increment $copyit is even) ? 'left' : 'right' ;

mouse
05-03-2003, 06:38 PM
Thanks very much Ökii, obvious really http://silverpaw3d.com/smilies/thumb.gif

Spookster, why would I have to initialise it, and how could I use before incrementing it in a loop situation?

Spookster
05-03-2003, 11:40 PM
Originally posted by mouse
Spookster, why would I have to initialise it, and how could I use before incrementing it in a loop situation?

If you are going to increment a variable it has to have a initial value to be incremented. You should always initialize variables. Just good programming habit. Some languages may implicitly initialize variables for you but you can never be sure and that would proivide for poor readability of code not to mention unpredictability.