-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] => )

12
  • 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? Commented Aug 13, 2018 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. Commented Aug 13, 2018 at 15:16
  • You should use some sort of validation for the (required) fields. What type of database are you using? Commented Aug 13, 2018 at 15:18
  • It is an array. I have 7 rows with columns of part, rel, chart, dob, age, gender Commented Aug 13, 2018 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. Commented Aug 13, 2018 at 15:19

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

IsThisJavascript, many thanks. All seems to be working now. Thank you for pointing out the 6th element. Looking to see where it is coming from.
Happy to hear, happy coding @JohnEubanks
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;
?>

1 Comment

Remove the sample data and un-comment the $_POST[] line while using this code

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.