【问题标题】:Create multiple html tables from database table using any PHP loop使用任何 PHP 循环从数据库表创建多个 html 表
【发布时间】:2015-03-16 23:30:55
【问题描述】:

我正在尝试创建一段 PHP 代码,它将为我的数据库表中的每个数组创建一个 HTML 表。这样做的重点是,如果在数据库中添加一个新数组,则会创建一个新的 HTML 表。 这是我现在的代码:

<?php
?>

<!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>JOY FLOWERS</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
    <div id="outter">
        <div id="top"></div>
        <div id="menu">
            <ul>
                <li><a href="index.html">Home</a></li>
                <li><a href="services.php">Proionta</a></li>
                <li><a href="contactus.html">Epikoinonia</a></li>
            </ul>
        </div>
        <div id="body">
            <div id="leftcol"></div>
            <div id="midcol">

            <?php
            $con = mysql_connect ( "localhost", "root", "tei@123" ) or die ( "Αποτυχία σύνδεσης με τον Server<br />" );

            mysql_select_db ( "proionta", $con ) or die ( "Αποτυχία σύνδεσης με την database<br />" );
            $query = "SELECT * FROM proionta";

            mysql_query ( "SET NAMES 'utf8';", $con );
            mysql_query ( "SET CHARACTERS SET 'utf8';", $con );

            $result = mysql_query ( $query, $con );
            $row = mysql_fetch_array ( $result );
            foreach ($row as $result);{

            ?>  

                    <table>
                        <tr>
                            <th><img src="<?php echo $row["eikona"]?>" width="100%"</th>
                        </tr>
                        <td><?php echo $row["proion"]; ?></td>
                        </tr>
                        <tr>
                            <td><?php echo $row["timh"];?> ευρώ /τεμάχιο</td>
                        </tr>

                    </table>
                     <?php  }?>

            </div>
            <div id="rightcol"></div>
        </div>
    </div>
</body>
</html>

我尝试使用诸如 foreach 和 while 之类的循环,但失败了。考虑到这是我的第一个 PHP 项目,我对过去几天收到的所有信息感到有些迷茫,所以我真的可以寻求帮助在这个..

这是我使用的 foreach 循环。我觉得 $row 和 $result 应该是相反的,但这是我不会出错的唯一方法。虽然它只会为第一个数组创建一个表。

提前致谢,如有英文错误请见谅

【问题讨论】:

  • 请显示您的 foreach 或 while 循环不起作用,然后我们可以为您指明正确的方向。不要只留下空隙。

标签: php html mysql html-table


【解决方案1】:

试试这样的:

    while($row = mysql_fetch_array ( $result )) {
        ?>  
                <table>
                    <tr>
                        <th><img src="<?php echo $row["eikona"]?>" width="100%"</th>
                    </tr>
                    <td><?php echo $row["proion"]; ?></td>
                    </tr>
                    <tr>
                        <td><?php echo $row["timh"];?> ευρώ /τεμάχιο</td>
                    </tr>

                </table>
    <?php } ?>php

【讨论】:

猜你喜欢
  • 2022-12-04
  • 1970-01-01
  • 2016-07-26
  • 2015-11-17
  • 2015-10-16
  • 1970-01-01
  • 2012-01-11
  • 2013-07-16
  • 1970-01-01
相关资源
最近更新 更多