473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Read From an Array and Create Form

I have a text file that is changeable that I want to use to create a form with. Here is my text file, called 'test_read.txt":

Expand|Select|Wrap|Line Numbers
  1. title = Test Form;
  2. checkbox = shoe;
  3. checkbox = hair;
  4. radio_button = 3;
  5. comment_box
  6. dropbox = 18-29;
  7. dropbox = 30-49;
  8. dropbox = 50-75;
  9. textbox = 50;
Now my PHP code, right now, is just supposed to print the info out for test purposes. My problem is that it only prints the two checkboxes with no values for them. My next question is can I put the <form></form> tags in this same file, or will I need to create another file for it? Here is my PHP code:

[PHP]<?php
$lines = file('test_read.txt');

foreach ($lines as $line) {
list($field, $value) = explode("=", $line);

$value = substr(trim($values), 0, 1);
echo get_html($field, $value);

}

function get_html($tag, $value) {
switch(trim($tag)) {
case 'checkbox':
return "<input type=\"checkbox\" name=\"userbox\" value=\"$value\">$value";
break;

case 'radio':
return "<input type=\"radio\" name=\"userbox\" value=\"$value\">$value";
break;
// more cases below if needed.
}
}
?>[/PHP]
Oct 25 '07 #1
2 1444
Atli
5,058 Expert 4TB
Hi.

That code should work fine, with two minor exceptions.

First, this line doesn't make sense to me:
Expand|Select|Wrap|Line Numbers
  1. $value = substr(trim($values), 0, 1);
  2.  
What is this supposed to do? Does a variable called $values even exist in your code?

Second, the fields and the values you have there could both include white-spaces on each side. I would use the trim() function to clean both of them before using them.

As to the <form> fields. You can add them directly into the PHP code, before and after the foreach loop.
You could also add them to the text file and add a case to you switch that handles them.

In this particular case, I would go with the second option.
Oct 25 '07 #2
do it this way and it will work
Expand|Select|Wrap|Line Numbers
  1. list($field, $value) = explode(" = ", str_replace(";", "", $line));
and fix your $values variable, which if you do it this way, you won't need to trim it because the explode is taking consideration of the white space.
Oct 26 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Nick | last post by:
Loop to create an array from a dynamic form. I'm having trouble with an application, and I'll try to explain it as clearly as possible: 1. I have a form with two fields, say Apples and...
9
by: Randell D. | last post by:
Folks, I can program fairly comfortably in PHP and can, for the most part using these skills and others that I've picked up over the years manage to read/understand most code in Javascript... so...
14
by: Randell D. | last post by:
Folks, Here's my problem: I am createing an array in a file using the reslut of some PHP and MySQL processing. An example of my array would be examlpe="example one"; examlpe="example...
0
by: CroDude | last post by:
Hi all! I have problems when writting bitmap to a byte array and after reading it back form byte to Bitmap object. What I do is this: First I throw Bitmap to a memory-stream and then I write it...
3
by: Mark Dunmill | last post by:
I can't create a Constant/Read-only array field in managed C++ classes - doesn't allow the keyword const pointer to const object on array fields in managed C++ classes. e.g. Want to define a...
10
by: Danny | last post by:
I am working on a project where I will receive xml documents from clients machines as a byte array. They will use the web browser navigate method to post the data to my ASP.NET page. I then pick up...
5
by: Richard Lewis Haggard | last post by:
I am trying to create multi-dimensioned arrays in conventional ASP pages and pass these arrays as arguments to functions that are in a C# interop assembly. ASP complains because it doesn't...
3
by: assgar | last post by:
Hi I thought I had resolved this problem but I am intermittently the receving the warnings below. This code consist of a 1) HTML form, 2) a display function on the HTML form and 3) a...
4
by: assgar | last post by:
Hi Seasons Greetings Its back, I am being haunted. I thought I had resolved this problem but I am intermittently the receving the warnings below. This code consist of a 1) HTML form, 2)...
3
by: David K in San Jose | last post by:
I'm using managed (CLR) C++ in VS2005 to create a Windows app that contains a form named "MyForm". In the code for that form I'm trying to invoke some static functions by using an array of function...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.