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
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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.