Log in

View Full Version : Seek help in modifying PHP code



jiam1
03-06-2007, 12:04 PM
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.


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

mburt
03-06-2007, 12:16 PM
Store the contents in an array and use sort();

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

jiam1
03-06-2007, 12:46 PM
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.

jiam1
03-07-2007, 02:52 AM
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?

thetestingsite
03-21-2007, 03:25 AM
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.