Re: [PHP] Order directory output

2008-02-14 Thread Richard Lynch
opendir/readdir does not promise to deliver the files in any
particular order, no matter what it seems to do on any given day...

Put them in an array and sort() if it's a small list of files.

Or use exec and ls -als or somesuch for a large list of files.

Or...

glob *might* be documented to do things in a certain order, but I
doubt it.

On Fri, February 8, 2008 4:44 pm, Pastor Steve wrote:
 Hi, thanks for all your help today.

 I have the following code and I am trying to order the output.
 Currently it
 seems really random. Can anyone point me in the right direction?

 ?php

 $dir = content/current/breaking_news/;

 // set pattern
 $pattern = .txt*|.TXT*;

 // open directory and parse file list
 if (is_dir($dir))
 {
 if ($dh = opendir($dir))
 {

 //This is the div that contains the wrap around for the breaking news
 section.
 echo 
 div class=\spstory\ style=\font-family: Times New Roman,
 Times,
 serif; font-size: 12px; width: 290px;\
 div style=\width: 285px; background-color: #CC; padding:
 3px;\
 span class=\NormalHeadRed\Breaking News/span
 br /Please check here often for breaking news stories.
 /div
 p /
 span class=\NomalText\

 ul;

 // iterate over file list
 while (($filename = readdir($dh)) !== false)

 {

 // if filename matches search pattern, print it
 if (ereg($pattern, $filename))
if(strpos($filename,'.')0)

 {
 $fh = fopen($dir . $filename, r);

 $filehead = fgets($fh);

 fclose($fh);
 echo 
 li class=\bn_bullet\
 a href=\/breaking_news/$filename\$filehead/a
 /li;
 }
 }
 echo 
 /ulp /
 /span
 /div;

 // close directory
 closedir($dh);
 }
 }

 ?

 Thank you,

 --
 Steve Marquez




-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Order directory output

2008-02-08 Thread Pastor Steve
Hi, thanks for all your help today.

I have the following code and I am trying to order the output. Currently it
seems really random. Can anyone point me in the right direction?

?php

$dir = content/current/breaking_news/;

// set pattern
$pattern = .txt*|.TXT*;

// open directory and parse file list
if (is_dir($dir))
{
if ($dh = opendir($dir))
{

//This is the div that contains the wrap around for the breaking news
section.
echo 
div class=\spstory\ style=\font-family: Times New Roman, Times,
serif; font-size: 12px; width: 290px;\
div style=\width: 285px; background-color: #CC; padding:
3px;\
span class=\NormalHeadRed\Breaking News/span
br /Please check here often for breaking news stories.
/div
p /
span class=\NomalText\

ul;

// iterate over file list
while (($filename = readdir($dh)) !== false)

{

// if filename matches search pattern, print it
if (ereg($pattern, $filename))
   if(strpos($filename,'.')0)

{
$fh = fopen($dir . $filename, r);

$filehead = fgets($fh);

fclose($fh);
echo 
li class=\bn_bullet\
a href=\/breaking_news/$filename\$filehead/a
/li;
}
}
echo 
/ulp /
/span
/div;

// close directory
closedir($dh);
}
}

?

Thank you,

--
Steve Marquez



Re: [PHP] Order directory output

2008-02-08 Thread Jim Lucas

Pastor Steve wrote:

Hi, thanks for all your help today.

I have the following code and I am trying to order the output. Currently it
seems really random. Can anyone point me in the right direction?

?php

$dir = content/current/breaking_news/;

// set pattern
$pattern = .txt*|.TXT*;

// open directory and parse file list
if (is_dir($dir))
{
if ($dh = opendir($dir))
{

//This is the div that contains the wrap around for the breaking news
section.
echo 
div class=\spstory\ style=\font-family: Times New Roman, Times,
serif; font-size: 12px; width: 290px;\
div style=\width: 285px; background-color: #CC; padding:
3px;\
span class=\NormalHeadRed\Breaking News/span
br /Please check here often for breaking news stories.
/div
p /
span class=\NomalText\

ul;


// iterate over file list
while (($filename = readdir($dh)) !== false)

{

// if filename matches search pattern, print it
if (ereg($pattern, $filename))
   if(strpos($filename,'.')0)

{
$fh = fopen($dir . $filename, r);

$filehead = fgets($fh);

fclose($fh);
echo 
li class=\bn_bullet\
a href=\/breaking_news/$filename\$filehead/a
/li;
}
}
echo 
/ulp /
/span
/div;

// close directory
closedir($dh);
}
}

?

Thank you,

--
Steve Marquez




A cut down version of what you are trying to do, I think.

?php

$dir = ../../;

// set pattern
$allowed_ftypes = array('php','txt','TXT');

// open directory and parse file list
if (is_dir($dir)) {
if ( ($files = glob($dir.'*') ) !== false ) {

// This is the div that contains the wrap around for the breaking news
// section.
echo 'div
Breaking Newsbr /
Please check here often for breaking news stories.
p
span class=NomalText
ul';


// iterate over file list
foreach ( $files AS $file ) {

// if filename matches search pattern, print it
if ( in_array(array_pop(explode('.', $file) ), $allowed_ftypes ) ) {

// Open file for reading
if ( ( $fh = fopen($file, r) ) !== false ) {

// Get the first line of the file
$filehead = fgets($fh);

// Extract file name for link
$filename = array_pop(explode('/', $file));

// Display Link
echo 'lia href=/breaking_news/'.$filename.''.
  htmlspecialchars($filehead).'/a/li';

fclose($fh);
}
}
}
echo '/ul/p/span/div';
}
}

?

--
Jim Lucas

   Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them.

Twelfth Night, Act II, Scene V
by William Shakespeare

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Order directory output

2008-02-08 Thread Nathan Nobbe
i hooked up an spl example; and the files are sorted by name.
also, did you want that p inside or outside the span w/
class=NormalText, because the opening and closing tags are
mixed up..

?php
class FileIterator extends FilterIterator {
public function __construct(Iterator $it) {
if(!($it instanceof DirectoryIterator)) {
throw new LogicException(get_class($it) . ' must be
a DirectoryIterator!');
}
parent::__construct($it);
}

public function accept() {
if($this-getInnerIterator()-current()-isFile()) {
return true;
}
}
}

class TextInfoIterator extends ArrayIterator {
public function __construct($array, $flags=0) {
$this-ksort();
parent::__construct($array, $flags);
}

public function current() {
return 'li' . parent::current() . '/li';
}
}

$fileMatchString = '/.*(\.txt|\.TXT)$/';
$resultData = array();

foreach(new RegexIterator(new FileIterator(new
DirectoryIterator('testDir')), $fileMatchString) as $curAllowedFile) {
try {
$theFile = $curAllowedFile-openFile('r');
$theFile-setFlags(SplFileObject::DROP_NEW_LINE);
$resultData[$curAllowedFile-getFilename()] =
$theFile-fgets();
unset($theFile);// close the file ??
} catch(RuntimeException $e) {
// do something here
}
}
?
div class=spstory style=font-family: Times New Roman, Times,serif;
font-size: 12px; width: 290px;
div style=width: 285px; background-color: #CC; padding:3px;
span class=\NormalHeadRed\Breaking News/span
br /Please check here often for breaking news stories.
/div
p
span class=NomalText
ul
?php foreach(new TextInfoIterator($resultData) as
$curTextData) { ?
?=$curTextData?
?php } ?
/ul
/span
/p
/div

-nathan


Re: [PHP] Order directory output

2008-02-08 Thread Nathan Nobbe
On Fri, Feb 8, 2008 at 9:19 PM, Nathan Nobbe [EMAIL PROTECTED] wrote:

 i hooked up an spl example; and the files are sorted by name.
 also, did you want that p inside or outside the span w/
 class=NormalText, because the opening and closing tags are
 mixed up..


damnit; i had a couple of mistakes; so sue me; its friday night!

to ensure the path is in the href; i added it
$resultData[$curAllowedFile-getPathname()] = $theFile-fgets();

to make sure the results are sorted; i sorted them after the parent
ArrayIterator was created
parent::__construct($array, $flags);
$this-ksort();

and lastly, i added the anchor tag
a href=?=$curFilename??=$curTextData?/a

i could use a drink right about now..=/

-nathan


Re: [PHP] Order directory output

2008-02-08 Thread Paul Scott

On Fri, 2008-02-08 at 21:19 -0500, Nathan Nobbe wrote:
 i hooked up an spl example; and the files are sorted by name.
 also, did you want that p inside or outside the span w/
 class=NormalText, because the opening and closing tags are
 mixed up..
 

Here is another, generic extension filter with SPL. Untested, and I have
just woken up, so if it doesn't work... :-P

class ExtensionFilter extends FilterIterator {

  private $ext; 
  private $it;

  public function __construct(DirectoryIterator $it, $ext) {
  parent::__construct($it);
  $this-it = $it;
  $this-ext = $ext;
  }
  public function accept() {
  if ( ! $this-it-isDir() ) {
$ext = array_pop(explode('.', $this-current()));
return $ext != $this-ext;
  }
  return true;
  }
}

--Paul
-- 
.
| Chisimba PHP5 Framework - http://avoir.uwc.ac.za   |
::

All Email originating from UWC is covered by disclaimer 
http://www.uwc.ac.za/portal/public/portal_services/disclaimer.htm 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php