Column name field showing multiple time in php? - php

I am searching records and records are displaying from database, but column name is repeating.Please check below images.
<table border="1" align="center">
<thead>
<tr>
<th>User id</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM newrecords_1 WHERE CONCAT( First_name, ' ',Last_name ) LIKE '%$name%' ORDER BY `First_name` ASC";
$result = $conn->query($query);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {?>
<tr>
<td><?php echo $row['ID'];?></td>
<td><?php echo $row['First_name'];?></td>
<td><?php echo $row['Last_name'];?></td>
</tr>
</tbody>
</table>
<?php
}
} else {
echo "0 results";
}
What i am getting
What i need
after added html code on header getting output

Put your table code outside of the loop like below :-
<table border="1" align="center">
<thead>
<tr>
<th>User id</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM newrecords_1 WHERE CONCAT( First_name, ' ',Last_name ) LIKE '%$name%' ORDER BY `First_name` ASC";
$result = $conn->query($query);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {?>
<tr>
<td><?php echo $row['ID'];?></td>
<td><?php echo $row['First_name'];?></td>
<td><?php echo $row['Last_name'];?></td>
</tr>
<?php } } else { echo "0 results";}?>
</tbody>
</table>

You are getting header part after every fetching because you are using header part also in loop. Place header part above loop and keep only
below code in loop.
<tr>
<td><?php echo $row['ID'];?></td>
<td><?php echo $row['First_name'];?></td>
<td><?php echo $row['Last_name'];?></td>
</tr>

Related

Datatables console error jQuery.Deferred exception: Cannot set property '_DT_CellIndex'

my datatables didn't work and i get console error Uncaught TypeError: Cannot set property '_DT_CellIndex' of undefined but when i delete the query inside tbody its working fine.
<table class="table table-bordered table-striped table-fixed text-center" id="myTable">
<thead>
<tr>
<th>No</th>
<th>ID</th>
<th>Nama</th>
<th>Golongan</th>
<th>Nilai Output</th>
<th>Penilaian Atasan</th>
<th>Nilai Learning</th>
<th>Nilai Kedisiplinan</th>
<th>Nilai 5R</th>
<th>Hasil</th>
<th>Tanggal</th>
</tr>
</thead>
<!-- query -->
<?php
$no = 0;
$query = "SELECT * FROM user
where id= $_SESSION[id]";
$query = "SELECT user.id,user.nama,golongan,nilai_output,nilai_atasan,nilai_learning,nilai_kedisiplinan,nilai_5r,overall,tanggal
FROM tkaryawan
JOIN user
ON user.id=tkaryawan.id
where user.id= $_SESSION[id]
ORDER BY tanggal DESC";
$result = mysqli_query($koneksi, $query);
if(!$result){
die ("Query Error: ".mysqli_errno($koneksi).
" - ".mysqli_error($koneksi));
}
while($data = mysqli_fetch_assoc($result))
{
$no++;
?>
<tbody>
<tr>
<td><?php echo $no;?></td>
<td><?php echo $data['id'];?></td>
<td class="text-capitalize"><?php echo $data['nama'];?></td>
<td><?php echo $data['golongan'];?></td>
<td><?php echo $data['nilai_output'];?></td>
<td><?php echo $data['nilai_atasan'];?></td>
<td><?php echo $data['nilai_learning'];?></td>
<td><?php echo $data['nilai_kedisiplinan'];?></td>
<td><?php echo $data['nilai_5r'];?></td>
<td class="font-weight-bold text-danger"><?php echo $data['overall'];?></td>
<td class="text-secondary"><?php echo $data['tanggal'];?></td>
<tr>
</tbody>
<?php
}
// free the memory
mysqli_free_result($result);
// close conection
mysqli_close($koneksi);
?>
</table>
appreciate any help you can provide.
EDITED
<?php
$stmt = $mysqli->prepare("SELECT id,nama FROM user WHERE user.id= $_SESSION[id]");
$stmt->execute();
$res = $stmt->get_result();
$row = $res->fetch_assoc();
if(!$row){
mysqli_query($link, $sql);
}
else
{
echo "<h3 class='text-uppercase'>HASIL KINERJA </br> <p class='font-weight-bold text-danger'> $row[nama] </p> </h3>";
}
?>
You put your while loop above tbody which will loop the tbody as well. You need to move the while and its closing tag on <tr>. Also try to change the closing tr tag on tbody to be </tr>.
<table class="table table-bordered table-striped table-fixed text-center" id="myTable">
<thead>
<tr>
<th>No</th>
<th>ID</th>
<th>Nama</th>
<th>Golongan</th>
<th>Nilai Output</th>
<th>Penilaian Atasan</th>
<th>Nilai Learning</th>
<th>Nilai Kedisiplinan</th>
<th>Nilai 5R</th>
<th>Hasil</th>
<th>Tanggal</th>
</tr>
</thead>
<!-- query -->
<?php
$no = 0;
$query = "SELECT * FROM user
where id= $_SESSION[id]";
$query = "SELECT user.id,user.nama,golongan,nilai_output,nilai_atasan,nilai_learning,nilai_kedisiplinan,nilai_5r,overall,tanggal
FROM tkaryawan
JOIN user
ON user.id=tkaryawan.id
where user.id= $_SESSION[id]
ORDER BY tanggal DESC";
$result = mysqli_query($koneksi, $query);
if(!$result){
die ("Query Error: ".mysqli_errno($koneksi).
" - ".mysqli_error($koneksi));
}
?>
<tbody>
<?php
while($data = mysqli_fetch_assoc($result))
{
$no++;
?>
<tr>
<td><?php echo $no;?></td>
<td><?php echo $data['id'];?></td>
<td class="text-capitalize"><?php echo $data['nama'];?></td>
<td><?php echo $data['golongan'];?></td>
<td><?php echo $data['nilai_output'];?></td>
<td><?php echo $data['nilai_atasan'];?></td>
<td><?php echo $data['nilai_learning'];?></td>
<td><?php echo $data['nilai_kedisiplinan'];?></td>
<td><?php echo $data['nilai_5r'];?></td>
<td class="font-weight-bold text-danger"><?php echo $data['overall'];?></td>
<td class="text-secondary"><?php echo $data['tanggal'];?></td>
</tr>
<?php } ?>
</tbody>
<?php
// free the memory
mysqli_free_result($result);
// close conection
mysqli_close($koneksi);
?>
</table>

How to fix a table causing "502: Bad Gateway" error in PHP

I am creating a simple database that can do basic CRUD operations (create, read, update, delete) using php. I am able to complete the create, and able to see the results if I directly query the mySQL DB in the back end. But, I am having trouble getting the table to display on the webpage. It is instead displaying a "Bad Gateway" error if I attempt to display the database entries.
I tried removing the reference to the table, specifically
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>...
and the web page on front end works fine. Albeit can only see the data if I query the backend.
<?php include('php_code.php'); ?>
...
...
...
<?php $results = mysqli_query($db, "SELECT * FROM info"); ?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['city']; ?></td>
<td>
<a href="test.php?edit=<?php echo $row['id']; ?>"
class="edit_btn" >Edit</a>
</td>
<td>
<a href="php_code.php?del=<?php echo $row['id']; ?>"
class="del_btn">Delete</a>
</td>
</tr>
<?php } ?>
</table>
<!--in php_code.php-->
//to retrieve records
$select_query = "SELECT * FROM info";
$result = mysqli_query($db, $select_query);
I should be able to see the table with data containing name, address and city. But I am getting a 502 error instead.
Try this
<?php
include('php_code.php');
$results = mysqli_query($db, "SELECT * FROM `info` ");
$return = <<<HTML
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody>
HTML;
while ($row = mysqli_fetch_array($results)) {
$return .= <<<HTML
<tr>
<td>{$row['name']}</td>
<td>{$row['address']}</td>
<td>{$row['city']}</td>
<td><a href="test.php?edit={$row['id']}" class="edit_btn" >Edit</a></td>
<td>Delete</td>
</tr>
HTML;
}
$return .= <<<HTML
</tbody>
</table>
HTML;
echo $return;
?>
your php_code.php should really only have the database config...
you are closing the php statements so you cannot retrieve the result of your query. Don't split php parts and just echo html like this
<?php
echo " <table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan='2'>Action</th>
</tr>
</thead> ";
while ($row = mysqli_fetch_array($results)) {
echo "<tr>
<td>$row['name']</td>
<td>$row['address']</td>
<td> $row['city']</td>
<td>
<a href='test.php?edit=$row['id']'
class='edit_btn' >Edit</a>
</td>
<td>
<a href='php_code.php?del=$row['id']'
class='del_btn'>Delete</a>
</td>
</tr>";
}
echo "</table>";
?>

PHP MySQL only displays Blank Table

