display plain text php mysql - php

after having found the data in a table the function displays the information without missing, with a td tag only the first word which displays the others after the space are not displayed.
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Service </th>
<th>Prix</th>
<th >Quantite</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$reqq = "SELECT * FROM service WHERE client = '$clientId' AND dossier = '$dossier' ";
$results = $connect->query($reqq);
?>
<?php if($results->num_rows > 0){ ?>
<?php
foreach($results as $data){ ?>
<?php echo $data['nom']; ?>
<tr>
<td> <?php print $data['nom']; ?>
<td><?= $data['prix']?></td>
<td><?= $data['quantite']?></td>
<td>Supprimer</td>
</tr>
</tbody>
</table>
the result is
Inside the table the last field have Franchise Procedure the table only display the first word which is Franchise
Help me find the solution please.

Try with:
<td> <?php echo htmlspecialchars($data['nom']); ?> </td>
Often, text stored in databases can contains reserved characters in HTML like "<" or "&" which should be replaced by html entities
That's what the PHP function htmlspecialchars was invented for.

Related

Php variable in a html table

I'm trying to display an html table in which I'd like to show some php variables.
My code is the following:
<?php
$title = the_title();
?>
<table>
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $title?></td>
</tr>
</tbody>
</table>
In the output I can see the word "Name" as expected but it doesn't print the $title variable (there is a empty space instead).
I've already read some of the questions on the site but they doesn't help me because they suggest to write the same code that I wrote.

how to display comma separated values from multiple columns in php inside html table

i have an sql table with three columns which have comma separated values, i am trying to print it inside an html table, my code looks like below:
<table class="table custom-table m-0">
<thead>
<tr>
<th>Description</th>
<th>Make</th>
<th>UOM</th>
</tr>
</thead>
<tbody>
<?php
$one=explode(',', $row['description']);
$two=explode(',', $row['make']);
$three=explode(',', $row['uom']);
foreach($one as $ones) {
?>
<tr>
<td>
<?php echo $ones?>
</td>
<td></td>
<td></td>
</tr>
<?php }?>
</tbody>
</table>
here am only able to get the values of first column, can anyone please tell me how to get values from all the three columns, thanks in advance
Use a counter - assuming exact same number of entries per row
http://sandbox.onlinephpfunctions.com/code/555be47daf3bc3e99d496585f702bfc9dfae4e4e
<?
$one=explode(',', $row['description']);
$two=explode(',', $row['make']);
$three=explode(',', $row['uom']);
$i=0;
?>
<table class="table custom-table m-0">
<thead>
<tr>
<th>Description</th>
<th>Make</th>
<th>UOM</th>
</tr>
</thead>
<tbody>
<?php
foreach($one as $ones) {
?>
<tr>
<td><?php echo $ones; ?></td>
<td><?php echo $two[$i]?></td>
<td><?php echo $three[$i]?></td>
</tr>
<?php $i++;}?>
</tbody>
</table>

inside the echo, why the php variable can't be printed with html tag

I have a problem printing the dynamic value in the table.
The first photo is the result I want:
The second photo is the result I got:
Here is my code:
<?php
if(in some conditions, the table will appears with dynamic vairalbe) {
//some logic to get the single value, here let's assume the result is 85.00
$single = 85.00;
//here is the table with value
echo '
<table class="table table-striped">
<thead>
<tr>
<th scope="row">Status</th>
<th scope="row">Tax</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Single</th>
<td> {$single} <td>
</tr>
<tr>
</tbody>
</table>
';
}
?>
The problem here are the single-quotes. They display almost everything "as-is". (Reference difference between single and double qoutes)
If you want the variable to be parsed, you have to use double-quotes instead. (")
Important: Don't forget to escape all the other double-quotes you want to be echoed literally.
Example:
<?php
echo "
<table class=\"table table-striped\">
<thead>
<tr>
<th scope=\"row\">Status</th>
<th scope=\"row\">Tax</th>
</tr>
</thead>
<tbody>
<tr>
<th scope=\"row\">Single</th>
<td> {$single} </td>
</tr>
</tbody>
</table>
";
?>

How to make this table with expandable rows to display other rows when expanded?

I have a datatable in HTML which i am populating with dynamic data from PHP. each row is expandable but what I want is to have child rows for each parent row. So, for example there is a device type with the number 4 which contains 20 different devices, so I want to display in the parent row Device number 4 and when I expand that row display the 20 devices (with headers such as IMEI, Serial no., etc.)
Here is the table I have right now:
See screenshot of my table
EDIT: Added current table (no child rows)
<table id="example" class="table table-bordered table-striped" cellspacing="0" width="100%">
<thead>
<th>Device</th>
<th>Dev.no</th>
<th>Barcode</th>
<th>IMEI</th>
<th>Serial no.</th>
<th>Date added on stock</th>
</thead>
<tbody>
<?php if (!empty($devices)) { ?>
<?php foreach ($devices as $device) : ?>
<tr>
<td>
<?php echo $device["name"] ?>
</td>
<td>
<?php echo $device["device_no"] ?>
</td>
<td>
<?php echo $device["barcode"] ?>
</td>
<td>
<?php echo $device["serial_imei"] ?>
</td>
<td>
<?php echo $device["serial_no"] ?>
</td>
<td>
<?php echo $device["created_date"] ?>
</td>
</tr>
<?php endforeach; ?>
<?php } ?>
</br>
</tbody>
</table>
I think you're looking for something like this https://datatables.net/reference/api/row().child().
Then you can add a table in the child row with more information.
Note: It looks like you have the responsive option enabled (green plus button that hides/shows hidden columns). If you have a child row open and try to show the hidden columns, it might overwrite your child row.
Edit:
You need to specify the HTML you want to put in the child. I would personally use ajax to retrieve the children when I wanted to display them. Otherwise the data needs to be hidden somewhere in the page.
I've created a codepen that puts the HTML in a data attribute on the row. Then you can click a button to view the child rows (devices). Obviously, there are many other ways to store the data on the page.
$(document).on("click", ".viewChildren", rowClickHandler);
$("#example").DataTable();
function rowClickHandler() {
var btn = $(this);
var tr = btn.closest('tr');
var dtRow = $("#example").DataTable().row(tr);
if (dtRow.child.isShown()) {
dtRow.child.hide();
btn.text('Show Children');
} else {
var children = tr.data("children");
dtRow.child(children).show();
btn.text('Hide Children');
}
}
th{
text-align: left;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.20/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="//cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css" />
<table id="example" class="table table-bordered table-striped" cellspacing="0" width="100%">
<thead>
<tr>
<th>Device</th>
<th>Dev.no</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr data-children='<table>
<thead>
<tr>
<th>barcode</th>
<th>imei</th>
<th>serial_no</th>
<th>created_date</th>
</tr>
</thead>
<tbody>
<tr>
<td>123456</td>
<td>11324</td>
<td>654654</td>
<td>2020-01-17</td>
</tr>
<tr>
<td>1234987</td>
<td>1654</td>
<td>654687</td>
<td>2020-04-30</td>
</tr>
</tbody>
</table>'>
<td>Laptop</td>
<td>2</td>
<td><button class = 'viewChildren'>See Children</button></td>
</tr>
</tbody>
</table>

Outputting Data into an HTML table using php/html

I am trying to output data onto my webpage from a database. I am able to so successfully, but in a very untidy way. I have tried to put the data in a table on the website with no success.
Below, data is retrieved from the db, but just echoed out crudely. It looks untidy but it outputs successfully onto the webpage
<?php
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo "Name: " . $row["name"] . " - Email: " . $row["email"] . " - Address: " . $row["address"] . "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
I try to put the data into an HTML table(on the same page, by combining PHP and HTML with no success. How can put this data into an organised table successfully?
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php $row["name"] ?></td>
<td><?php $row["email"] ?></td>
<td><?php $row["address"]?></td>
</tr>
</tbody>
</table>
</div>
Below is roughly how I would like the data to be structured, how can achieve this?
Name | Email | Address
Jo |J#o.com|12 Street
Ben |B#e.com|23 street
try this:
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php $row["name"] ?></td>
<td><?php $row["email"] ?></td>
<td><?php $row["address"]?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
Try below code :
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) { ?>
<td><?php $row["name"]?></td>
<td><?php $row["email"]?></td>
<td><?php $row["address"]?></td>
<?php }
} ?>
</tr>
</tbody>
</table>
</div>
<?php
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
?>
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead> <tbody>
<?php
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php $row["name"] ?></td>
<td><?php $row["email"] ?></td>
<td><?php $row["address"]?></td>
</tr>
<?php }
} else {
echo "<tr><td colspan=3>0 results</td></tr>";
}
mysqli_close($conn);
?>
</tbody>
</table>
</div>
You can always print the results from the select like this:
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>".$row["name"]."</td>
<td>".$row["email"]."</td>
<td>".$row["address"] . "</td></tr>";
}
It's not a very clean way, but for each row should print another row, below the three table headers.
There isn't really a very neat way to do this beyond the other answers.
But what you can do is keep the HTML and PHP separate as much as possible in the same file.
The PHP at the top of the file can be as follows:
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
$htmlToDisplay = ''; //this variable will contain table rows
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
//create the HTML row - we'll use this later
$htmlToDisplay .= "<tr><td>".$row['name']."</td><td>". $row['email']."</td><td>".$row['address']."</td></tr>";
}
} else {
$htmlToDisplay = "<tr><td>0 results</td><tr>";
}
Then you can have your HTML after your closing PHP tag (?>) as follows:
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<!-- echo your php row(s) here in a slightly neater way since it's one line -->
<?php echo $htmlToDisplay; ?>
</tbody>
</table>
This will make the the different parts of the file (the PHP and HTML) more readable.
You could also look at using a PHP template engine like smarty.

Categories