简体   繁体   中英

Use PHP to Generate HTML table with static Cell Count of MySQL Data

I have a customer who has asked me to modify one of their scripts to display a table of file names that have been deleted. I am not allowed to modify mysql to mysqli as this is not my site.

Rather than putting them all in a line and paginating, he wants columns so that the information can fit on one page. I have tried several methods but none seem to work properly

Method 1: Displays the correct number of columns, but repeats the same file name in every cell:

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

Method 2: Displays the correct number of columns, but on each row it repeats a file name 5x. (Example, Row 1 has the name of the first record 5 times, Row 2, the name of the 2nd file, etc).

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

(This is untested, but I think it'll work)

After tinkering with this the last few days I've come up with a solution:

// 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>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM