首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >正确编写php代码和html - PHP

正确编写php代码和html - PHP
EN

Stack Overflow用户
提问于 2018-07-16 03:34:10
回答 1查看 62关注 0票数 0

我不知道如何在技术队伍中调用这种情况,我将发布两个功能齐全的代码,我的目标是能够在我的代码中写得更容易。

在我将在下面发布的代码中,我初始化了一个while循环,但这种编写形式使我有可能不会在单引号之间浪费对php回显至关重要的无聊时间,因为我可以插入免费的html代码,以便更容易地构建前端部分。

代码语言:javascript
复制
<?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部分

代码语言:javascript
复制
<?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';
}

?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-16 03:40:16

您可以使用以下代码:

代码语言:javascript
复制
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
 }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51351579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档