Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Another question that has probably been asked a 1000 times

Status
Not open for further replies.

IamStang

Technical User
Feb 12, 2005
27
0
0
US
Using the following example:
Code:
$myFile = "optionList.txt"; 
$myOptions = fopen($myFile, "r"); 
$lock = flock($myOptions, LOCK_SH);
if ($lock) { 
 $options_read = fread($myOptions, filesize($myFile));
 $lock = flock($myOptions, LOCK_UN); } 
fclose($myOptions); 
$exp_Options = explode(",", $options_read);
$number = 20;
$x = 0;
while ($x < $number) {
$namenumber = $x + 1;
echo "<option value = '$exp_Options[$x]'>$exp_Options[$x]</option>";
++$x;
}

Is it possible to rewrite this so that $number will equal the correct number of entries that were exploded, regardless of how many there are at the time?

I am trying to work this so that I do not need to go in and edit the variable $number everytime the text file changes.

This snippet is part of a code for filling in an option list (dropdown) and may contain, on any given day, from 1 to 20 entries. Leaving $number equaling 20 outputs a dropdown with (at times) 3 actual entries and 17 blank ones. Which just looks really tacky in my opinion.

Thanks in advance!
IamStang
 
I would iterate the array with foreach:
Code:
foreach($exp_Options as $key => $value){
   echo "<option value = '$value'>$value</option>";
}
No counting necessary.
 
For crying out loud! Why didnt it occur to me to use a foreach statement? After all, I just got through coding another part of my script using it. LOL

Thanks DRJ478 for hooking the truck up and pulling my head out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top