Jump to content

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

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.