php - To make data into table format before exporting to Excel -
i use function below process array data retrieve db, export data excel.
# feed final items our formatting function... $contents = get_excel_data($items); function get_excel_data($items){ # set variable $output = null; # check if data items , not empty if (is_array($items) && !empty($items)) { # start row @ 0 $row = 0; # loop items # "foreach(array_values($data) $item)" complicated. "foreach($data $item)" suffice. # foreach(array_values($items) $item) foreach($items $item) { if (is_array($item) && !empty($item)) { if ($row == 0) { # write column headers $output = implode("\t",array_keys($item)); $output .= "\n"; } # create line of values row... $output .= implode("\t",array_values($item)); $output .= "\n"; # increment row don't create headers on again $row++; } } } # return result return $output; }
it processes output below,
cat_id cat_name cat_order cat_hide cat_important cat_created cat_updated 1 primary image 1 0 1 0000-00-00 00:00:00 2011-01-17 17:26:51 2 secondary image 2 0 1 0000-00-00 00:00:00 2011-01-17 17:27:01 3 tertiary image 3 0 1 0000-00-00 00:00:00 2010-10-08 20:03:56 4 quartary image 4 0 1 0000-00-00 00:00:00 2010-10-08 20:03:56
but ideally want wrap output in table this,
<table border="1"> <tr> <th>cat_id</th> <th>cat_name</th> <th>cat_order</th> <th>cat_hide</th> <th>cat_important</th> <th>cat_created</th> <th>cat_updated</th> </tr> <tr> <td>1</td> <td>primary image</td> <td>1</td> <td>0</td> <td>1</td> <td>0000-00-00 00:00:00</td> <td>2011-01-17 17:26:51</td> </tr> <td>2</td> <td>secondary image</td> <td>2</td> <td>0</td> <td>1</td> <td>0000-00-00 00:00:00</td> <td>2011-01-17 17:26:51</td> </tr> ... </table>
any ideas should add original code output table above?
i tried version of modification below fails!
function get_excel_data($items){ # set variable $output = null; # check if data items , not empty if (is_array($items) && !empty($items)) { # start row @ 0 $row = 0; # loop items # "foreach(array_values($data) $item)" complicated. "foreach($data $item)" suffice. # foreach(array_values($items) $item) foreach($items $key => $value ) { if ($row == 0) { /* # write column headers $output = implode("\t",array_keys($item)); $output .= "\n"; */ $output = '<table border="1"><tr>'; $output .= '<th>'.$key.'</th>'; $output .= '</tr>'; } /* # create line of values row... $output .= implode("\t",array_values($item)); $output .= "\n"; */ $output .= '<tr>'; $output .= '<td>'.$value.'</td>'; $output .= '</tr>'; if ($row == 0) { $output .= '</table>'; } # increment row don't create headers on again $row++; } } # return result return $output; }
thanks.
here untested variant on function work.
# feed final items our formatting function... $contents = get_excel_data($items); function get_excel_data($items){ # set variable $output = null; # check if data items , not empty if (is_array($items) && !empty($items)) { # start row @ 0 $row = 0; $output = '<table border="1">' # loop items # "foreach(array_values($data) $item)" complicated. "foreach($data $item)" suffice. # foreach(array_values($items) $item) foreach($items $item) { if (is_array($item) && !empty($item)) { if ($row == 0) { $output .= '<tr>'; foreach(array_keys($item) $header) { $output .= '<th>'.$header.'</th>'; } $output .= '</tr>'; } $output .= '<tr>'; foreach(array_values($item) $cell) { $output .= '<td>'.$cell.'</td>'; } $output .= '</tr>'; # increment row create headers once $row++; } } $output .= '</table>'; } # return result return $output; }
Comments
Post a Comment