Question

I am totally lost here, I made a text file with names in it. I would like to assign a array to each name and open it into a php file for displaying on a webpage so I can change things, I just cannot for the life of me figure this out.

$lines = file('responders.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); 
foreach ($lines as $line_num => $line) 
{ 
print "<input type='checkbox' name='responders[]' value='" . $line_num . "' >" . $line . " " . $line_num . "<br />\n"; 
}

My text file just has names example

Jon
Jim
Tim
Tom
Jerry

I would like to assign an array somehow to basically say

4 Jon
5 Jim
17 Tim
47 Tom
52 Jerry

Without having to use a bunch of white space per line. Is it at all possible? Thanks. In the end I would like to read my mysql database find out whos all listed and put a check mark next to people who is in the mysql field. I used Implode for that if that helps anybody.

Was it helpful?

Solution

If combine wouldn't work for you you could just explicitly declare the array k/v.

$lines = file('responders.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); 
$nameArray = array();
$i = 0;
foreach ($lines as $line) 
{ 
    echo $i . " > " . $line . "\n"; 
    $nameArray[i] = $line;
    $i++;
}

OTHER TIPS

There is a more easy way to achieve this:

$content = file_get_contents("responders.txt");
if(strlen($content) > 0) {
    $myData= explode(" ", $content);
    if(count($myData) > 0) {
        $c = 1;
        foreach($myData as $value) {
            echo $value . " -> ";
            if($c % 2 == 0) {
                echo "<br />";
            }
            $c++;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top