Jump to content

displaying even/odd items of an array


superkingkong

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

@ 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>";
?>

Link to comment
Share on other sites

<?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";
}

 

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.