基于PHP循环创建一个新的HTML页面

时间:2018-02-13 15:03:29

标签: php html sql

正如您所见,$x在SQL查询中传递。我想多次增加$x++,每次为每个不同的$result创建一个新的 .html 页面。这怎么可能?

    $x = 1;
    $query = "SELECT * FROM `persons` WHERE CODE_NO = '$x'";
    $result = mysqli_query($con, $query);

    }

$table = "<table>"; // table tag in HTML


while($row = mysqli_fetch_array($result))
{
//Creates a loop to loop through results
$table .= "<tr>";
$table .= "<th>CODE_NO:</th>";
$table .= "<td>" . $row['CODE_NO'] . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th>FIRST_NAME:</th>";
$table .= "<td>" . $row['FIRST_NAME'] . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th>SURNAME:</th>";
$table .= "<td>" . $row['SURNAME'] . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th>Date of birth:</th>";
$table .= "<td>" . $row['DOB'] . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th>Date of death:</th>";
$table .= "<td> " . $row['DOD'] . "</td>";
$table .= "</tr>";
}
$table .= "</table>"; //Close the table in HTML

echo $table;

?>

1 个答案:

答案 0 :(得分:0)

我更新查询以在一个查询中提取所有代码(按CODE_NO排序)。

$query = "SELECT * FROM `persons` WHERE CODE_NO > 0 ORDER BY CODE_NO;";

您应该将CODE_NO编入索引进行优化。

    <?php

$rows = [
    ['CODE_NO'=>1,'FIRST_NAME'=>1,'SUR_NAME'=>1,'DOB'=>1,'DOD'=>1],
    ['CODE_NO'=>1,'FIRST_NAME'=>2,'SUR_NAME'=>2,'DOB'=>2,'DOD'=>2],
    ['CODE_NO'=>3,'FIRST_NAME'=>1,'SUR_NAME'=>1,'DOB'=>1,'DOD'=>3],
    ['CODE_NO'=>4,'FIRST_NAME'=>1,'SUR_NAME'=>1,'DOB'=>1,'DOD'=>4],
    ['CODE_NO'=>5,'FIRST_NAME'=>1,'SUR_NAME'=>1,'DOB'=>1,'DOD'=>5],
];
    $index =1;
    $codes=[];
    foreach($rows as $row){
        $codes[$row['CODE_NO']][] =$row;
    }
    foreach($codes as $tableArray){
        $table = "<table>".PHP_EOL;
        $is_titles = true;
        foreach($tableArray as $rowArray){

            if($is_titles){
                $is_titles = false;
                $table .= "<tr>".PHP_EOL;
                foreach($rowArray as $key => $value){
                    $table .= "<th>".$key."</th>".PHP_EOL;
                }    
                $table .= "</tr>".PHP_EOL;
            }
            $table .= "<tr>".PHP_EOL;
                foreach($rowArray as $key => $value){
                    $table .= "<td>" . $value . "</td>".PHP_EOL;
                }    
                $table .= "</tr>".PHP_EOL;


        }


        $table .= "</table>".PHP_EOL;
        echo $table;
        }



?>

在我的代码中,我在不同的表中打印了每个CODE_NO值,但是如果需要,您可以创建一个新文件并放置表格内容。

$rows是我的模拟,您应该用真实数据替换它。

输出:

<table>
<tr>
<th>CODE_NO</th>
<th>FIRST_NAME</th>
<th>SUR_NAME</th>
<th>DOB</th>
<th>DOD</th>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
</tr>
</table>
<table>
<tr>
<th>CODE_NO</th>
<th>FIRST_NAME</th>
<th>SUR_NAME</th>
<th>DOB</th>
<th>DOD</th>
</tr>
<tr>
<td>3</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>3</td>
</tr>
</table>
<table>
<tr>
<th>CODE_NO</th>
<th>FIRST_NAME</th>
<th>SUR_NAME</th>
<th>DOB</th>
<th>DOD</th>
</tr>
<tr>
<td>4</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>4</td>
</tr>
</table>
<table>
<tr>
<th>CODE_NO</th>
<th>FIRST_NAME</th>
<th>SUR_NAME</th>
<th>DOB</th>
<th>DOD</th>
</tr>
<tr>
<td>5</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>5</td>
</tr>
</table>