Need syntax help using foreach () in an attempt to publish input

I am obviously a beginner coder, and I read various posts trying to do the following:

I have a form containing many <textarea> input fields, 150 rows and three columns (the reason why they <textarea> are associated with special javaScript functionality that I will not go into details with, but that matters <textarea> ) .

Here is a capture of how it looks (but before 150):

form capture in browser

I submit the form to another page, PHP, of course, where I want to do three things:

1) Display a summary of certain parameters (date of dispatch, amounts, calculations, etc.)

2) Assign variables corresponding to all 450 common inputs using loops somehow

3) Create, save and create a link for the user to download the CSV data file.



Step 1), if pretty much done, no dram there

Step 3) I am sure that I can work, as there are many good tutorials for writing files with php.

Step 2), where I am at a loss.




It is also important to note:

A) Not all lines must be completed when submitting the form; although a separate line should be completed

B) Posting (right word?) In a loop should be limited only by the maximum number of completed lines. Therefore, I do not think that the direct for() loop will work, since it will return all 150 lines, even if empty.



I played with a bit of code that I found here using foreach() , this post .

Here is the code that I combined Frankenstein, it returns ok (only for the first column {let call it column_A}, and it is limited by the number of fields filled using column_A as a ruler.

 foreach($_POST as $name => $inputA) { if(strpos($name, 'column_A')===0) { if($inputA !== ''){ echo $i , " )" , " " , $inputA , "<br />"; $i++; } } } 



Therefore, if I fill in 5 lines, with some data, then this code will return:

1) column A one

2) column A two

3) column A three

4) column A four

5) column A five



But I also need to echo the column column_B and column_C along column_A. as below:

1) column A one, column B one, column C one

2) column A two, column B two, column C two

3) column A three, column B three, column C three

4) column A four, column B four, column C four

5) column A five, column B five, column C five



Therefore, there may be an en echo statement, for example:

 echo $i , " )" , " " , $inputA , " " , $inputB, " " , $inputC , "<br />"; 

I tried to read in strpos() and for() and foreach() in the PHP manual, but I hardly get it. Sorry for a long time, wanted to paint a good picture.

+4
source share
4 answers

But I also need to echo also column_B and column_C along column column_A.

You should name your input fields in the distance, first of all, get a more convenient data structure.

You can add square brackets to get an array of input values ​​in PHP, and you can also specify indexes to use:

 name="field[0][A]" name="field[0][B]" name="field[1][A]" name="field[1][B]" 

Try this and then use var_dump($_POST['field']) to find out what data structure you will get.

0
source

Try the following:

 $lt = range('A', 'C'); foreach ($_POST as $name => $input) { foreach ($lt as $i => $letter) { echo $i, " )"; if (strpos($name, 'column_' . $letter) === 0) { if ($input !== '') { echo " ", $input; } } echo "<br />"; } } 
0
source

Maybe so?

 foreach($_POST as $name => $inputA) { $found = false; if(strpos($name, 'column_A')===0) { if($inputA !== ''){ echo $i , " )" , " " , $inputA; $found = true; } }elseif(strpos($name, 'column_B')===0) { if($inputB !== ''){ echo " " , $inputB; $found = true; } }elseif(strpos($name, 'column_C')===0) { if($inputC !== ''){ echo " " , $inputC; $found = true; } } if($found){ echo "<br />"; $i++; } } 
0
source

You mentioned that you don't want to use for to loop through each of 150 lines, but I think this is the best way. In this case, a loop with 150 iterations is not a problem.

As you go through POST , I know that you expect the order of the $_POST array to match the FORM order. Now it will be valid in most cases, but it is not guaranteed. Therefore, I would advise against using it.

To make it work, I would name the columns when creating the FORM with a row id so that it looks something like

 <form> <!--row 1 --> <textarea name='row_1_col_a'></textarea> <textarea name='row_1_col_b'></textarea> <textarea name='row_1_col_c'></textarea> <!--row 2 --> <textarea name='row_2_col_a'></textarea> <textarea name='row_2_col_b'></textarea> <textarea name='row_2_col_c'></textarea> <!-- continue with all rows --> </form> 

You can easily do this with PHP

 <form> <?php for($row=1;$row<=150;$row++) { ?> <!--row <?php echo $row; ?> --> <textarea name='row_<?php echo $row; ?>_col_a'></textarea> <textarea name='row_<?php echo $row; ?>_col_b'></textarea> <textarea name='row_<?php echo $row; ?>_col_c'></textarea> <?php } ?> </form> 

Now you can uniquely identify each column in each row. In PHP you can scroll through lines.

 <?php $completerows = 0; for($row=1;$row<=150;$row++) { //check the vars for the row. If its empty continue with next row //I dont use the empty() method to check incase you need to enter a 0 in one of the columns //col A if (!isset($_POST['row_'.$row.'_col_a']) || strlen($_POST['row_'.$row.'_col_a'])==0) continue; $colA = $_POST['row_'.$row.'_col_a']; //col B if (!isset($_POST['row_'.$row.'_col_b']) || strlen($_POST['row_'.$row.'_col_b'])==0) continue; $colA = $_POST['row_'.$row.'_col_b']; //col C if (!isset($_POST['row_'.$row.'_col_c']) || strlen($_POST['row_'.$row.'_col_c'])==0) continue; $colA = $_POST['row_'.$row.'_col_c']; //if you are here the entire row is filled. //do something with $colA, $colB and $colC $completerows++; } var_dump($completerows); ?> 
0
source

Source: https://habr.com/ru/post/1483730/


All Articles