0

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!

2
  • show example what exactly want ! . difficult to read your explanation . Commented Feb 15, 2014 at 12:57
  • im not getting what actual you need, but u can use loop above <table> so that it will print/display till your query will be execute... Commented Feb 15, 2014 at 12:58

3 Answers 3

3

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this - its close to working! The new issue is the loop looks through the $row_buyerdetails array (which contains ~50 items). Where they match up to a value in the $arrTitles array it works fine, but where there is no entry in the $arrTitles it still creates a table row but puts "undefined index" in there. How do I make sure that it only loops through the values in the $arrTitles array rather than the values in the $row_buyerdetails array?
Sorted, just had to change the array that looped from the big $row_buyerdetails to the small $arrTitles as this was the one that determined the size of the table (obviously changing the echo arrays accordingly too). Thankyou for your help!
1

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'); ?>

1 Comment

I wish I could accept both answers. This is a far more useful answer as it allows me to pass the array to the function for any table I want. However the accepted answer does do what I asked in the question so better stick with my first choice! Great code though, thankyou! :)
-1
<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>

1 Comment

@mathielo take it easy man, if he doesn't get it , I will explain it in more details, thanks for downvoting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.