Populating popup menus with text file - Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Populating popup menus with text file - Question

Post by mikebr »

:( I am learning PHP and have encountered a problem with populating popup menus with lines from a text file, the menus load the text lines from the .txt file but they are all on the same line, for example the text from one of the files would be:

John
Simon
Peter
George
Andy

and the menu would populate as:

-- Employee Name --
JohnSimonPeterGeorgeAndy

when it should populate as:

-- Employee Name --John
Simon
Peter
George
Andy

I have posted the sample code I am using below, could some helpful person point me in correcting this problem, I am using the latest version of PHP on my "localhost" home computer.

Regards
Mike

_________ CODE FOR EMPLOYEE.PHP

<HTML>
<BODY>
<CENTER><H3>Delivery ETC...<BR>
Vote For Employee Of the Month</H3></CENTER>
<FORM METHOD=GET ACTION=get_nomination.php>
<?php
echo (date("[F]"));
if (!$file_op_names = fopen("./tmp/NAMES.txt","r")) {
echo "Sorry I cannot open the file NAMES.txt";
echo "<BR>An error has occurred, please contact the administrator";
exit;
}

if (!$file_op_dept = fopen("./tmp/DEPT.txt","r")) {
echo "Sorry I cannot open the file DEPT.txt";
echo "<BR>An error has occurred, please contact the administrator";
exit;
}

$lines=file("./tmp/NAMES.txt");
echo "<BR><B>Employee Name :<B>";
echo "<SELECT NAME=names >";
echo "<OPTION>-- Employee Name --";
for ($loop=0; $loop < count($lines); $loop++)
{
echo "<OPTION>$lines[$loop]";
}
echo "</SELECT>";

# read from a file and populate a menu
$lines=file("./tmp/DEPT.txt");
echo "<B>Employee Department :</B>";
echo "<SELECT NAME=dept>";
echo "<OPTION>-- Department --";
for ($loop=0; $loop < count($lines); $loop++)
{
echo "<OPTION>$lines[$loop]";
}
echo "</SELECT>";

echo "<BR><BR>";
echo "<B>General Comments</B><BR>";
echo "<TEXTAREA COLS=\"40\" ROWS=\"5\" NAME=\"comment\"></TEXTAREA>";
fclose($file_op_names);

fclose($file_op_dept);
?>

<BR><BR>
<INPUT TYPE="SUBMIT" VALUE="Send me the details !">
<INPUT TYPE="RESET" VALUE="Clear me!">
</FORM>
</BODY>
</HTML>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

maybe this quote from the file() manual page is helpful
Note: If you are having problems with PHP not recognizing the line endings when reading files either on or created by a Macintosh computer, you might want to enable the auto_detect_line_endings run-time configuration option.
  • and some 'style' tips
  • there's no need to (f)open the files you want to read with file(). you can do the error check with $lines = file('abcd.txt') or die('...file not found...');
  • if you use a highlighting editor or even debugger echo '<OPTION>',$lines[$loop],'</OPTION>' might be more readable
  • when you open a tag -please- also close the tag <OPTION>...</OPTION>
  • and if you do this, also close the no-content tags as well <br/> <input type.... /> (your nearest xml/xhtml-representative will be gratefull ;)
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Post by mikebr »

Yes I am using a Mac and I guess the file you refer to is in the php.ini file which I believe I need to alter with terminal so I will look into that.

I have just started to learn php so I am following the guidelines of the book I am learning from at the moment.

Thanks for the tips
Post Reply