skip to Main Content
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 ——————————————————————————————————————————————–

OUTPUT
enter image description here

Table for payment in phpmyadmin

2

Answers


  1. In PHP:

    $row['treatment'] = explode(",", $row['treatment']);
    $row['totalprice'] = explode(",", $row['totalprice']);
    

    In HTML:

    <tbody>
        <tr>
    
    <?php 
    foreach($row['treatment'] as $key => $value){
        echo '<td>'.$value.'</td>';
        echo '<td>'.$row['totalprice'][$key].'</td>';
    }
    ?>
        </tr>
    </tbody>
    
    Login or Signup to reply.
  2. 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.

    The Database Code
    <?php
        ini_set('error_reporting', E_ALL);
        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "halcondentalclinic";
        $mysqli = new mysqli($servername, $username, $password, $dbname);
        if($mysqli->connect_error) {
        exit('Error connecting to database'); //Should be a message a typical user could understand in production
        }
    
        $mysqli->set_charset("utf8mb4");
        $stmt = $mysqli->prepare("SELECT * FROM `payment` WHERE patient_id = ?");
        $stmt->bind_param("si", $id);
        $stmt->execute();
        $result = $stmt->get_result();
    ?>
    

    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.

    <table class="table table-bordered" width="50%" cellspacing="0">
        <thead>
            <th>Teeth</th>
            <th>Price</th>
        </thead>
        <tbody>
            <tr>
    <?php
    while($row = $result->fetch_assoc()) {
         echo '<tr>';
            echo '<td>'.$row['treatment'].'</td>';
            echo '<td>'.$row['totalprice'].'</td>';
            }
            echo '</tr>';
        }
    ?>
        </tbody>
    </table>
    

    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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search