user3733831
user3733831

Reputation: 2936

Creating HTML table from PHP array

This is how i define my array from $_POST

$destination = $_POST['destination'];
$depart_date = $_POST['depart_date'];

print_r result as below:

Array
(
    [destination] => Array
        (
            [0] => a
            [1] => b
            [2] => c
            [3] => d
        )

    [depart_date] => Array
        (
            [0] => 2019-06-04
            [1] => 2019-06-06
            [2] => 2019-06-13
            [3] => 2019-06-22
        )
)

Now I want to echo these arrays into a HTML table.

This is how I tried it:

if (is_array($destination)) {       
    $dtble = "<table>
                <tr>
                  <th>Destination</th>
                  <th>Depart Date</th>
                </tr>";
    foreach($destination as $k => $v){
      $d = $depart_date[$k];
      $dtble .= "<tr>
                   <td>{$v}</td>
                   <td>{$d}</td>
                 <tr>";
    }
    $dtble .= "</table>";
}

But its give an output something like this:

Destination     Depart Date
a               2019-06-04
b               2019-06-06
c               2019-06-13
d               2019-06-22
                2019-06-04

** NEW UPDATES** This is how I defined those two arrays and its output:

$destination = array_values(array_unique($_POST['destination'])); 
if (is_array($destination)) {
    $destination[]  = filter_input(INPUT_POST, 'destination', FILTER_SANITIZE_STRING); 
} 

echo '<pre>',print_r($destination).'</pre>';

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => 
)
1

// Depart Date
if (!empty($_POST['depart_date'])) { 
    $depart_date = array_values(array_unique($_POST['depart_date']));// to remove duplicate and re-index array
    if (is_array($depart_date)) {
        foreach($depart_date as $dt){
          if (preg_match("/\d{4}\-\d{2}-\d{2}/", $dt)) {  
            $depart_date[] = $dt;
          } else{
                array_push($errors, '- Depart date is NOT in required format!');
          }
        }
    } 
}

echo '<pre>',print_r($depart_date).'</pre>';

Array
(
    [0] => 2019-06-03
    [1] => 2019-06-04
    [2] => 2019-06-05
    [3] => 2019-06-06
    [4] => 2019-06-03
    [5] => 2019-06-04
    [6] => 2019-06-05
    [7] => 2019-06-06
)

You can see date 2019-06-04 is duplicating. Can anybody tell how to figure this out.

Upvotes: 2

Views: 98

Answers (3)

Sujeet
Sujeet

Reputation: 3810

You can use array_combine() to combine them in an associative array. Then you can easily loop over them. Make sure you have depart date for every destination and vice versa.

<?php

   $destination = $_POST['destination'];
   $depart_date = $_POST['depart_date'];
   echo '<table>';
   echo '<tr>';
   echo '<th>Destination</th>';
   echo '<th>Depart Date</th>';
   echo '</tr>';
   foreach(array_combine($destination,$depart_date) as 
   $destination=>$depart_date)
   {
      echo "<tr>";
      echo "<td>$destination</td>";
      echo "<td>$depart_date</td>"
      echo "</tr>";
    }
    echo "</table>";

Upvotes: 1

Rakesh Jakhar
Rakesh Jakhar

Reputation: 6388

You can use array_combine with foreach

$arr = array_combine($destination, $depart_date);
$html =  "<table>
            <tr>
              <th>Destination</th>
              <th>Depart Date</th>
            </tr>";
foreach ($arr as $key => $value) {
    $html .= "<tr>
            <td>{$key}</td>
            <td>{$value}</td>
        </tr>";
}
$html .= "</table>";
echo $html;

Upvotes: 1

Joseph_J
Joseph_J

Reputation: 3669

Your code as you have it displayed appears to be working just fine.

I have tested this and it works.

$_POST = array(

  'destination' => array(

    '0' => 'a',
    '1' => 'b',
    '2' => 'c',
    '3' => 'd'

    ),

  'depart_date' => array(

    '0' => '2019-06-04',
    '1' => '2019-06-06',
    '2' => '2019-06-13',
    '3' => '2019-06-22'

    )

);

$destination  = $_POST['destination'];
if (is_array($destination)) {
    $destination[]  = filter_input(INPUT_POST, 'destination', FILTER_SANITIZE_STRING); //<---This is your problem.  You are adding an extra element to the array.
    //print_r($destination);
}

if (!empty($_POST['depart_date'])) {
    $depart_date = $_POST['depart_date'];
    if (is_array($depart_date)) {
        foreach($depart_date as $dt){
          if (preg_match("/\d{4}\-\d{2}-\d{2}/", $dt)) {
            $depart_date[] = $dt;
          } else{
                array_push($errors, '- Depart date is NOT in required format!');
          }
        }
    }
}

if (is_array($destination)) {
    $dtble = "<table>
                <tr>
                  <th>Destination</th>
                  <th>Depart Date</th>
                </tr>";
    foreach($destination as $k => $v){
      $d = $depart_date[$k];
      $dtble .= "<tr>
                   <td>{$v}</td>
                   <td>{$d}</td>
                 </tr>";
    }
    $dtble .= "</table>";
}

echo $dtble;

UPDATE:

Your problem is here:

In your $destination array that you are trying to sanitize you are adding another element to the array with this line:

$destination[]  = filter_input(INPUT_POST, 'destination', FILTER_SANITIZE_STRING);

I would loop across the array and sanitize each value as you iterate through the array.

If I remove the line of code, your code works just fine.

This returns:

Destination     Depart Date
a               2019-06-04
b               2019-06-06
c               2019-06-13
d               2019-06-22

filter_input is not meant to be used on an array, only a single value. I suggest that you read the docs for filter_input_array and adjust your code to suit.

Upvotes: 2

Related Questions