0

スクリプトの 1 つを変更して、削除されたファイル名のテーブルを表示するように依頼された顧客がいます。これは私のサイトではないため、mysql を mysqli に変更することは許可されていません。

すべてを 1 行に並べてページ付けするのではなく、情報が 1 ページに収まるように列が必要です。いくつかの方法を試しましたが、適切に機能していないようです

方法 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>";
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 に答える