trying to create an array based on a files data... then pull into foreach

I am trying to pull data from a file, in this case we’ll call it FILE.txt. Here’s it’s data:

item1=value1
item2=value2
;item3=value3
item4=value4
item5=value5
;item6=value6
;item7=value7

What I am trying to do is parse the data from the file into a form. For example:

    <label for="item1">item1</label>
    <input type="text" value="value1" name="item1">
..... (so on and so forth for each item found in the file.)

I have been racking my brain trying to get this figured out, and have just about given up. I’ve scrapped my code since it appears to not work at all.

I’m also trying to make it so that it will ignore values that start with a semi-colon ( ; ). those won’t be parsed into the form that’s generated.

Any help you can give would be greatly appreciated.
Happy Holidays.

[member=71845]JimL[/member] Do you have any ideas. You always seem to be my saving grace. HAHA.

its always a good idea to include whatever code you have. You can get advice on what you could/should have done differently, and it shows you’ve given.it a ggo and aactually tried yourself.

Regarding the code itself it should be pretty easy, read the file line by line, if it starts with a semi colon then continue to the next iteration (line). If not then explode by = and do whqtever you need with the two values

[member=71845]JimL[/member] Luckily, I was still able to recover my code :wink:

[php]
$fopen = fopen($file, r);
$fread = fread($fopen,filesize($file));
fclose($fopen);
$remove = “\n”;
$split = explode($remove, $fread);

$array[] = null;
$tab = “=”;

foreach ($split as $string) {
$row = explode($tab, $string);

foreach ($row as $setting) {
echo $setting."
";
}
}
[/php]

I’m getting an array inside of an array, but that’s not really what I want, as you can see by my example. I may have had one to many at dinner yesterday (xmas). I think I forgot how to code LOL.

[php]$items = [];

$handle = fopen($file, ‘r’);
while (($line = fgets($handle)) !== false)
{
if (strpos($line, ‘;’) === 0) {
continue;
}

list($key, $value) = explode('=', $line);
$items[$key] = $value;

}

fclose($handle);

foreach ($items as $key => $value): ?>
<?= $key ?>

<?php endforeach; ?>[/php]

Im sure you use Git or some other version control so wasnt expecting anything else ^^

Haha doesn’t everyone? :stuck_out_tongue:

I hope so :slight_smile:

[member=71845]JimL[/member] I just tried your code and I’m getting and unable to parse error.

Parse error: syntax error, unexpected '[' in /var/www/html/include/functions.inc.php on line 1319

That line contains the following:

[php]$items = [];[/php]

Not sure why that’s happening. This server (my dev server) is running PHP 5.3. I’m assuming that could be causing the issue.

I believe that syntax started in 5.6, convert it to the older array syntax.

I changed it to array() and it worked immediately. Thanks for the heads up, and thanks [member=71845]JimL[/member] for the speedy resolution as usual!

Sponsor our Newsletter | Privacy Policy | Terms of Service