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");
}
?>

Link to comment
Share on other sites

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";
}
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.