简体   繁体   中英

how to display data in a table using javascript and php

What is the actual problem in this code?

<script type="text/javascript">
   $(document).ready(function () {
       var str="";

       <?php
       require 'dbcon.php';
       $query = "SELECT PatientID, HospitalRegistrationNo, FirstName, RegistrationDate,Gender FROM patientdemography";
       $result = mysqli_query($conn,$query);
       while($row = mysqli_fetch_array($result))
       {?>
       str = "<tr><td><?php $row["PatientID"]?></td><td><?php $row["HospitalRegistrationNo"]?></td><td><?php $row["FirstName"]?><td><?php $row["RegistrationDate"]?> <td><?php $row["Gender"]?></td></tr>";
            //echo "".$row["PatientID"]." ".$row["HospitalRegistrationNo"]." ".$row["FirstName"]." ".$row["RegistrationDate"]." 
            //".$row["Gender"]."<br>";
            $('#tbpatientlist').append(str);
     } 

       });
</script>

For php, when printing a value to html please use either echo $blah or print($blah) , ALWAYS finish code statements with semi-colons ; and your while loop doesnt look closed.

For example: <?php $row["PatientID"]?> becomes <?php echo $row["PatientID"]; ?> <?php echo $row["PatientID"]; ?> , and if opening a <?php while(){ ?> make sure the closing } is also inside <?php ?> tags too.

So, without seeing any more of your code, this looks like the cause. If not, please be clearer in what the expected output vs actual output is thanks. And also, you do need to include what youve already tried.

$(document).ready(function () { 
       var str="";

       <?php
       require 'dbcon.php';
       $query = "SELECT PatientID, HospitalRegistrationNo, FirstName, RegistrationDate,Gender FROM patientdemography";
       $result = mysqli_query($conn,$query);
       while($row = mysqli_fetch_array($result))
       {?>
       str = "<tr><td><?php echo $row["PatientID"]; ?></td><td><?php echo $row["HospitalRegistrationNo"]; ?></td><td><?php echo $row["FirstName"]; ?><td><?php echo $row["RegistrationDate"]; ?> <td><?php echo $row["Gender"]; ?></td></tr>";
            //echo "".$row["PatientID"]." ".$row["HospitalRegistrationNo"]." ".$row["FirstName"]." ".$row["RegistrationDate"]." 
            //".$row["Gender"]."<br>";
            $('#tbpatientlist').append(str);
       <?php } ?>


});

Please do see https://stackoverflow.com/help/how-to-ask , else you may find your questions downvoted or removed, and there really are many resources (questions already answered, tutorials, ect) already out there that will teach you better the basics, so that you can be even quicker in solving problems in future.

In your code many issues,

1) You did't echo PHP variable. 2) <td> not properly closed. ...

<?php
   require 'dbcon.php';
   $query = "SELECT PatientID, HospitalRegistrationNo, FirstName, RegistrationDate,Gender FROM patientdemography";
   $result = mysqli_query($conn, $query);
   if (!empty($result)) {
?>
<script type="text/javascript">
    $(document).ready(function () {
        var str = "";
        <?php
            while($row = mysqli_fetch_array($result))
            {
        ?>
            str += " <tr>
                        <td>
                            <?php echo $row["PatientID"]; ?>
                        </td>
                        <td>
                            <?php echo $row["HospitalRegistrationNo"]; ?>
                        </td>
                        <td>
                            <?php echo $row["FirstName"]; ?>
                        </td>
                        <td>
                            <?php echo $row["RegistrationDate"]; ?>
                        </td>
                        <td>
                            <?php $row["Gender"]?>
                        </td>
                    </tr>";
        <?php
            }
        ?>
        $('#tbpatientlist').append(str);
    });
</script>
<?php } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM