5 Replies - 1041 Views - Last Post: 12 April 2012 - 01:35 PM Rate Topic: -----

#1 Amerowolf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 31-January 09

Ordering a list alphabetically

Posted 09 April 2012 - 08:07 PM

Ok, so. I have this code that prints the content of a directory to a website as links to webpages (since that's all that is in the directory). What I am having problems with is ordering the list alphabetically.

My original code:
<?php

 $dir="../zpress/pages"; // Directory where files are stored

 if ($dir_list = opendir($dir))
 {
 while(($filename = readdir($dir_list)) !== false)
 if(!is_dir($filename))
 {
  ?>
  <p><a href="../zpress/pages/<?php echo $filename; ?>"><?php echo $filename;
  ?></a></p>
  <?php
  }
  closedir($dir_list);
  }

  ?>



I also got some advice from a friend, and I created the code below, but it ends up just showing nothing on the page and I've not been able to troubleshoot it myself.

$the_files = array();
while(($filename = readdir($dir_list)) !== false) {
    if(!is_dir($filename)) {
        array_push($the_files,$filename);
    }
}
sort($the_files);
foreach($the_files as $the_file) : ?>
   <p><a href="../zpress/pages/<?php echo $filename; ?>"><?php echo $filename;?></a></p>
<?php endforeach; ?> 


Is This A Good Question/Topic? 0
  • +

Replies To: Ordering a list alphabetically

#2 Macjohn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 407
  • Joined: 10-April 09

Re: Ordering a list alphabetically

Posted 09 April 2012 - 11:45 PM

You may wanna take a look at THIS and ALSO THIS...

for the second piece of code, here is a "correction" that should make it work...

$the_files = array();
while(($filename = readdir($dir_list)) != false) {
    if(!is_dir($filename)) {
        array_push($the_files,$filename);
    }
}
sort($the_files);
foreach($the_files as $the_file) ;
   echo '<p><a href="../zpress/pages/'.$filename.'"><'echo $filename.'></a></p>';
endforeach;


...Almost forgot...what do you use to code, an IDE I mean, NOTEPAD++, Eclipse, NetBeans.... try using something like NetBeans or so...It helps you alot with code errors, like the ":" instead of ";" in your second code and stuff like that.

This post has been edited by Macjohn: 12 April 2012 - 02:30 AM

Was This Post Helpful? 0
  • +
  • -

#3 4D1  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 225
  • Joined: 22-October 09

Re: Ordering a list alphabetically

Posted 12 April 2012 - 02:26 AM

@MacJohn the ':' is not an error it is PHP's alternate syntax.

This post has been edited by 4D1: 12 April 2012 - 02:34 AM

Was This Post Helpful? 1
  • +
  • -

#4 Macjohn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 407
  • Joined: 10-April 09

Re: Ordering a list alphabetically

Posted 12 April 2012 - 02:31 AM

Corrected it! :gun_bandana:
Thanks
Was This Post Helpful? 0
  • +
  • -

#5 4D1  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 225
  • Joined: 22-October 09

Re: Ordering a list alphabetically

Posted 12 April 2012 - 02:37 AM

sorry m8 there is still an error, either the script wont run because of an unexpected endforeach; or the echo statement will never execute because foreach() is followed by a terminator ;

This post has been edited by 4D1: 12 April 2012 - 03:01 AM

Was This Post Helpful? 0
  • +
  • -

#6 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Ordering a list alphabetically

Posted 12 April 2012 - 01:35 PM

To the OP, it shows nothing on the page because you are referencing the wrong variable. $filename is a temporary variable used in the while, you should be using $the_file, like this:
$the_files = array();
while(($filename = readdir($dir_list)) !== false) {
    if(!is_dir($filename)) {
        array_push($the_files,$filename);
    }
}
sort($the_files);
foreach($the_files as $the_file) : ?>
   <p><a href="../zpress/pages/<?php echo $the_file; ?>"><?php echo $the_file;?></a></p>
<?php endforeach; ?> 


...or this...
$the_files = array();
while(($filename = readdir($dir_list)) !== false) {
    if(!is_dir($filename)) {
        array_push($the_files,$filename);
    }
}
sort($the_files);
foreach($the_files as $the_file) {
    echo '<p><a href="../zpress/pages/' . $the_file . '">' . $the_file . '</a></p>';
}


...which I tend to find more readable when mixing languages.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1