saint saint - 5 months ago 11
HTML Question

Loop through database record to generate table

I want to construct an html table based on the returned results from the database. Assuming I have a table called constraints in my database and a column called riskName and it has these values: Security, Financial, Legal and Technical as shown in the image below. How do i loop through my database and come up with this table. I have tried different approach but no has worked. Here is my code so far:

<?php
error_reporting(0);
$optioner = 12;
$getObs = $db->prepare("SELECT * FROM constraints WHERE constraintsID = ?");
$riski->bindParam(1, $optioner);
$riski->execute();
$result = $riski->fetch(PDO::FETCH_ASSOC);
while($getObs->fetch(PDO::FETCH_ASSOC)){
echo "<tr><td>".($result['riskName'])."<td><tr>";
//...other code
}
?>
</tbody>
<?php };
?>


enter image description here

Answer

I'm not sure if this will give you the exact table you're looking for, but it should at least put you on the right lines. Also, you weren't keeping your variables the same and notice how $result is set in the while loop

$optioner = 12;
$riski = $db->prepare("SELECT * FROM constraints WHERE constraintsID = ?");
$riski->bindParam(1, $optioner);
$riski->execute();
?>

<form>
<table>
<tr> <th>i</th> <th>Importance</th> <th>How Much More?</th> <tr>

<?php
while($result = $riski->fetch(PDO::FETCH_ASSOC)){
    echo '<tr>';
    echo '<td>'. $result['riskName']).'<td>';
    echo '<td>';
    for ($i=1;$i<10;$i++){
      //radio buttons go here 
    } 
    echo'<tr>';
    //...other code
}
?>

Sorry I couldn't help more.

Hope this works for you.

Comments