Jump to content

Recommended Posts

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 :)

Link to comment
https://forums.phpfreaks.com/topic/133719-displaying-evenodd-items-of-an-array/
Share on other sites

@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.

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?

ooh...

 

here is my code.... wonder whether if you can help me to "streamline" it :D

 

<?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>&#149;</b></font> " . $stack[$i];
}

echo "</td><td>";

$type = 1; //Odd;
for($i = $type; $i < count($stack); $i += 2){
	echo "<br><font color='#ff0000'><b>&#149;</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 :) :)

@ 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>&#149;</b></font>$file";
    }else{
      $right .= "<br><font color='#ff0000'><b>&#149;</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>";
?>

<?php
if(($i % 2) == 0 || $i == 0){
  $left .= "<br><font color='#ff0000'><b>&#149;</b></font>$file";
}else{
  $right .= "<br><font color='#ff0000'><b>&#149;</b></font>$file";
}
$i++;

=>

<?php
if(($i++ % 2) == 0){
  $left .= "<br><font color='#ff0000'><b>&#149;</b></font>$file";
}else{
  $right .= "<br><font color='#ff0000'><b>&#149;</b></font>$file";
}

 

Fractionally faster still if you pre-increment, and compare with 1 rather than 0

if((++$i % 2) == 1){
  $left .= "<br><font color='#ff0000'><b>&#38;#149;</b></font>$file";
}else{
  $right .= "<br><font color='#ff0000'><b>&#38;#149;</b></font>$file";
}

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.

hi,

 

i'm sorry to bother again :P

 

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.

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.. :(

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.