0

我有一位客户要求我修改他们的一个脚本以显示已删除的文件名表。我不允许将 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:显示正确的列数,但在每一行上重复一个文件名 5x。(例如,第 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>";
4

2 回答 2

0
$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>";

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

于 2012-12-27T16:43:19.900 回答
0

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

// 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>";
于 2012-12-31T22:07:28.587 回答