-5

I have some form values in an array, and need to get them into table rows and could use some guidance/help. Below is what I have and am not sure if I am on the right path or not:

//These are my form values
$part = $_POST['part'];
$rel = $_POST['rel'];
$chart = $_POST['chart'];
$dob = $_POST['dob'];
$age = $_POST['age'];
$gender = $_POST['gender'];

//If participant (part) is not empty, start building the table  
if ($part != "") {

//The table header (not worried about <td>/<th> semantics right now
$participants = "Participants<table border='1'>
    <tr><td>Name</td><td>Relationship</td><td>Chart #</td><td>DOB</td> <td>Age</td><td>Gender</td></tr>";

//This is where I am lost...looping over and outputting on a row by row basis       
foreach($part as $row) {
    $participants_table = "<tr><td>". $part . "</td><td>". $rel ."</td><td>". $chart ."</td><td>". $dob ."</td><td>". $age ."</td><td>". $gender ."</td></tr>";
    }
} else {
//If there are no names in the participant column(s), display the following
$participants = "No Other Participants";
$participants_table = "";
}

//Output from print_r Next, the following may be overkill, but here goes: Array ( [0] => Christine Eubanks [1] => Ariel Eubanks [2] => Synthia Clow [3] => [4] => [5] => [6] => ) Array ( [0] => Wife [1] => Daughter [2] => Daughter [3] => [4] => [5] => [6] => ) Array ( [0] => 123456 [1] => 654321 [2] => 543210 [3] => [4] => [5] => [6] => ) Array ( [0] => 04/03/1974 [1] => 07/21/2004 [2] => 12/28/1995 [3] => [4] => [5] => [6] => ) Array ( [0] => 44 [1] => 14 [2] => 22 [3] => [4] => [5] => [6] => ) Array ( [0] => F [1] => F [2] => F [3] => [4] => [5] => [6] => ) Array ( [0] => Christine Eubanks [1] => Ariel Eubanks [2] => Synthia Clow [3] => [4] => [5] => [6] => ) Array ( [0] => Wife [1] => Daughter [2] => Daughter [3] => [4] => [5] => [6] => ) Array ( [0] => 123456 [1] => 654321 [2] => 543210 [3] => [4] => [5] => [6] => ) Array ( [0] => 04/03/1974 [1] => 07/21/2004 [2] => 12/28/1995 [3] => [4] => [5] => [6] => ) Array ( [0] => 44 [1] => 14 [2] => 22 [3] => [4] => [5] => [6] => ) Array ( [0] => F [1] => F [2] => F [3] => [4] => [5] => [6] => ) Array ( [0] => Christine Eubanks [1] => Ariel Eubanks [2] => Synthia Clow [3] => [4] => [5] => [6] => ) Array ( [0] => Wife [1] => Daughter [2] => Daughter [3] => [4] => [5] => [6] => ) Array ( [0] => 123456 [1] => 654321 [2] => 543210 [3] => [4] => [5] => [6] => ) Array ( [0] => 04/03/1974 [1] => 07/21/2004 [2] => 12/28/1995 [3] => [4] => [5] => [6] => ) Array ( [0] => 44 [1] => 14 [2] => 22 [3] => [4] => [5] => [6] => ) Array ( [0] => F [1] => F [2] => F [3] => [4] => [5] => [6] => ) Array ( [0] => Christine Eubanks [1] => Ariel Eubanks [2] => Synthia Clow [3] => [4] => [5] => [6] => ) Array ( [0] => Wife [1] => Daughter [2] => Daughter [3] => [4] => [5] => [6] => ) Array ( [0] => 123456 [1] => 654321 [2] => 543210 [3] => [4] => [5] => [6] => ) Array ( [0] => 04/03/1974 [1] => 07/21/2004 [2] => 12/28/1995 [3] => [4] => [5] => [6] => ) Array ( [0] => 44 [1] => 14 [2] => 22 [3] => [4] => [5] => [6] => ) Array ( [0] => F [1] => F [2] => F [3] => [4] => [5] => [6] => ) Array ( [0] => Christine Eubanks [1] => Ariel Eubanks [2] => Synthia Clow [3] => [4] => [5] => [6] => ) Array ( [0] => Wife [1] => Daughter [2] => Daughter [3] => [4] => [5] => [6] => ) Array ( [0] => 123456 [1] => 654321 [2] => 543210 [3] => [4] => [5] => [6] => ) Array ( [0] => 04/03/1974 [1] => 07/21/2004 [2] => 12/28/1995 [3] => [4] => [5] => [6] => ) Array ( [0] => 44 [1] => 14 [2] => 22 [3] => [4] => [5] => [6] => ) Array ( [0] => F [1] => F [2] => F [3] => [4] => [5] => [6] => ) Array ( [0] => Christine Eubanks [1] => Ariel Eubanks [2] => Synthia Clow [3] => [4] => [5] => [6] => ) Array ( [0] => Wife [1] => Daughter [2] => Daughter [3] => [4] => [5] => [6] => ) Array ( [0] => 123456 [1] => 654321 [2] => 543210 [3] => [4] => [5] => [6] => ) Array ( [0] => 04/03/1974 [1] => 07/21/2004 [2] => 12/28/1995 [3] => [4] => [5] => [6] => ) Array ( [0] => 44 [1] => 14 [2] => 22 [3] => [4] => [5] => [6] => ) Array ( [0] => F [1] => F [2] => F [3] => [4] => [5] => [6] => ) Array ( [0] => Christine Eubanks [1] => Ariel Eubanks [2] => Synthia Clow [3] => [4] => [5] => [6] => ) Array ( [0] => Wife [1] => Daughter [2] => Daughter [3] => [4] => [5] => [6] => ) Array ( [0] => 123456 [1] => 654321 [2] => 543210 [3] => [4] => [5] => [6] => ) Array ( [0] => 04/03/1974 [1] => 07/21/2004 [2] => 12/28/1995 [3] => [4] => [5] => [6] => ) Array ( [0] => 44 [1] => 14 [2] => 22 [3] => [4] => [5] => [6] => ) Array ( [0] => F [1] => F [2] => F [3] => [4] => [5] => [6] => )

  • Well I guess my first question is: Is `$_POST['part']` an array or are you purely looking to output the 6 variables defined at the top? – IsThisJavascript Aug 13 '18 at 15:15
  • 3
    These types of questions are all over SO, please research more before posting. You should be checking if these elements exist before trying to use them. – mickmackusa Aug 13 '18 at 15:16
  • You should use some sort of validation for the (required) fields. What type of database are you using? – Bas van Dijk Aug 13 '18 at 15:18
  • It is an array. I have 7 rows with columns of part, rel, chart, dob, age, gender – John Eubanks Aug 13 '18 at 15:18
  • _Am I doing this right_ is not really the type of question that we handle here. That caters to opinionated answers. Please read about how to ask a question here. Take the tour. I'm going with Too Broad this time. – mickmackusa Aug 13 '18 at 15:19
  • Not using a database, just need to write the table data out. So, if three people fill out the fields, I have three rows of data. If 1 person fills it out, one row of data, etc. – John Eubanks Aug 13 '18 at 15:21
  • You guys are so quick to close a topic without offering any help besides "do more googling". Cmon don't you remember how challenging this stuff was to understand when you first started? The question is written well and with good intentions. – IsThisJavascript Aug 13 '18 at 15:23
  • @JohnEubanks can you post the result of `print_r($_POST)` so we can get a better handle on your data structure? ( put it in the OP please, dont post it in the comments ) – IsThisJavascript Aug 13 '18 at 15:23
  • IsThisJavascript, many thanks for assisting. I put the output in the OP. – John Eubanks Aug 13 '18 at 15:37
  • Yikes, please hightlight that output and Ctrl+K to format as code. Help us to see. I think you want a single foreach loop on `$_POST` then implode with `<\td>` (after valudating/sanitizing the data). – mickmackusa Aug 13 '18 at 15:37
  • Wrapping your code block in backticks does not improve readability. – mickmackusa Aug 13 '18 at 15:44
  • Duplicates that I found while Googling `output POST array as table in php site:stackoverflow.com` : https://stackoverflow.com/a/45800921/2943403 https://stackoverflow.com/q/20661894/2943403 https://stackoverflow.com/q/2465046/2943403 https://stackoverflow.com/q/13961388/2943403 – mickmackusa Aug 13 '18 at 15:48

