需要帮助简化我的 php 表

发布于 2024-09-01 21:18:27 字数 1535 浏览 7 评论 0原文

我对 php 比较陌生,感觉在显示来自 mysql 的数据时我走了很长的路。

我有一个表,我想显示数据库中的一些字段。

我如何实现这一点而不必回显表格的每一点???

这是代码:

     <?php
$query1 = mysql_send("SELECT firstname, lastname, email, user, country FROM customers WHERE id='".$_COOKIE['custid']."'");

while ($row = mysql_fetch_array($query1))

{

      echo     ' <table id="account_table" style="width:550px; border:none; ">
              <tr>
                <td width="155">Contact Name</td>';
      echo          '<td width="335">';

    echo $row['firstname'] ;
    echo '&nbsp;';
    echo $row['lastname'];

    echo '</td>
              </tr>
              <tr>
                <td>Email Address</td>
          <td>';
   echo $row['email'];

  echo '  </td>
             </tr>
              <tr>
                <td>Username</td>
                <td>' ;

    echo $row['user'];

    echo '</td>
              </tr>
              <tr>
                <td>Country</td>
                <td>';

    echo $row['country'];

    echo '</td>
              </tr>
              <tr>
                <td>Time Zone</td>
                <td>GMT+1</td>
              </tr>
              <tr>
                <td>Activated</td>
                <td>16 Dec 2009</td>
              </tr>
            </table>';

            }
?>

I am relatively new to php and have a feeling that I am going the long way round when displaying data from mysql.

I have a table a I want to show a few fields from my database.

How would I achieve this without having to echo every bit of the table???

Here is the code:

     <?php
$query1 = mysql_send("SELECT firstname, lastname, email, user, country FROM customers WHERE id='".$_COOKIE['custid']."'");

while ($row = mysql_fetch_array($query1))

{

      echo     ' <table id="account_table" style="width:550px; border:none; ">
              <tr>
                <td width="155">Contact Name</td>';
      echo          '<td width="335">';

    echo $row['firstname'] ;
    echo ' ';
    echo $row['lastname'];

    echo '</td>
              </tr>
              <tr>
                <td>Email Address</td>
          <td>';
   echo $row['email'];

  echo '  </td>
             </tr>
              <tr>
                <td>Username</td>
                <td>' ;

    echo $row['user'];

    echo '</td>
              </tr>
              <tr>
                <td>Country</td>
                <td>';

    echo $row['country'];

    echo '</td>
              </tr>
              <tr>
                <td>Time Zone</td>
                <td>GMT+1</td>
              </tr>
              <tr>
                <td>Activated</td>
                <td>16 Dec 2009</td>
              </tr>
            </table>';

            }
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

转身泪倾城 2024-09-08 21:18:27

我建议查看一些模板引擎,例如 Smarty,它允许您将演示文稿与 php 代码分开。

I would suggest to look at some templating engine like Smarty, that would allow you to separate presentation from php code.

遗弃M 2024-09-08 21:18:27

您可以先将所有数据提取到数组中,然后迭代该数组。无需使用 PHP 来回显所有 HTML。

文件顶部,您应该执行所有处理(即获取、验证数据),并在其余部分中编写 >纯 HTML,仅使用 PHP 打印值。

这已经给你们带来了一定程度的分离。其他人提到模板引擎(Smarty 等)。我不认为你真的需要它,因为 PHP 本身就是一个模板引擎。
只是不要试图在演示文稿中做复杂的事情;)

还有 控制结构的替代语法对于与演示文稿结合使用非常有用,因为它更具可读性。


我稍微更改了表结构,因为您没有生成有效的 HTML(您在原始代码中创建了许多具有相同 ID 的表)。
这只会生成一个表,每个客户对应一行。

<?php
$customers = array();
$query1 = mysql_send("SELECT firstname, lastname, email, user, country FROM customers WHERE id='".$_COOKIE['custid']."'");

while ($row = mysql_fetch_array($query1)) {
    $cusomters[] = $row;
}
?>

