Jump to content

Converting data into HTML table


kraybrett

Recommended Posts

I am new to coding and working on a website, I have data coming from my data base in this format to my product description area:

 

Length=8 in|Width=0.180 in|Thickness=|Size Group=|Bundle Diam=1.9000 in [Max]|Material=Nylon|Color=Weather Resistant

 

I need to somehow convert the data to this format in an HTML table in the same area:

 

Length 8 in

Width 0.180 in

Thickness

Size Group

Bundle Diam 1.9000 in [Max]

|Material=Nylon

Color=Weather Resistant

 

I know it seems friggin easy, but I am stumped!

 

Any help will be appreciated!

Link to comment
https://forums.phpfreaks.com/topic/286890-converting-data-into-html-table/
Share on other sites

explode on the |  (pipe). Use foreach to loop over the returned array. In the loop start a new table row (<tr>) and a new table cell (<td>). For the current item use str_replace to replace the = with  </td><td>  after close the table cell (</td>)  and  the table row (</tr>). Code:

$data = 'Length=8 in|Width=0.180 in|Thickness=|Size Group=|Bundle Diam=1.9000 in [Max]|Material=Nylon|Color=Weather Resistant';
echo '<table>';
foreach(explode('|', $data) as $item)
{
   echo '<tr><th>'.str_replace('=', '</th><td>', $item) . '</td></tr>';
}
echo '</table>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.