Reputation:
I do not know how to call this situation in technical ranks, I will post two fully functional codes, my goal is to be able to write my code in a more "easy" for me.
In the code that I will post below I'm initializing a while loop but this form of writing gives me the possibility of not losing boring time between single quotes essential for php echos because I have the possibility to insert free html code in order to build more easily the frontend part
<?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 } ?>
and here we come to the heart of the matter, I have this other piece of code but I've tried to smash my head a bit but without any result I would like to have the possibility to write the code as above in order to write faster the html part
<?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';
}
?>
Upvotes: 0
Views: 83
Reputation: 510
You can use code below:
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
}
Upvotes: 2