superkingkong Posted November 21, 2008 Share Posted November 21, 2008 hi guys, for eg, i have a listing of files in a directory, assigned to an array eg, abc.txt def.txt ghi.txt jkl.txt mno.txt pqr.txt stu.txt vwx.txt yz.txt how can i display the even count or odd count of them? eg, abc.txt ghi.txt mno.txt stu.txt yz.txt or def.txt jkl.txt pqr.txt vwx.txt your help is very much appreciated, thanks Quote Link to comment https://forums.phpfreaks.com/topic/133719-displaying-evenodd-items-of-an-array/ Share on other sites More sharing options...
JasonLewis Posted November 22, 2008 Share Posted November 22, 2008 Use a for() loop and set the start at either 0 or 1 then go up in increments of 2. $type = 1; //Odd; for($i = $type; $i < count($your_array); $i += 2){ //do stuff here } Quote Link to comment https://forums.phpfreaks.com/topic/133719-displaying-evenodd-items-of-an-array/#findComment-695918 Share on other sites More sharing options...
superkingkong Posted November 22, 2008 Author Share Posted November 22, 2008 Use a for() loop and set the start at either 0 or 1 then go up in increments of 2. $type = 1; //Odd; for($i = $type; $i < count($your_array); $i += 2){ //do stuff here } Hi, thank you so much :) Quote Link to comment https://forums.phpfreaks.com/topic/133719-displaying-evenodd-items-of-an-array/#findComment-695939 Share on other sites More sharing options...
DarkWater Posted November 22, 2008 Share Posted November 22, 2008 @ProjectFear: Using count() on every iteration is SLOW. This is probably faster: <?php foreach (glob('*') as $num => $name) { if ($num & 1) { continue; } echo "$name\n"; } ?> To change it to even numbers, add a ! in front of $num in the if condition. Quote Link to comment https://forums.phpfreaks.com/topic/133719-displaying-evenodd-items-of-an-array/#findComment-695957 Share on other sites More sharing options...
JasonLewis Posted November 22, 2008 Share Posted November 22, 2008 Really? I use count() a fair bit in loops actually... I suppose I should be counting and storing it in a variable then using that variable instead. So instead of: for($i = 0; $i < count($array); $i++){ It'd be: $count = count($array); for($i = 0; $i < $count; $i++){ That would be faster, yes? Quote Link to comment https://forums.phpfreaks.com/topic/133719-displaying-evenodd-items-of-an-array/#findComment-695976 Share on other sites More sharing options...
superkingkong Posted November 22, 2008 Author Share Posted November 22, 2008 ooh... here is my code.... wonder whether if you can help me to "streamline" it <?php $viewExt = '.jpg|.jpeg|.tif|.tiff|.gif|.png|.pdf|.txt'; // only filenames with these extensions will be displayed $dirHandle = opendir('../upload'); $fcount = 0; while ($file = readdir($dirHandle)) { if ($file != '.' && $file != '..' && eregi("($viewExt)$",$file) && !eregi("^index.",$file)) { $stack[] = $file; // append filename to an array $fcount ++; } } closedir($dirHandle); sort($stack); echo "<table border=1><tr><td colspan='2' align='center'><b>Your Uploaded Files</b></td></tr><tr><td>"; $type = 0; //Even; for($i = $type; $i < count($stack); $i += 2){ echo "<br><font color='#ff0000'><b>•</b></font> " . $stack[$i]; } echo "</td><td>"; $type = 1; //Odd; for($i = $type; $i < count($stack); $i += 2){ echo "<br><font color='#ff0000'><b>•</b></font> " . $stack[$i]; } echo "</tr><tr><td colspan='2' align='center'><b>Total Uploaded Files:" . $fcount . "</b></td></tr></table>"; ?> thank you very much :) Quote Link to comment https://forums.phpfreaks.com/topic/133719-displaying-evenodd-items-of-an-array/#findComment-695986 Share on other sites More sharing options...
xtopolis Posted November 22, 2008 Share Posted November 22, 2008 @ ProjectFear Yes, a bottleneck in code is performing count x number of times on an array/loop that stays the same length. Doing the same thing once >faster than> doing the same thing 50 times for the same result. Just as your example is how it should be done. @superkingkong Not much smaller. <?php $dir = './upload/'; $files = glob("$dir{*.jpg,*.jpeg,*.tif,*.tiff,*.gif,*.png,*.pdf,*.txt,*.JPG,*.JPEG,*.TIF,*.TIFF,*.GIF,*.PNG,*.PDF,*.TXT}", GLOB_BRACE); sort($files); $i = 0; $left='';$right=''; foreach($files as $file){ $file = str_replace($dir, '', $file); if(($i % 2) == 0 || $i == 0){ $left .= "<br><font color='#ff0000'><b>•</b></font>$file"; }else{ $right .= "<br><font color='#ff0000'><b>•</b></font>$file"; } $i++; } echo "<table border=1><tr><td colspan='2' align='center'><b>Your Uploaded Files</b></td></tr><tr><td>"; echo $left; echo "</td><td>"; echo $right; echo "</td></tr><tr><td colspan='2' align='center'><b>Total Uploaded Files:" . count($files) . "</b></td></tr></table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/133719-displaying-evenodd-items-of-an-array/#findComment-696049 Share on other sites More sharing options...
Mark Baker Posted November 22, 2008 Share Posted November 22, 2008 <?php if(($i % 2) == 0 || $i == 0){ $left .= "<br><font color='#ff0000'><b>•</b></font>$file"; }else{ $right .= "<br><font color='#ff0000'><b>•</b></font>$file"; } $i++; => <?php if(($i++ % 2) == 0){ $left .= "<br><font color='#ff0000'><b>•</b></font>$file"; }else{ $right .= "<br><font color='#ff0000'><b>•</b></font>$file"; } Quote Link to comment https://forums.phpfreaks.com/topic/133719-displaying-evenodd-items-of-an-array/#findComment-696132 Share on other sites More sharing options...
Mark Baker Posted November 22, 2008 Share Posted November 22, 2008 Fractionally faster still if you pre-increment, and compare with 1 rather than 0 if((++$i % 2) == 1){ $left .= "<br><font color='#ff0000'><b>&#149;</b></font>$file"; }else{ $right .= "<br><font color='#ff0000'><b>&#149;</b></font>$file"; } Quote Link to comment https://forums.phpfreaks.com/topic/133719-displaying-evenodd-items-of-an-array/#findComment-696137 Share on other sites More sharing options...
superkingkong Posted November 22, 2008 Author Share Posted November 22, 2008 thanks for the suggestion i tried to put the code in the particular directory, it only shows the top and bottom row. total files uploaded is only 1. all the files are not showing. do you know the reason? by the way, the directory is not empty. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/133719-displaying-evenodd-items-of-an-array/#findComment-696511 Share on other sites More sharing options...
xtopolis Posted November 22, 2008 Share Posted November 22, 2008 Is the path correct? I changed my $dir to be /upload, not ../upload. Forgot to change it back before posting, sorry. Quote Link to comment https://forums.phpfreaks.com/topic/133719-displaying-evenodd-items-of-an-array/#findComment-696549 Share on other sites More sharing options...
superkingkong Posted November 23, 2008 Author Share Posted November 23, 2008 Is the path correct? I changed my $dir to be /upload, not ../upload. Forgot to change it back before posting, sorry. hey, thanks my bad, too excited to try out the new code, missed the 2 dots thanks again Quote Link to comment https://forums.phpfreaks.com/topic/133719-displaying-evenodd-items-of-an-array/#findComment-696681 Share on other sites More sharing options...
superkingkong Posted November 24, 2008 Author Share Posted November 24, 2008 hi, i'm sorry to bother again i've noticed that... although the directory is "empty", the count is showing 1. well, actually, my upload directory has a .htaccess file. is glob reading it as well although it is not declared in glob? thanks. Quote Link to comment https://forums.phpfreaks.com/topic/133719-displaying-evenodd-items-of-an-array/#findComment-698217 Share on other sites More sharing options...
superkingkong Posted November 24, 2008 Author Share Posted November 24, 2008 oh, i'm not sure whether it reads the .htaccess. all i know is, when my directory has no files (excluding) .htaccess, the count shows 1 if i have abc.gif and .htaccess, the count shows 1 if i have abc.gif and def.jpg and .htaccess, the count shows 2 so, i'm not sure what is wrong.. Quote Link to comment https://forums.phpfreaks.com/topic/133719-displaying-evenodd-items-of-an-array/#findComment-698224 Share on other sites More sharing options...
xtopolis Posted November 25, 2008 Share Posted November 25, 2008 Do a print_r($files) and see if it contains anything. Quote Link to comment https://forums.phpfreaks.com/topic/133719-displaying-evenodd-items-of-an-array/#findComment-698281 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.