I'm new to PHP and trying to populate the contents of a flat file to a dropdown box. Clearly haven't a clue what I am doing, but here is what I have tried so far. The first part is there to confirm that I am actually opening the file and that there is something in it. And there is!

$fh = fopen("data.txt", "r"); 
while (!feof($fh) ) {
$line_of_text = fgets($fh);
$parts = explode('=', $line_of_text);
echo $parts[0] . $parts[1]. $parts[2] . "<BR>";    

}
// attempt to populate a dropdown list
echo "<select name=\"$parts\">\n";
foreach($fh as $parts) {
echo "($name, id, $choice, $part)\n";
}
echo "</select>\n"; 

Thaks for any suggestions.

Recommended Answers

All 13 Replies

How is the data structure in your flat data.txt for the options of the select?
value=text <line> ?

Hi. Good start))).
In 5 line you must not show data and must write their in array.

$lines[] = array($part[0], $part[1], $part[2]);

Then you must in a loop to go through the array and will show all elements.

if(!empty($lines))
{
    echo "<select name=\"$parts\">\n";
    foreach($lines as $line)
    {
        echo "<option id=\"$id\">$value</option>";
    }
    echo "</select>\n";
}

All code entirely.

$lines = array();
$fh = fopen("data.txt", "r"); 
while (!feof($fh) ) 
{
    $line_of_text = fgets($fh);
    $parts = explode('=', $line_of_text);
    $lines[] = array($part[0], $part[1], $part[2]);  
}

if(!empty($lines))
{
    echo "<select name=\"$parts\">\n";
    foreach($lines as $line)
    {
        echo "<option id=\"$id\">$value</option>";
    }
    echo "</select>\n";
}

radow what are the $id and $value inside the foreach($lines as $line) ? Where do you define them ? How could you know the data structure of the data.txt ?

You split each line of it on “=” as delimiter and get an array out of it ($parts) in my understanding this logically should have two elements , but then you push a new array to the $lines array with three first elements of the $parts array. So what will be the $lines[0][3] ?

Thanks to both of you. I tested the code supplied and it results in an empty dropdown box. I presumed that I should be able to simply paste it into a php file without alterations and see it work. Forgive me. I don't even know what questions to ask! Thanks again.

watonca , this is an easy question but to give you a correct answer please provide at least one line of the data.txt and what that mean for you.

radow what are the $id and $value inside the foreach($lines as $line) ? Where do you define them ?

I nowhere didn't define this variable, instead their values from array $line ($line[0] or $line[1] or $line[2] depend from struct data.txt)

You split each line of it on “=” as delimiter and get an array out of it ($parts) in my understanding this logically should have two elements , but then you push a new array to the $lines array with three first elements of the $parts array. So what will be the $lines[0][3] ?

Part of my example I take from code watonca, which he wrote in him first post.
If watonca an example struct data.txt I could will write complete example.

Member Avatar for diafol

f watonca an example struct data.txt I could will write complete example.

Perhaps if we steer watonca to write his/her own solution?

Thanks everyone.

Here is what a couple of lines of data from the data.txt file looks like:

Green||ID9999||Tennis||Wilson
Bender||ID9998||Golf||Thomas

Which will information must populate dropdownlist?
First word and ID, or all words and ID or yet as anything.
ID must will be whole word (ID9998) or only number (9998)?

The ID in the second field is redundant so it could be eliminated if it makes any difference. It is just a relic from a paper system. All fields need to have some entry.

Member Avatar for diafol

Using file(), you can read over lines one at a time:

$lines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$options = "";
foreach ($lines as $line) {
    list($somename, $id, $sport, $make) = explode("||",$line);
    $options .= "<option..."; //you can figure this out with $id, $make etc... 
}

Thanks again to all of you. Sorry but I had to drop out of this for a while.

diafol, you give me far more credit that I deserve.

I have tried a couple of combinations of data in the line and have failed to come up with anyting that works.

Here was what I thought the most likely solution was....

$options .= "<$name . $id . $choice . $part>";

I have spent many hourssearching the Internet forums and tutorials as well as a couple of textbooks on PHP that are on my desk and have not been able to undersatnd the syntax to make this workfor me. It is not for lack of effort. Just a lack of talent and experience. Sorry to be so dense! My struggle continues.

Member Avatar for diafol
$lines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$options = "";
foreach ($lines as $line) {
    list($somename, $id, $sport, $make) = explode("||",$line);
    $options .= "\n\t<option value='$id'>$name $choice</option>"; 
    // \n\t just formats the output in html view
    //I have no idea what you need displayed as your dropdown items
}

Your options are now stored in the $options variable. You can echo it out anywhere in your code, but you'd want to do this inside your select tags:

<select id="...">
    <?php echo $options;?>
</select>

Ok, no more feeding, my nipples are sore :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.