【问题标题】:php script to display data from SQL database用于显示来自 SQL 数据库的数据的 php 脚本
【发布时间】:2016-10-21 13:48:18
【问题描述】:

我在 phpmyadmin 上有一个数据库,我想将一些数据(不是所有数据)从一个表显示到 HTML/PHP 上的一个表中。我无法弄清楚我的代码有什么问题。每个表格行必须有 3 个单元格。这就是我在 php 脚本中使用 $nrArt 变量的原因。你能帮忙吗?

<table class="art-content">
    <div class="row">
    <? php
       try {$cnx= new PDO("mysql:host=localhost;charset=utf8;dbname=restaurant","root", "");
     $interogare=$cnx -> prepare("SELECT id_preparat, nume, pret, foto from preparate WHERE id_categorie= 1");
     $interogare -> execute();
     $nrArt = 0;
     foreach ($interogare-> fetchAll() as $linie){
        $id=$linie["id_preparat"];
        $nume=$linie["nume"];
        $pret=$linie["pret"];
        $img=$linie["foto"];
        if($nrArt==0){
            echo '<tr class="col-1-3">';
             }
        echo '<div class="wrap-col">';
        echo '<td class="item-container">';
        echo'<a class="example-image-link" href="images/'.$img.'" data-lightbox="example-set" data-title="'.$nume.'">';
        echo '<div class="item-caption">';
            echo '<div class="item-caption-inner">';
                echo'<div class="item-caption-inner1">';
                        echo'<h3>Pret</h3>';
                        echo '<span>'.$pret.'RON</span>';
                        echo'</div>';
                echo '</div>';
        echo '</div>';
    echo '<img class="example-image" src="images/'.$img.'" alt=""/>';
    echo'</a>';                     
    echo '</td>';
    $nrArt++;
    if($nrArt>2){
        echo '</tr>';
        }
    }
}
    catch (PDOException $e){
    die("Conectare imposibila: ". $e -> getMessage()); }
     ?>
    </div>
    </table>

【问题讨论】:

    标签: php html phpmyadmin xampp


    【解决方案1】:

    你忘了重置你的变量所以

    不是:

    if($nrArt>2){
            echo '</tr>';
            }
        }
    

    但是:

    if($nrArt>2){
            echo '</tr>';
    $nrArt = 0;
            }
        }
    

    【讨论】:

    • 谢谢。我重置了它,但脚本仍然不起作用。 :(
    • 您的另一个问题是您将 DIV 与 TABLE 和 TD 混合在一起。在 TABLE 中,您不能这样放置 DIV。在 HTML 文档中查看看起来正确的 TABLE TR TD 语法
    • 你需要选择是使用TABLE还是DIV来创建你的布局
    猜你喜欢
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    • 2020-09-05
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多