Jump to content

[SOLVED] PHP 2D array into an HTML table


random1

Recommended Posts

My web application currently has a working function that creates a correctly formatted HTML table from a supplied 2D array (results from database) and caption.

 

How can I modify they below code for this logic:

 

* If data field is a link (http:, https:// or ftp://) then create an HTML <a> around field

* If data field is a URL to an image then create an HTML <img> around field

* If data field is a comma sperated list then create a list box and submit button

* Add form buttons to last column with 'edit', 'delete' and a change status select list

 

Function Code:

 

public static function DisplayAsGrid($records, $caption) {

 

$r = '';

 

//if there is 1 or more

if(count($records) > 0) {

 

//start table

$r .= '<table summary="Example Summary">'. "\r\n";

if($caption!=""){$r .= '<caption><em>'.$caption.'</em></caption>' . "\r\n";};

//Table Head

$r .= '<thead>' . "\r\n";

$r .= '<tr>' . "\r\n";

foreach($records[0] as $key=>$value) {

$r .= '<th scope="col" abbr="'.$key.'">' . $key . '</th>' . "\r\n";

}

$r .= '</tr>'. "\r\n";

$r .= '</thead>'. "\r\n";

$r .= '<tbody>'. "\r\n";

//Table Footer : ToDo

 

foreach($records as $record) {

 

// Table Fields

$r .= '<tr>' . "\r\n";

foreach($record as $field) {

$r .= '<td>' . $field . '</td>' . "\r\n";

}

$r .= '</tr> ' . "\r\n";

}

 

$r .= '</tbody>'. "\r\n";

 

//end table

$r .= '</table>' . "\r\n";

 

}

 

return $r;

 

}

Link to comment
https://forums.phpfreaks.com/topic/82629-solved-php-2d-array-into-an-html-table/
Share on other sites

You'll need to use strpos and substr to get the various parts of the string you're looking for. For the CSV question you could use explode.

The form buttons sounds simple enough. Do you really not know how to write the HTML or am I misunderstanding?

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.