Forum Moderators: coopster
I'm somewhat new to PHP. Not totally - I'd say I'm pretty mediocre at best. I have, however, managed to scrape together enough knowledge to write my first PHP script - an email form - that actually kicks ass (much to my amazement) and has been one of the most requested things I do. I get more requests for that script than I do for my web design skills. (How that reflects on my design skills, I don't know!)
Anyway, apparently the form instructions are quite convoluted - many people can't understand them (technical writer, I am not.) So I thought I'd try my hand at making it more "automated". So far, I've been pretty successful - things are rolling along nicely - but I've run into a bit of a snag that I can't seem to figure out.
What I'm doing is basically having the end user fill out a form. Based on the stuff they enter in the form fields, the output file will actually generate the code they need to use the form. As I said, I'm surprising myself by doing a pretty good job. But the snag I'm running into is this: I want to make room for any "extra fields" that may be needed. So I have a section that is a list of possible input types: text field, textarea, radio, checkbox and select. AT first, I was trying to so a multiple select list with an array, and it was *almost* doing what I needed it to do. But I've since decided this is a little too cumbersome, and maybe it would be better if I provided a list of options, and allowed the end user to just input a number in the field next to it (so, if they need 3 checkboxes, they'd just put "3" in the input box next to "how many checkboxes do you need?" in the form).
What this *should* generate is a line of corresponding code for each type. If more than one is chosen, how many lines there are will be adjusted to match (and each appended by +1).
So, for example (using the "checkbox" example above), if the end user needs 3 checkboxes, the output would see something like:
I need checkbox1 here, and name it checkbox1.
I need checkbox2 here, and name it checkbox2.
I need checkbox3 here, and name it checkbox3.
If the end user chooses 2 checkboxes and 2 text fields:
I need checkbox1 here, and name it checkbox1.
I need checkbox2 here, and name it checkbox2.
I need text1 here, and name it text1.
I need text2 here, and name it text2.
See where I'm going? All of these sections will output the same "sentence" - just the subject is different with each one (and increased by the value of 1).
Unfortunately, I'm getting stuck at trying to figure out exactly *how* to get this done. If I could put *all* possibilities in a single array, instead of 1 type at a time (so I could just input the "sentence" one time and have it auto-fill over and over with the right values), that would be even cooler - but I know that's probably asking for more than I can handle right now. But if it's a possibility, that would rock so hard!
The closest I can find to what I need it output_buffering (I think) - it's close, but no cigar. Would anyone be able to point me in the right direction for how to do this? I'd truly appreciate it.
<?php
function make_form($i_text,$i_checkbox,$i_radio) {
$form = null;
$num_types = [url=http://www.php.net/array]array[/url]($i_text, $i_checkbox, $i_radio);
$types = array('text','checkbox','radio');
$template = '<input type="_TYPE_" name="_NAME_" />';
for($i = 0; $i < 3; $i++) {
for($j = 1; $j <= $num_types[$i]; $j++) {
$input = [url=http://www.php.net/str-replace]str_replace[/url]('_TYPE_',$types[$i],$template);
$input = str_replace('_NAME_',$types[$i].$j,$input);
$form .= $input."<br/>\n";
}
}
return $form;
}
echo '<form>';
echo make_form(2,3,4);
echo '</form>';
?>
Have fun :)
P.S. Don't just copy and paste this code; understand it fully and even play around with it a little. Our goal here is to get you to learn the material, not to just use it. :)