Giovanni
Giovanni

Reputation: 838

Populate a table with data from a database using a loop

I have quite a few tables on my site, each of which contains two columns: A title and a value from the SQL database. I am currently putting the data in the table using:

<table>
<tr>
<td class = "item">House</td>
<td class = "req"><?php $row_buyerdetails['tod_house']; ?></td>
</tr>
<tr>
<td class = "item">Bungalow</td>
<td class = "req"><?php $row_buyerdetails['tod_bung']; ?></td>
</tr>
</table>

This seems like a long winded way of creating the table (especially as I will have alot more rows than this and quite a few separate tables). Also if I want to make a change to the class name for example, Id have to change every row in every table! There must be a way of putting the 'item' and the 'req' pairs in an array, and then creating a loop which makes the table, populates with the data from the database and assigns the classes? Im new to PHP so cant work out how to do this!


EDIT

The table code above will create the following table:

enter image description here

The tables layout is how I want it. However, i dont want to type out every line of code, I want to put the type of dwelling (with a class of 'item') and the result from the database (with a class of 'req') in an array, and then loop through this array to create the table rather than typing out every <tr> and <td> for every row and every table. I just dont know the technique in PHP to loop through and create the different tags, insert the data into them, and assign the different <td> a class each!

Upvotes: 0

Views: 2354

Answers (3)

Andrew Mackrodt
Andrew Mackrodt

Reputation: 1826

You can create a view helper function that will accept an array and return a table. Take note of the usage of htmlspecialchars.

function create_table(array $rows, $headingClassName = '') {
    $buffer = '<table>';
    foreach ($rows as $row) {
        $buffer .= '<tr><td';
        if ($headingClassName) {
            $buffer .= ' class="' . htmlspecialchars($headingClassName) . '"';
        }
        $buffer .= '>' . htmlspecialchars($row['name']) . '</td><td';
        if ($row['class']) {
            $buffer .= ' class="' . htmlspecialchars($row['class']) . '"';
        }
        $buffer .= '>' . htmlspecialchars($row['value']) . '</td></tr>';
    }
    $buffer .= '</table>';

    return $buffer;
}

Example of how your code may look after receiving information from your database:

// row received from database (column => value)
$row_buyerdetails = array(
    'tod_house' => 1,
    'tod_bung'  => 1,
    'tod_flat'  => 1,
    'tod_conv'  => 1,
    'tod_farm'  => 0,
    'tod_hold'  => 0,
    'tod_plot'  => 1
);

// describe the headings for the rows from the database
$headings = array(
    'tod_house' => 'House',
    'tod_bung'  => 'Bungalow',
    'tod_flat'  => 'Flat',
    'tod_conv'  => 'Barn Conversion',
    'tod_farm'  => 'Farm',
    'tod_hold'  => 'Small Holding',
    'tod_plot'  => 'Building Plot'
);

// convert integers to yes/no and add the req class. you can do this more elegantly
// by modifying the create_table method which would remove the need for this loop.
array_walk($row_buyerdetails, function (&$value, $key) use ($headings) {
    $value = array(
        'class' => 'req',
        'name'  => $headings[$key],
        'value' => $value ? 'yes' : 'no'
    );
});

Lastly you can output the table in your view:

<?php echo create_table($row_buyerdetails, 'item'); ?>

Upvotes: 1

Vivek Vaghela
Vivek Vaghela

Reputation: 1075

Though your question is not quite clear, I am suggesting a solution from what I understood.

You can use foreach loop. First you store the title that you want to show on page("House", "Bungalow",..) in an array associated with the titles from your database table.

So it will be like

<?php $arrTitles = array("tod_house"=>"House", "tod_bung"=>"Bungalow"); ?>

And your table code should be like

<table>
<?php
   foreach($row_buyerdetails as $k=>$v) {
?>
     <tr>
       <td class = "item"><?php echo $arrTitles[$k]; ?></td>
       <td class = "req"><?php echo $v; ?></td>
     </tr>
<?php
   }
?>
</table>

Upvotes: 3

JohnTaa
JohnTaa

Reputation: 2824

<table>
foreach ($results as $row) {
echo '<tr>';

echo '<td class = "item">House</td>';
echo '<td class = "req">'. $row_buyerdetails['tod_house'] .'</td>';
echo '</tr>';
echo '<tr>';
echo '<td class = "item">Bungalow</td>';
echo '<td class = "req">'. $row_buyerdetails['tod_bung'].'</td>';
echo '</tr>';
}
</table>

Upvotes: -1

Related Questions