正确编写php代码和html-PHP

时间:2018-07-15 19:34:10

标签: php html

我不知道如何在技术上称呼这种情况,我将发布两个功能齐全的代码,我的目标是能够以更轻松的方式编写我的代码。

在我将在下面发布的代码中,我正在初始化一个while循环,但是这种形式的编写使我有可能不会在php echo必不可少的单引号之间失去无聊的时间,因为我可以在其中插入免费的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';
}

?>

1 个答案:

答案 0 :(得分:2)

您可以使用以下代码:

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
 }