如何使用数据库和for循环(php)生成数据表

时间:2013-12-02 02:37:10

标签: php html mysql

我正在尝试从我的数据库中获取一些数据,然后迭代结果以生成一个如下所示的表。我仍然是php / html的新手,所以请耐心等待我的编码能力。到目前为止,我有以下代码:

<?php
 if (isset($_POST['vote'])) {
 echo "
    <table id='myTable' class=''>
        <thead>
            <tr>
                <th>i</th>
                <th>Importance</th>
                <th>How Much More</th>
            </tr>
        </thead>
    <tbody>";

 $getObs = $db->prepare("SELECT ComplianceID, ObstacleDescription FROM obstacles WHERE ComplianceID = :option");
 $getObs->bindParam(':option', $option);
 $getObs->execute();
 $result = $getObs->fetchAll();
 for ($i=0; $i < $result; $i++) {
    echo '<tr id="'.$result['ObstacleDescription'][$i].'">';
    echo '<td class = "aObs">'.$result[$i].'</td>
    <td class = "aObs">'."<input type='radio' name='op1' value='1'>"
    .$result['ObstacleDescription'][$i]."<?php echo "or"?>"; "<input type='radio' name='op2' value='2'>"
    .$result['ObstacleDescription'][$i+1].</td>'


    <td class="aObs"><input type='radio' name='op1' value='1'>
<input type='radio' name='op2' value='2'>
<input type='radio' name='op3' value='3'>
<input type='radio' name='op4' value='4'>
<input type='radio' name='op5' value='5'>
<input type='radio' name='op6' value='6'>
<input type='radio' name='op7' value='7'>
<input type='radio' name='op8' value='8'>
<input type='radio' name='op9' value='9'>
 </td>';
 }
 echo"</tbody>";
 }
?>

在这个例子中,我有值:Security,Finaicial,Legal和Technical作为我在db中的ObstacleDescription。每一行与每行的下一行进行比较,最后一行与第一行进行比较。如果有人能从这里指出我正确的方向。提前致谢

CPC Calculator

2 个答案:

答案 0 :(得分:0)

如果您关闭PHP并使用echo

删除所有引号,则可以对此进行大量清理
<?php
   if (isset($_POST['vote'])) {
?> // It's a lot easier to just close PHP instead of dealing with single and double quotes
        <table id='myTable' class=''>
            <thead>
                <tr>
                    <th>i</th>
                    <th>Importance</th>
                    <th>How Much More</th>
                </tr>
            </thead>
        <tbody>
<?php

 $getObs = $db->prepare("SELECT ComplianceID, ObstacleDescription FROM obstacles WHERE ComplianceID = :option");
 $getObs->bindParam(':option', $option);
 $getObs->execute();
 $result = $getObs->fetchAll();
 for ($i=0; $i < $result; $i++): ?> // This saves you a lot of hassle, again, with quotes.
<td class="aObs"><input type='radio' name='op1' value='1'>
  <input type='radio' name='op2' value='2'>
  <input type='radio' name='op3' value='3'>
  <input type='radio' name='op4' value='4'>
  <input type='radio' name='op5' value='5'>
  <input type='radio' name='op6' value='6'>
  <input type='radio' name='op7' value='7'>
  <input type='radio' name='op8' value='8'>
  <input type='radio' name='op9' value='9'>
</td>

当您进入for()循环时,如果您想在每个<?php echo result['whatever']; ?>内放置一些内容,则可以任意使用<td>。我想这就是你最终想要做的事情。

答案 1 :(得分:0)

尝试以下代码:

<?php
 if (isset($_POST['vote'])) {
 echo "
    <table id='myTable' class=''>
        <thead>
            <tr>
                <th>i</th>
                <th>Importance</th>
                <th>How Much More</th>
            </tr>
        </thead>
    <tbody>";

  $getObs = $db->prepare("SELECT ComplianceID, ObstacleDescription FROM obstacles WHERE ComplianceID = :option");
 $getObs->bindParam(':option', $option);
 $getObs->execute();
 $result = $getObs->fetchAll();
 for ($i=0; $i < $result; $i++) {
    echo "<tr id='".$result['ObstacleDescription'][$i]."'>";
    echo "<td class = 'aObs'>".$result[$i]."</td>
    <td class = 'aObs'><input type='radio' name='op1' value='1'>
    ".$result['ObstacleDescription'][$i]." or <input type='radio' name='op2' value='2'>
    ".$result['ObstacleDescription'][$i+1]."</td>


<td class='aObs'>
<input type='radio' name='op1' value='1'>
<input type='radio' name='op2' value='2'>
<input type='radio' name='op3' value='3'>
<input type='radio' name='op4' value='4'>
<input type='radio' name='op5' value='5'>
<input type='radio' name='op6' value='6'>
<input type='radio' name='op7' value='7'>
<input type='radio' name='op8' value='8'>
<input type='radio' name='op9' value='9'>
</td>";
}
echo "</tr></tbody>";
}
?>