将Mysql回显到表中-演示文稿和DOM

时间:2018-08-11 18:07:28

标签: php html mysql

两个问题合二为一: 第一个问题,主要是关于展示的: 我正在回显以下应创建表的代码。表格应该只有一列,但是它是用图像上方的元素作为一行呈现的。谁能看到原因?

<?php   
$sql = "SELECT * FROM catdata WHERE featured='yes' LIMIT 2";
if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
        echo "<table>";
        while($row = mysqli_fetch_array($result)){
            echo "<tr>";
                echo "<td>" . $row['manufacturer'] . "</td>";
            echo "</tr";

            echo "<tr>";
                echo "<td>" . $row['title'] . "</td>";
            echo "</tr";

            echo "<tr>";

                echo "<td><img src=\"6.diesel.png\"></td>";
            echo "</tr>";

            echo "<tr>";    
                echo "<td>" . $row['size'] . "l</td>";
            echo "</tr>";
            echo "<tr>";
                echo "<td>" . $row['mileage'] . "</td>";
            echo "</tr>";
            echo "<tr>";
                echo "<td>" . $row['fitsmodel'] . "</td>";
            echo "</tr>";
            echo "<tr class=\"tablePriceBlock\">";
                echo "<td>£" . $row['pricefitted'] . "</td>";
            echo "</tr>";
            echo "<tr>";
                echo "<td>£" . $row['pricedel'] . "</td>";

            echo "</tr>";
        }
        echo "</table>";
        // Free result set
        mysqli_free_result($result);
    } else {
            echo "No records matching your query were found.";
            }
} else{
            echo "ERROR: Unable to execute $sql. " . mysqli_error($link);
    }
?>

问题2:

我可以在第二列或单独的表格中显示第二个结果吗?另外,是否可以访问$result之类的$[manufacturer][1]元素?

2 个答案:

答案 0 :(得分:0)

始终通过“查看源代码”或使用诸如curl之类的工具查看代码生成的源代码。您会发现此错误:

echo "</tr";

您缺少>

许多编写HTML的人都有浏览器插件,可以链接到HTML验证程序以确保他们具有正确的语法。您可能需要查找并安装其中之一。

答案 1 :(得分:0)

要生成第二个结果是一个单独的表,请遵循以下代码。

   echo "<table>";
                            $count = 1;
                           while($row = mysqli_fetch_array($result)){
  if($count == 2){
  echo "<table>";
  echo "<tr>";
  echo "<td>put your code here</td>";
  echo "</tr>"
  echo </table>  

   }else{
                         echo "<tr>";
                         echo "<td>" . $row['manufacturer'] . "</td>";
                            echo "</tr";

                            echo "<tr>";
                                echo "<td>" . $row['title'] . "</td>";
                            echo "</tr";

                            echo "<tr>";

                                echo "<td><img src=\"6.diesel.png\"></td>";
                            echo "</tr>";

                            echo "<tr>";    
                                echo "<td>" . $row['size'] . "l</td>";
                            echo "</tr>";
                            echo "<tr>";
                                echo "<td>" . $row['mileage'] . "</td>";
                            echo "</tr>";
                            echo "<tr>";
                                echo "<td>" . $row['fitsmodel'] . "</td>";
                            echo "</tr>";
                            echo "<tr class=\"tablePriceBlock\">";
                                echo "<td>£" . $row['pricefitted'] . "</td>";
                            echo "</tr>";
                            echo "<tr>";
                                echo "<td>£" . $row['pricedel'] . "</td>";

                            echo "</tr>";
 }
$count ++;
                        }
                        echo "</table>";

由于您将查询限制为仅两个记录,因此此方法对此并不坏,希望对您有所帮助。