2 Answers2

0

when ever you are receiving an array you have to loop it using for loop or foreach loop, Since you have assigned values to variables like $part = $_POST['part']; which is not present, it will always throw error. And in your code $part is not an array to loop through.

<?php
//$part = $_POST[];
$part = array(
  array('part', 'rel', 'chart', 'dob', 'age', 'gender'),
  array('part1', 'rel1', 'chart1', 'dob1', 'age1', 'gender1')
);

if ($part != "") {
  //The table header (not worried about <td>/<th> semantics right now
  $participants = "Participants<table border='1'><tr><td>Name</td><td>Relationship</td><td>Chart #</td><td>DOB</td> <td>Age</td><td>Gender</td></tr>";

  //This is where I am lost...looping over and outputting on a row by row basis       
  foreach ($part as $value) {
    $participants .= "<tr><td>" . $value[0] . "</td><td>" . $value[1] . "</td><td>" . $value[2] . "</td><td>" . $value[3] . "</td><td>" . $value[4] . "</td><td>" . $value[5] . "</td></tr>";
  }

  $participants .= '</table>';
}
else {
  //If there are no names in the participant column(s), display the following
  $participants = "No Other Participants";
}
echo $participants;
?>
Viswanath Polaki
  • 1,357
  • 1
  • 10
  • 19
0

Whatever you used in your print_r is what I'm focusing on here for this solution. So if you did print_r($_POST) then this would work:

I use htmlspecialchars to clean the string to prevent nasty xss injections.

<?php

foreach($_POST as $row) {
    $part = htmlspecialchars($row[0], ENT_QUOTES, 'UTF-8');
    $rel = htmlspecialchars($row[1], ENT_QUOTES, 'UTF-8');
    $chart = htmlspecialchars($row[2], ENT_QUOTES, 'UTF-8');
    $dob = htmlspecialchars($row[3], ENT_QUOTES, 'UTF-8');
    $age = htmlspecialchars($row[4], ENT_QUOTES, 'UTF-8');
    $gender = htmlspecialchars($row[5], ENT_QUOTES, 'UTF-8');
    $unkown = htmlspecialchars($row[6], ENT_QUOTES, 'UTF-8'); //your output shows a [6]'th element

    $participants_table = "<tr><td>". $part . "</td><td>". $rel ."</td><td>". $chart ."</td><td>". $dob ."</td><td>". $age ."</td><td>". $gender ."</td></tr>";

}

What's most interesting to me is that in your datadump, there's no associative keys, hence why numerical keys are used instead

IsThisJavascript
  • 1,726
  • 2
  • 16
  • 25