通过PHP将数据插入可重复的div中

时间:2015-03-09 11:00:02

标签: php html

JSFiddle here,显然无法使用PHP,但在本地主机上它可以很好地提取信息。

我正在尝试将数据循环附加到特定类标记内,在某个类的di中。

例如,对于表格中的每一行,添加一个带有“card”类的div,并将“Title”附加到H2中,将“description”附加到P标签等中。

任何人都知道如何做到这一点?我正在使用谷歌搜索,但很难说出我的问题。

Div“卡片”加价:

<div class="card">
  <h2>Health and Wellbeing</h2>
  <p>Sometimes you just did too much sitting not enough jumping go go go.</p>
</div> 

PHP拉:

$sql = "SELECT title, description, count FROM relevant_topics";
    $result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<b>Title: </b>" . $row["title"]. " - Description: " . $row["description"]. " " . $row["count"]. "<br>"; 
}
} else {
echo "0 results";
}

2 个答案:

答案 0 :(得分:2)

您可以将div标记回显到PHP代码中,如下所示:

while($row = $result->fetch_assoc()) {
echo'<div class="card">
         <h2>'.$row["title"].'</h2>
         <p>'.$row["description"].'
     </div>';
}

不确定你想要Row ['count']的位置。

你也可能想使用PDO而不是mysql,因为mysql已经过时了:How can I prevent SQL injection in PHP?

答案 1 :(得分:1)

检查此朋友

if ($result->num_rows > 0) {

// output data of each row
while($row = $result-> mysql_fetch_array()) {//if not works change mysql_fetch_array() to fetch_assoc()
    echo'<div class="card"><h2>';
    echo '.$row["title"].';
    echo '</h2><p>';
    echo '.$row["description"].';
    echo '</P></div>'; 

}

相关问题