php - 将mysql数据插入div

标签 php jquery html mysql css

正在尝试完成教程并尝试改进样式。 我知道整张代码可能是当前的任何东西, 但它帮助我分解部分并理解整体。

就是说,我正在尝试将 mysql 数据插入到 div 而不是表格中。 (考虑到更多响应式 CSS 设计的可能性)

任何帮助都会很棒。

<?php 
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php 
// Run a select query to get my letest 4 items
// Connect to the MySQL database  
include "storescripts/connect_to_mysql.php"; 
$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 4");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
    while($row = mysql_fetch_array($sql)){ 
             $id = $row["id"];
             $product_name = $row["product_name"];
             $price = $row["price"];
             $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
             $dynamicList .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">
        <tr>
          <td width="17%" valign="top"><a href="product.php?id=' . $id . '"><img style="border:#666 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="77" height="102" border="1" /></a></td>
          <td width="83%" valign="top">' . $product_name . '<br />
            $' . $price . '<br />
            <a href="product.php?id=' . $id . '">View Product Details</a></td>
        </tr>
      </table>';
    }
} else {
    $dynamicList = "We have no products listed in our store yet";
}
mysql_close();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The Shop</title>
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen" />
</head>
<body>
<div align="center" id="mainWrapper">
  <?php include_once("template_header.php");?>
  <div id="pageContent">
  <table width="85%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="14%" valign="top"><h3></h3>
      <p><br />
        <br />
        </p></td>
    <td width="66%" align="center">
        <h3>The Shop </h3>
      <p><?php echo $dynamicList; ?><br />
        </p>
      <p><br />
      </p></td>
    <td width="20%" valign="top"><h3></h3>
        <p></p></td>
  </tr>
</table>

  </div>
  <?php include_once("template_footer.php");?>
</div>
</body>
</html>

最佳答案

我假设这会让您朝着正确的方向前进(相应地忽略 ...)

<?php
  ...
  $dynamicList .= '
  <div>
    <a href="product.php?id=' . $id . '"><img style="border:#666 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="77" height="102" border="1" /></a>
  </div>
  <div>' . $product_name . '<br />
    $' . $price . '<br />
    <a href="product.php?id=' . $id . '">View Product Details</a>
  </div>';
  ...
?>

...

<div id="pageContent">
  <h3>The Shop </h3>
  <?php echo $dynamicList; ?>
</div>

关于php - 将mysql数据插入div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24871290/

相关文章:

php - Jquery Bootstrap 模式框不适用于 WordPress 中的检查验证

jquery - Div 的 float 不准确。如何正确安排它们?

javascript - 如何在元素中添加标记?

javascript - 自定义 Google 图表列过滤器

javascript - value() 和 focus() 不起作用

php - 连接 mysql 表

javascript - 设置间隔内的设置间隔

javascript - javascript `this.closest` 在 IE11 和 Chrome 中的使用

javascript - 使超链接仅在禁用 JavaScript 时有效?

php - 使用 MySQLi 连接到多个数据库