【问题标题】:Use PHP to Generate HTML table with static Cell Count of MySQL Data使用 PHP 生成带有 MySQL 数据的静态单元格计数的 HTML 表
【发布时间】:2018-06-15 03:07:34
【问题描述】:

我有一位客户要求我修改他们的一个脚本以显示已删除的文件名表。我不允许将 mysql 修改为 mysqli,因为这不是我的网站。

而不是将它们全部放在一行中并分页,他想要列以便信息可以放在一页上。我尝试了几种方法,但似乎都没有正常工作

方法1:显示正确的列数,但在每个单元格中重复相同的文件名:

$q = "SELECT `name` FROM `files` WHERE `deleted` = 1";
$r = mysql_query($q);
$rows = mysql_num_rows($r); 
$deleted = mysql_fetch_assoc($r);

// Build table and iterate through the results
    $end = $rows; // total # of results
    $t_rows =ceil($end/5); // number of cells per row
    $x = 0;
    $start = 0;

    echo "<table>";
    while($x <= $t_rows){
        echo "<tr>";
        for($y = 0; $y < 5; $y++, $start++){
            if($start <= $end){
                echo "<td>".$deleted['name']."</td>";
            }
        }
        echo "</tr>";
        $x++;
    }
    echo "</table>";

方法 2:显示正确的列数,但在每一行上重复 5 次文件名。 (例如,第 1 行有 5 次第一个记录的名称,第 2 行有第 2 个文件的名称等)。

$q = "SELECT `name` FROM `files` WHERE `deleted` = 1";
$r = mysql_query($q);
$rows = mysql_num_rows($r); 

// Build table and iterate through the results
    $end = $rows; // total # of results
    $t_rows =ceil($end/5); // number of cells per row
    $x = 0;
    $start = 0;

    echo "<table>";
    while($x <= $t_rows){
        echo "<tr>";
        while($deleted = mysql_fetch_assoc($r)){
            for($y = 0; $y < 5; $y++, $start++){
                if($start <= $end){
                    echo "<td>".$deleted['name']."</td>";
                }
            }
            echo "</tr>";
            $x++;
        }
    }
    echo "</table>";

【问题讨论】:

    标签: php mysql html-table


    【解决方案1】:
    $q = "SELECT `name` FROM `files` WHERE `deleted` = 1";
    $r = mysql_query($q);
    
    // Build table and iterate through the results
    $cols = 5; //number of columns
    $x = 0;
    
    echo "<table>";
    while($deleted = mysql_fetch_assoc($r)){
        if($x % $cols == 0) echo '<tr>'; // when $x is 0, 5, 10, etc.
        echo "<td>".$deleted['name']."</td>";
        if($x % $cols == $cols-1) echo "</tr>"; // when x is 4, 9, 14, etc.
        $x++;
    }
    if($x%$cols!=0) echo "</tr>"; // add a closing </tr> tag if the row wasn't already closed
    echo "</table>";
    

    (这是未经测试的,但我认为它会起作用)

    【讨论】:

    • 没有这样的运气。使用它,我没有在屏幕上显示任何内容。不过感谢您的帮助。
    【解决方案2】:

    在过去几天对此进行了修补后,我想出了一个解决方案:

    // Build table and iterate through the results
    $int = 1;
    
    echo "<table>";
    while($deleted = mysql_fetch_assoc($r)){
        if($int%5==1){
            echo "<tr>";
        }
    
        echo "<td>".htmlspecialchars($deleted['name'])."</td>";
    
        if($int%5==0){
            echo "</tr>";
        }
        $int++;
    }
    echo "</table>";
    

    【讨论】:

    • 注意htmlspecialchars()的添加——这是防止XSS攻击的必要条件。
    • 请注意 mysql_* 函数已弃用。改用 MySQLi 或 PDO
    • @gahooa - 感谢您提供此信息,但您是否认为有必要担心此类攻击,因为页面通过会话控制检查锁定并且没有数据通过 $_GET 传递?此外,没有 API 可供其他网站交互或将数据传递给脚本本身。
    • @Timr - 这不是我的网站。正如我在 OP 中提到的,我被要求不要进行任何 rdbms 代码更改。
    • @rsmith84 - 我认为无论输入的安全性如何,都必须为输出格式正确编码数据。您是说数据中永远不会有 & 字符,或者 ? data-with-data 的概念由来已久,并且有无数种格式。 HTML 中的内容要求对 [&,] 进行编码,那么为什么不 100% 地进行编码呢? SQL 有规则,HTML 有规则,HTML 中的 JS 有规则中的规则,等等……例如:&lt;script&gt; if(x&lt;abc) alert(10); &lt;/script&gt; 是 INVALID HTML,因为即使认为 JS 是正确的,它也被嵌入在没有正确编码的 HTML 中。
    猜你喜欢
    • 2017-07-12
    • 1970-01-01
    • 2016-10-26
    • 2017-01-11
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    相关资源
    最近更新 更多