Amy Lane
Amy Lane

Reputation: 3

Table not looping

please find below my table, $page['body'] retrieves a column from my database, this column is a text box, the text box has multiple lines with multiple comma's on each line, I've been trying to split each line from the column into it's own row in the HTML table and each word(s) before the comma's into each own column in the html table, I hope that doesn't sound confusing sorry.

This is what the text box column in my database record looks like,

  more data, even more data
  some more data, and even more data
  this is data, and so is this

and this is what I'm trying to show in the HTML table

  | # | First          | Second             |
  | 1 | more data      | even more data     |
  | 2 | some more data | and even more data |
  | 3 | this is data   | and so is this     |

the body column from my database looks like this below

more data, even more data
some more data, and even more data,
this is data, and so is this

The number's in the left column should auto echo each line by itself, please find below my code, I'm new to coding since October so I hope I haven't made too many mistakes, thank you very much in advance to anyone who replies, it's most appreciated.

<table>
<thead>
<tr>
<th>#</th>
<th>First</th>
<th>Second</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$pizza  = escape($page['body']);
$pieces = explode(",", $pizza);

foreach ($pieces as $pizza) {
echo "";
echo "<td>$pieces[1]</td>";
echo "<td>$pieces[2]</td>";
}
?>
</tr>
</tbody>
</table>

Upvotes: 0

Views: 84

Answers (3)

akrys
akrys

Reputation: 563

You have to explode the lines first to get the text for each row.

Then you can create a loop over all rows and extract the columns there.

It would work like this:

<?php
//Text to test the output
$text=<<<TEXT
more data, even more data
some more data, and even more data
this is data, and so is this
TEXT;

//just to be sure, the line ending characters are right for
//Windows ("\r\n"), Linux ("\n") and classic Macintosh ("\r")
$text = str_replace("\r", "\n", str_replace("\r\n", "\n", $text));

//exploding the lines
$lines = explode("\n", $text);

foreach($lines as $key => $line) {
    //exploding the columns separated by comma (,)
    $pieces = explode(',', $line);

    //printing the line, the line endings (PHP_EOL) are optional
    //and only used to get a more beautiful result
    if(count($pieces) > 0) {
        echo '<tr>'.PHP_EOL;

        //printing the count column
        echo '<td>'.($key + 1).'</td>'.PHP_EOL;

        //looping the columns
        foreach($pieces as $piece){
            echo '<td>'.$piece.'</td>'.PHP_EOL;
        }
        echo '</tr>'.PHP_EOL.PHP_EOL;
    }
}

Upvotes: 1

maha lakshmi
maha lakshmi

Reputation: 77

Use this. print_r($pieces);

It print format of array.then use

    foreach ($pieces as $pizza) {
    echo "";
    echo "<td>$pizza</td>";
   }

If it's associated array ,use

foreach($pieces as $key => $value )
{
echo "";
echo "<td>$key</td>";
echo "<td>$value</td>";
}

Upvotes: 0

Md. Abutaleb
Md. Abutaleb

Reputation: 1645

Try this:

foreach ($pieces as $key => $pizza) { ?>
    <tr>
        <td>
            <?php echo My key is : $key & My value is : $pizza ?>
        </td>
    </tr>
<?php } ?>

Upvotes: 0

Related Questions