1

I have a PHP shell_exec command that outputs the following:

Cats
3
Dogs
9
Fish
2

Every second line is a number, that corresponds to the name of the animal on previous line, i would like the output in a HTML table for example:

HTML TABLE
------------
| Cats | 3 |
| Dogs | 9 |
| Fish | 2 |
------------

I think i need to create an array, but i am not sure how to align the animal name and number onto the same line. At the moment i have this:

<?php
    $array1 = array();
    exec( "Command", $Output );

?>
<html>


<table>
  <tr>
    <th>Animal</th>
    <th>Number</th>
  </tr>
  <tr>
    <td>*</td>
    <td>*</td>
  </tr>
  <tr>
    <td>*</td>
    <td>*</td>
  </tr>
  <tr>
    <td>*</td>
    <td>*</td>
  </tr>

  </tr>
</table>

</body>
</html>

How can i put this into a HTML Table?

2 Answers 2

1

You can convert it to array with array-chunk after using explode to convert to array.

$str = 'Cats
3
Dogs
9
Fish
2';

$arr = explode(PHP_EOL, $str); //break each line
$arr = array_chunk($arr,2); // group each pair
foreach($arr as $e)
    $res[$e[0]] = $e[1]; // group each pair as key and value
print_r($res);

This will output:

Array
(
    [Cats] => 3
    [Dogs] => 9
    [Fish] => 2
)

You can now use this more easily to display by looping with foreach($res as $animal => $number)

Your HTML should be:

<table>
  <tr>
    <th>Animal</th>
    <th>Number</th>
  </tr>
<?php foreach($res as $animal => $number)
    echo '<tr><td>'. $animal . '</td><td>' . $number . '</td></tr>'; ?>
</table>
Sign up to request clarification or add additional context in comments.

12 Comments

Thanks, How would i enter this into the HTML table?
@jonnyb You can add PHP for-loop in the HTML - edited my answer
Thanks, i can see this works however i get an error when using it with the output from shell_exec: PHP Notice: Undefined offset: 1 in /var/www/html/user1/examples/test1.php on line 6 PHP Stack trace: PHP 1. {main}() /var/www/html/user1/examples/test1.php:0 Array ( [000000000] => ) <html> <table> <tr> <th>Animal</th> <th>Number</th> </tr> <tr><td>000000000</td><td></td></tr></table> </html>
Can you please verify $res contain my output before running the HTML code? (in the PHP section) - Define the array hard-coded as $res = array("Cats" => "3", "Dogs" => "9", "Fish" => "2"); and see if it works
I fixed it by changing exec to shell_exec .. Thankyou!
|
0

You could use the preg_split() function to split the string along linebreaks into an array, and loop through the array two at a time, shifting the first two off at every loop.

<table>
<?php
$Output = <<< EOF
Cats
3
Dogs
9
Fish
2
EOF;

$animals = preg_split('/[\n\r]+/', trim($Output));

while (!empty($animals)) {
    echo "<tr>\n";
    echo "<td>{$animals[0]}</td>\n";
    echo "<td>{$animals[1]}</td>\n";
    echo "</tr>\n";
    array_shift($animals);
    array_shift($animals);
}
?>
</table>

2 Comments

I am receiving the following error: trim() expects parameter 1 to be string, array given in /test.php on line 4
I see. I expected $Output to be a string, but I suppose it is an array, when you use the exec() function. If you'd use the function shell_exec(), as you said under dWinder's answer, it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.