php - 正确编写php代码和html - PHP
问题描述
我不知道如何称呼这种情况在技术行列中,我将发布两个功能齐全的代码,我的目标是能够以对我来说更“简单”的方式编写我的代码。
在我将在下面发布的代码中,我正在初始化一个 while 循环,但是这种写作形式使我有可能不会在 php 回显所必需的单引号之间失去无聊的时间,因为我可以插入免费的 html 代码以构建更容易的前端部分
<?php
session_start();
include 'FILE_DI_CONNESSIONE';
$id = $_SESSION['id'];
$query_string = "QUERY";
$query = mysqli_query($VARIABILE_FILE_DI_CONNESSIONE, $query_string);
?>
<?php
while($row = mysqli_fetch_assoc($query)){ ?>
HTML <?php echo $row['NOME_CAMPO_TABELLA'] ;?> HTML
<?php } ?>
在这里我们来到了问题的核心,我有另一段代码,但我试图打破我的头,但没有任何结果我希望有可能编写上面的代码以便编写更快的 html 部分
<?php
//fetch.php
include '../../../connessione.php';
$output = '';
if(isset($_POST["query"]))
{
$cerca = mysqli_real_escape_string($connessione, $_POST["query"]);
$query = "
SELECT * FROM table
WHERE nome_cliente LIKE '%".$cerca."%'
OR cognome_cliente LIKE '%".$cerca."%'
OR azienda_cliente LIKE '%".$cerca."%'
OR telefono_cliente LIKE '%".$cerca."%'
OR email_cliente LIKE '%".$cerca."%'
";
}
else
{
$query = "SELECT * FROM table ORDER BY id_cliente";
}
$result = mysqli_query($connessione, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '
<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>Customer Name</th>
<th>cognome_cliente</th>
<th>azienda_cliente</th>
<th>Postal Code</th>
<th>email_cliente</th>
</tr>
';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["nome_cliente"].'</td>
<td>'.$row["cognome_cliente"].'</td>
<td>'.$row["azienda_cliente"].'</td>
<td>'.$row["telefono_cliente"].'</td>
<td>'.$row["email_cliente"].'</td>
</tr>
';
}
echo $output;
}
else
{
echo 'Nessun risultato corrisponde alla tua ricerca';
}
?>
解决方案
您可以使用以下代码:
if(mysqli_num_rows($result) > 0)
{
?>
<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>Customer Name</th>
<th>cognome_cliente</th>
<th>azienda_cliente</th>
<th>Postal Code</th>
<th>email_cliente</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row["nome_cliente"]; ?></td>
<td><?php echo $row["cognome_cliente"]; ?></td>
<td><?php echo $row["azienda_cliente"]; ?></td>
<td><?php echo $row["nome_cliente"]; ?></td>
<td><?php echo $row["email_cliente"]; ?></td>
</tr>
<?php
}
推荐阅读
- maven - 如何在 pom.xml 中检索 Maven 内部版本号?
- angular - 如何在Angular 4中使用打字稿基于其他下拉选择值显示下拉值
- c++ - 在 Windows 中运行 .exe 文件时如何防止 ucrtbase.dll 崩溃?
- c - 从文件的 inode 号获取文件名
- dashboard - 如何将现有仪表板复制到 Appdynamics 中的新项目
- python - 在哪里可以找到内置类和 ABC 的 Python 类型提示?
- javascript - Web 组件中页面加载时的模式弹出窗口
- python-3.x - 语音识别库给出输入设备错误
- mysql - MySQL 显示“possible_keys”但不使用它
- php - Lumen count():参数必须是数组或者实现了Countable的对象