Jump to content

Using php to create a form from a text file


hotrod57

Recommended Posts

am using php to write a function that reads in lines from a text file and creates a form with the corresponding input values. I have a text file that looks like this:

 

title = Test Page

checkbox = shoe

checkbox = comb

 

I can separate these lines into an array, ($array[0] = title, checkbox, checkbox, and $array[1] = Test Page, shoe, comb) but I can't seem to figure out how to get the function to print out each array with its value. For example: if my function reads the line "checkbox = shoe", I have the statement print ("<input type=checkbox name=userbox value=$check>$check");, which should print a checkbox on a form with its value being shoe. While testing, I got it to do that once, but it would only print out a checkbox with shoe like a million times on the page and never stop. It never reads the next line to see what the input will be. I tried to correct this, and now I can't even get it to print that out again. If anybody could help or show me where to get some help, it would be greatly appreciated. Here is where my code acts up:

 

<?php
$data = file('test_read.txt');
$line = explode("=", $data[1]);
while ($line[0] == 'checkbox'){
$check = $line[1]; 
print ("<input type=checkbox name=userbox value=$check>$check");
}
?>

Try something like this:

<?php

$lines = file('test_read.txt');

foreach ($lines as $line_num => $line) {
   list($type, $value) = explode(" = ", $line);

if($type == checkbox)
{
	echo "<input type=\"checkbox\" name=\"userbox\" value=\"$value\" /> $value";
}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.