The Database Code
<?php
ini_set('error_reporting', E_ALL);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "halcondentalclinic";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM `payment` WHERE patient_id='$id'";
$conn->multi_query($sql);
$result = $conn->use_result();
echo $conn->error;
$row = $result->fetch_assoc();
$treatments = $row['treatment'];
$treatmentss = $row['totalprice'];
$treatments_chunks = explode(",", $treatments);
$treatments_chunkss = explode(",", $treatmentss);
?>
For PHP code and declaration of variables ——————————qqqqqqqqqqqqqqqqqqqqqqqqqqqqq
<table class="table table-bordered" width="50%" cellspacing="0">
<thead><th> Teeth</th>
<th> Price</th>
</thead>
<tbody>
<tr>
<?php
foreach($treatments_chunks as $row ){
echo '<tr>';
$row = explode(',',$row);
foreach($row as $cell ){
echo '<td>'.$cell.'</td>';
}
echo '</tr>';
}
?>
<?php
foreach($treatments_chunkss as $row ){
echo '<tr>';
$row = explode(',',$row);
foreach($row as $cell ){
echo '<td>'.$cell.'</td>';
}
echo '</tr>';
}
?>
</tbody>
For table code ——————————————————————————————————————————————–
2
Answers
In PHP:
In HTML:
I added some SQL injection protection to your select query because the $id parameter is never checked for any input validation, you might want to check if its actually a number. Always assume the user wants to harm the program, because sooner or later somebody might.
The reason why the rows are only below eachother is because the TR causes a next row. The TD causes a next column. and you tell it to always make a new column in a loop function.
-This code below is unchecked by the compiler.
Debug the variables if its not working with var_debug($row); and temporarily put exit; below the debug line so you quickly see a single variable in php.
Please read the below article about MySQLi prepared statements and or google more information about on how to use them, they are more secure as your current way of retrieving data.
Source:
https://websitebeaver.com/prepared-statements-in-php-mysqli-to-prevent-sql-injection