Good Day! Im having a problem displaying the data from table. I have already inputted 6 data and it will show how many base from my $i++ but it does not show the userno, fullname and udate. i would really appreciate it Please and thank you! Table Display Here is my code:
table class="table table-striped table-sm">
<thead>
<tr>
<th>No</th>
<th>User No</th>
<th>User Name</th>
<th>Date Registered</th>
</tr>
</thead>
<tbody>
<?php
$i=0;
$uquery= mysqli_query($conn, "SELECT * FROM users");
while ($uresult=mysqli_fetch_array($uquery)){
$i++;
?>
<tr>
<td><?php echo $i;?></td>
<td><?php $uresult ['userno'];?></td>
<td><?php $uresult ['fullname'];?></td>
<td><?php $uresult ['udate'];?></td>
<?php }; ?>
</tbody>
</table>
You forgot to echo your results.
Change
<td><?php $uresult ['userno'];?></td>
<td><?php $uresult ['fullname'];?></td>
<td><?php $uresult ['udate'];?></td>
To
<td><?php echo $uresult ['userno'];?></td>
<td><?php echo $uresult ['fullname'];?></td>
<td><?php echo $uresult ['udate'];?></td>
Or
<td><?= $uresult ['userno'];?></td>
<td><?= $uresult ['fullname'];?></td>
<td><?= $uresult ['udate'];?></td>
$sql = "SELECT userno, fullname, udate FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> userno: ". $row["userno"]. " - Full Name: ". $row["fullname"]. " - udate: " . $row["udate"] . "<br>";
}
} else {
echo "0 results";
}
//Don't Forget To:
$conn->close();
This'll be your PHP, adjust it accordingly to your table, hope this helps!
First of all, you have missed end</tr>, second do you really have to use variable i and increment it? where you can echo all ID? and why there's semi-colon after your closing bracket.Lastly, you should have put echo in variables uresult Hope this could help you
<table class="table table-striped table-sm">
<thead>
<tr>
<th>No</th>
<th>User No</th>
<th>User Name</th>
<th>Date Registered</th>
</tr>
</thead>
<tbody>
<?php
$uquery= mysqli_query($conn, "SELECT * FROM users");
while ($uresult=mysqli_fetch_array($uquery)){
?>
<tr>
<td><?php echo $uresult ['id'];?></td>
<td><?php echo $uresult ['userno'];?></td>
<td><?php echo $uresult ['fullname'];?></td>
<td><?php echo $uresult ['udate'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
and also you can write it, in this way:
and echo variable `i` if you want to see the count of it.
<?php
$uquery= "SELECT * from users";
$viewquery = mysqli_query($con,$uquery);
while($uresult = mysqli_fetch_assoc($viewquery))
{
?>

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.

PHP - How can I nest a while loop inside an if isset condition?

I have this table element with the following code:
<?php
if(isset($_POST["submit"])){
if (strlen($cIdMsg = 0) && strlen($cFirstNameMsg = 0) && strlen($cLastNameMsg = 0) && strlen($pCodeMsg = 0)) {
require_once("conn.php");
$sql2 = "SELECT * FROM customer;";
$results = mysqli_query($conn, $sql2)
or die ('Problem with query' . mysqli_error($conn));
echo "no errors found";
}
}
?>
<table>
<tr>
<th>Customer ID</th>
<th>FIrst Name</th>
<th>Last Name </th>
</tr>
<?php
while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row["customerID"]?></td>
<td><?php echo $row["firstName"]?></td>
<td><?php echo $row["lastName"]?></td>
</tr>
<?php } ?>
</table>
Above this table I have the php code that makes the sql queries inside an if isset condition so that it only loads after pressing submit on the form. I would like to do the same to the table. That is to only make it load after pressing submit. because on page load it is trying to do the mysqli_fetch_array on a non existent $result yet
Wrap the whole table inside:
<?php if (isset($result)) { ?>
<table>
<tr>
<th>Customer ID</th>
<th>FIrst Name</th>
<th>Last Name </th>
</tr>
<?php
while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row["customerID"]?></td>
<td><?php echo $row["firstName"]?></td>
<td><?php echo $row["lastName"]?></td>
</tr>
<?php } ?>
</table>
<?php } ?>
I have used isset($result) based on what you have said. You can check for the POST values by checking for count($_POST), or something similar (not a good idea to check for isset($_POST["submit"])). If you are fetching for AJAX Response, it is always better to use a different separate file for this.
<?php if(mysqli_num_rows($result)!=0) { ?>
<table>
<tr>
<th>Customer ID</th>
<th>FIrst Name</th>
<th>Last Name </th>
</tr>
<?php
while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row["customerID"]?></td>
<td><?php echo $row["firstName"]?></td>
<td><?php echo $row["lastName"]?></td>
</tr>
<?php } ?>
</table>
<?php } ?>

Categories