Paul Haynes Paul Haynes - 5 months ago 12
PHP Question

Process HTML form values and output results to text files

Usually I can find solutions to similar problems online to come up with a solution to my own problem but the more I think about/research this, the more confused I get!

Happy to try & piece together my own solution (and post here for review/reference) but would appreciate some guidance on how to approach.

What I am trying to achieve:

From a simple HTML form containing the following fields:


  • Start No.

  • End No.

  • Start Letter

  • End Letter



pass the submitted information to a PHP script for processing.

Example values for the HTML form:


  • Start No. = 0

  • End No. = 3

  • Start Letter = A

  • End Letter = D



By entering only start and end numbers/letters the script would ascertain that in addition all values in-between those submitted are required for further processing steps/output i.e. 1,2 & B,C. I am assuming this information would be stored within either a single array or multiple arrays.

If all I wanted to do with the stored array(s) information was output a single text file with all possible letter+number combinations that looked similar to:

A0 A1 A2 A3
B0 B1 B2 B3
C0 C1 C2 C3
D0 D1 D2 D3


then that would be the end of my question and perhaps the first thing to try to achieve however what I require is the data to be split into 4 separate files as below:

files:

File1.txt

A0 B0 C0 D0


File2.txt

A1 B1 C1 D1


File3.txt

A2 B2 C2 D2


File4.txt

A3 B3 C3 D3


Breaking my question down into steps:


  1. By submitting only start/end numbers/letters how can I ascertain the letters/numbers in-between (range)?



Answer: Use

range()



  1. How can I then establish all possible letter+number combinations?



No longer think I need to do but would involve merging arrays


  1. How can I then group by number value and output?



Handled by foreach function

Update:

My code below produces the first output file.

File1.txt

A0
B0
C0
D0


You will notice in the foreach function I am specifically asking for the first value of $array2. How can I instead repeat/loop the foreach and echo function for every value stored in $array2? (I know I will also need to modify further to ensure the text files created have unique filenames.)

<?php

//Example form values
$start_number = 0;
$end_number = 3;
$start_letter = 'A';
$end_letter = 'D';

//Letters array
$array1 = range($start_letter, $end_letter,1);

//Numbers array
$array2 = range($start_number, $end_number,1);

foreach($array1 as $key => $value) {
$string .= $value . $array2[0] . "\n";
}

echo file_put_contents("File1.txt",$string);

?>


Working Example:

<?php

//Example passed form values
$start_number = 0;
$end_number = 8;
$start_letter = 'A';
$end_letter = 'D';

//Numbers array
$array1 = range($start_number, $end_number,1);

//Letters array
$array2 = range($start_letter, $end_letter,1);

foreach($array1 as $key => $value) {

$string = implode($array2, $value . "\n") . $value;

echo file_put_contents($value . ".txt",$string);
}

?>


I am not convinced this is the best solution by using implode() but it works for my requirements, maybe somebody may find this post useful. Please do suggest improvements.

Answer
<?php

//Example passed form values 
$start_number = 0;
$end_number = 8;
$start_letter = 'A';
$end_letter = 'D';

//Numbers array
$array1 = range($start_number, $end_number,1);

//Letters array
$array2 = range($start_letter, $end_letter,1);

foreach($array1 as $key => $value) {

    $string = implode($array2, $value . "\n") . $value;   

    echo file_put_contents($value . ".txt",$string);
}

?>
Comments