Results 1 to 5 of 5

Thread: Seek help in modifying PHP code

  1. #1
    Join Date
    Mar 2007
    Posts
    27
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Seek help in modifying PHP code

    I used a free guestbook created by a student. On the submit.php page, there is the following codes for the selection (drop down) menu of pictures for user to choose.

    PHP Code:
    <?
    $data_file
    =opendir('./image/face'); 
    while (
    $all_file=readdir($data_file)) {
      if (
    $all_file==".") continue;
      if (
    $all_file=="..") continue;
      echo 
    "<option value='image/face/".$all_file."'>$all_file</option>";
    }
    closedir($data_file); 
    ?>
    </select><img id="R1" src="image/face/paaa1.gif"></td>
      </tr>
    <?
    $f
    =fopen("pn_list.dat","r");
    $r=fread($f,filesize("pn_list.dat"));
    $main=stripslashes($r);
    ?>
    The pictures are stored in "http://domain.com/image/face/...gif"
    and the location with the name of the pictures are stored in pn_list.dat
    (RHS:location of the picture;LHS:year the pictures were taken)
    paaa1.gif∥<1998>
    paaa2.gif∥<1998>
    paaa3.gif∥<1998>
    paaa4.gif∥<1998>
    paaa5.gif∥<1998>
    paab1.gif∥<1999>
    paab2.gif∥<1999>
    paab3.gif∥<1999>
    paac1.gif∥<2000>
    paac2.gif∥<2000>
    paad1.gif∥<2001>
    paad2.gif∥<2001>
    paad3.gif∥<2001>

    Could someone advise on how to modify the above script so that the pictures in the selection (drop down) menu of submit.php will appear in the above numerical/alphebetical sequence? Many thanks in advance.

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Store the contents in an array and use sort();
    Code:
    <?
    $data_file=opendir('./image/face'); 
    $arr = array();
    while ($all_file=readdir($data_file)) {
      if ($all_file==".") continue;
      if ($all_file=="..") continue;
      array_push($arr,$all_file);
    }
    sort($arr);
    for ($i=0;$i<count($arr);$i++) {
      echo "<option value='image/face/".$arr[$i]."'>$arr[$i]</option>";
      }
    closedir($data_file); 
    ?>
    </select><img id="R1" src="image/face/paaa1.gif"></td>
      </tr>
    <?
    $f=fopen("pn_list.dat","r");
    $r=fread($f,filesize("pn_list.dat"));
    $main=stripslashes($r);
    ?>
    Untested, but should work. If there's any problems just ask.
    - Mike

  3. #3
    Join Date
    Mar 2007
    Posts
    27
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default

    Thank you so much for your prompt response. I've tested it out but the pictures shown in the selection drop down menu of "submit.php" are still not in the alphatical/numerical order.

  4. #4
    Join Date
    Mar 2007
    Posts
    27
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default

    I tested the above code that Mburt provided on a free web host and it works OK but not the webhost that I used which is a paid host.
    Both use linux server. I believe there is something to do with my web hosting company? Does anyone has any idea?

  5. #5
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Sorry for the late response, but what seems to be the problem with the one that does not work (error messages, page not displaying, etc). Please post back if the problem isn't resolved already.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •