Jump to content

Array into a table?


naykidd

Recommended Posts

So if i have an array that varies in how many sets of results there are, how would i go about putting it into a table of multiple rows, with column headings?

 

Below is in my current code, and this just gives the results in a list , trying to put it into table of 4 columns, with as many rows of data as required, depending on the size of the array:

 

<?php

else {  
$results = array(); // the result array  
$i = 1; 

while ($row = mysql_fetch_assoc($searchResult)) {  
$results[] = "{$i}: {$row['Column1']}<br />{$row['Column2']}<br />{$row['Column3']}<br />{$row['Column4']}<br />{$row['Column5']}<br /><br />";  
$i++;  
}  
}  


function removeEmpty($var) {  
return (!empty($var));   
}  
?>  

 

Tried to keep it short and to the point, thanks!

Link to comment
https://forums.phpfreaks.com/topic/165034-array-into-a-table/
Share on other sites

<table>
<tr>
    <th>Column Heading 1</th>
    <th>Column Heading 2</th>
    <th>Column Heading 3</th>
    <th>Column Heading 4</th>
</tr>
<?php

if (mysql_num_rows($searchResult)) {
    while ($row = mysql_fetch_assoc($searchResult)) {
        print '<tr>';
        print "\t<td>" . $row['column1'] . '</td>';
        print "\t<td>" . $row['column2'] . '</td>';
        print "\t<td>" . $row['column3'] . '</td>';
        print "\t<td>" . $row['column4'] . '</td>';
        print '</tr>';
    }
} else {
    print '<tr><td colspan="4">Search returned no results</td></tr>';
}

?>
</table>

Link to comment
https://forums.phpfreaks.com/topic/165034-array-into-a-table/#findComment-870353
Share on other sites

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.