I have data like this

something something_description, something2 something2_description, something3 something3_description...

And now I need with php to get table as:

<tr><td>something</td><td>something_description</td></tr>
<tr><td>something2</td><td>something2_decription</td></tr>

I don't know how many "something" and "something_decriptions" will it be so I need to set some loop.

for now I have this code:

$data = explode(',',$query);

from that I will get array like:

[0] => something someting_description

Now how can I put this into table?

On net I found some examples for sorting array to table, but this is with one more "explode" inside "explode"

I could use some help.

Tnx,

share|improve this question

explode, for loop, explode, print. – Karoly Horvath Mar 14 at 22:22
1  
It seems you already have the answer: Loop through the array and explode the elements on ' '. – jeroen Mar 14 at 22:22
feedback

2 Answers

up vote 0 down vote accepted

Probably you are looking for this:

    $data = explode(',',$query);

    echo '<table>';
    foreach($data as $row){
        echo '<tr>';
        $row = explode(' ',$row);
        foreach($row as $cell){
            echo '<td>';
            echo $cell;
            echo '</td>';
        }
        echo '</tr>';
    }
    echo '</table>';
share|improve this answer
feedback

Try this:

echo "<table><tr>".implode("</tr><tr>",array_map(function($a) {return "<td>".implode("</td><td>",explode(" ",trim($a)))."</td>";},explode(",",$query)))."</tr></table>";

One-liner ftw :p

share|improve this answer
Wohoou :) Thank you Kolink very much! – Ivan C. Mar 14 at 22:27
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.