<table id="account_table" style="width:550px; border:none;">
    <tr>
       <th width="155">Contact Name</th>
       <th>Email Address</th>
       <th>Username</th>
       <th>Country</th>
       <th>Time Zone</th>
       <th>Activated</th>
    </tr>
<?php foreach($customers as $customer): ?>
    <tr>           
        <td width="335">
             <?php echo $row['firstname'] ?>
              
             <?php echo $row['lastname'] ?>
        </td>          
        <td><?php echo $row['email'] ?> </td>     
        <td><?php echo $row['user'] ?></td>           
        <td><?php echo $row['country'] ?></td>
        <td>GMT+1</td>
        <td>16 Dec 2009</td>
    </tr>
<?php endforeach; ?>
</table>

You can fetch all data into an array first and then iterate over that array. There is no need to echo all that HTML with PHP.

At the top of your file, you should do all the processing (i.e. getting, validating data) and in the remainder you just write plain HTML, only printing the values with PHP.

This already gives you a certain degree of separation. Others mention template engines (Smarty, etc.). I don't think that you really need that, because PHP itself is a template engine.
Just don't get tempted to do sophisticated stuff in your presentation ;)

Also the alternative syntax for control structures is very useful for using in combination with the presentation as it is imho much more readable.


I changed the table structure a bit, because you were not generation valid HTML (you create a lot tables with the same ID in your original code).
This just generates one table, with a row for each customer.

<?php
$customers = array();
$query1 = mysql_send("SELECT firstname, lastname, email, user, country FROM customers WHERE id='".$_COOKIE['custid']."'");

while ($row = mysql_fetch_array($query1)) {
    $cusomters[] = $row;
}
?>

<table id="account_table" style="width:550px; border:none;">
    <tr>
       <th width="155">Contact Name</th>
       <th>Email Address</th>
       <th>Username</th>
       <th>Country</th>
       <th>Time Zone</th>
       <th>Activated</th>
    </tr>
<?php foreach($customers as $customer): ?>
    <tr>           
        <td width="335">
             <?php echo $row['firstname'] ?>
              
             <?php echo $row['lastname'] ?>
        </td>          
        <td><?php echo $row['email'] ?> </td>     
        <td><?php echo $row['user'] ?></td>           
        <td><?php echo $row['country'] ?></td>
        <td>GMT+1</td>
        <td>16 Dec 2009</td>
    </tr>
<?php endforeach; ?>
</table>
疧_╮線 2024-09-08 21:18:27

基本知识似乎是正确的,尽管您需要将 标记移出 while 循环,但现在您正在为每个条目创建一个表,我的猜测是这不是您想要的想。

您还需要使用 htmlspecialchars($row[...]) 之类的内容准备输出以输出到浏览器。这样,如果输出包含 html 标签(javascript 等),您就可以避免潜在的问题。

The basics seem right, although you will need to move the <table> tag out of the while loop, now you are creating a table for every entry and my guess is that that´s not what you want.

You also need to prepare your output for output to the browser with something like htmlspecialchars($row[...]). That way you avoid potential problems if the output contains html tags (javascript, etc.).

弥繁 2024-09-08 21:18:27

首先一些建议,保持 php 手册的窗口/选项卡打开。 (即,mysql_send() 函数不存在)。

缩进代码,并尝试找到一致的约定来保持代码的可读性。

您将表标记插入到 while 循环中,这是错误的。你的桌子应该包裹住你的循环。

按原样使用 $_COOKIE 从数据库中进行选择确实很糟糕。在任何查询中使用您的输入之前,请对其进行过滤和清理。

为了避免混合数据表示和业务逻辑,您可以查看模板引擎。

查看 19 个有前景的 PHP 模板引擎 来查找一个,或者采用查看维基百科列表

First some advices, keep a window/tab open to the php manual. (ie, mysql_send() function doesn't exist).

Indent your code, and try to find a consistent convention to keep your code readable.

You're inserting your table markup into your while loop which is wrong. Your table should wrap your loop.

Using $_COOKIE as is to select from your database is really bad. Filter and sanitize your input before to use it in any query.

To avoid mixing data presentation and business logic, you can take a look to template engine.

Look at 19 Promising PHP Template Engines to find one, or take a look at the wikipedia list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文