Bootstrap DataTables missing sorting/searching - php

I'm using Bootstrap DataTables to display data from database but I have an issue with missing sorting and searching. Values from database are displaying correctly so I'm assuming I have syntax error(missing ' or sth), it allows me to display data but features of DataTables aren't working.
Here is my code:
<div class="table-responsive">
<table id="orders_data" class="table table-striped table-bordered">
<thead>
<tr>
<td> Imię </td>
<td> Nazwisko </td>
<td> Książki </td>
</tr>
</thead>
<?php
while($row = mysqli_fetch_array($result))
{
$sql2 = "SELECT * FROM books_has_authors JOIN books on books_has_authors.books_book_id = books.book_id
WHERE authors_id_author = '".$row["id_author"]."'";
$result2 = mysqli_query($conn, $sql2);
echo '
<tr>
<td>'.$row['fname'].'</td>
<td>'.$row['lname'].'</td>
<td>';
while($row2 = mysqli_fetch_array($result2)){
echo '<p>'.$row2['name'].'</p>';
}
echo '</td>';'
</tr>
';
}
?>
</table>
</div>

Related

DATATABLE plugin Fetch only first row only in dynamic data

<div class="table-responsive">
<table class="table table-hover" id="dataTable" width="100%" cellspacing="0" >
<thead>
<tr class=" card-header py-2" style=" text-align:center;">
<th> Er no</th>
<th> Name</th>
<th> Branch</th>
<th> Adm Year</th>
<th>Profile</th>
</tr>
<?php
if (isset($_POST['pass'])) {
$d = $_POST['spass'];
include('bcpdb.php');
$iq = "select * from stuinfo where ERNO like '%$d' ";
$qs = mysqli_query($con, $iq);
//echo "Serch Query Data";
if (!$qs) {
echo mysqli_error($con) . " DATA NOT INSERTED";
}
} else {
include('bcpdb.php');
$iq = "select * from stuinfo ";
$qs = mysqli_query($con, $iq);
//echo "select Query Data";
}
if (!$qs) {
echo mysqli_error($con) . " DATA NOT INSERTED";
}
while ($res = mysqli_fetch_assoc($qs)) {
?>
<tbody>
<tr>
<td> <?php echo $res['ERNO']; ?></td>
<td> <?php echo $res['SNAME']; ?></td>
<td><?php echo $res['BRANCH']; ?></td>
<td> <?php echo $res['ADMYEAR']; ?></td>
<td style="text-align:center;">
</td>
<?php
}
?>
</tr>
</tbody>
</table>
Just show rows in page not fetch all rows in datatable. I want all of the data of database fetch in datatable plugin, when I put some data in html table row column then it works perfectly, fetch and search all rows of data but in database data which fetch by select query, fetch only one row
[1]: https://i.stack.imgur.com/44FRd.jpg

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 database get price total [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 5 years ago.
I have using php code with mysql database showing data into html table, and i'm using filter on listview, now I'm stack with get price Total of the list after filtered. I'm not good enough with php code, am I made mistake with php or sql?
MySQL table:
id INT -
name VARCHAR -
code VARCHAR -
price INT
here is my code so far:
<?php
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT * FROM `toy` WHERE CONCAT(`id`, `name`, `code`, `price`)
LIKE '%".$valueToSearch."%'";
$search_result = filterTable($query);
}
else {
$query = "SELECT * FROM `toy` LIMIT 5";
$search_result = filterTable($query);
}
// function to connect and execute the query
function filterTable($query)
{
$connect = mysqli_connect("localhost", "user", "pass",
"database");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Wu</title>
<link href="style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="toys-grid">
<div class="search-box">
<br>
<form action="index.php" method="post">
<input type="text" name="valueToSearch" placeholder="Name To Search">
<br><br>
<input type="submit" name="search" value="Filter"><br><br>
</div>
<table cellpadding="10" cellspacing="1">
<thead>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Code</th>
<th>Price</th>
</tr>
</thead>
<!-- populate table from mysql database -->
<?php while($row = mysqli_fetch_array($search_result)):?>
<tbody>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['code'];?></td>
<td><?php echo $row['price'];?></td>
</tr>
</tbody>
<?php endwhile;?>
<thead>
<tr>
<th> Total </th>
<th>
<?php
$results = mysql_query('SELECT SUM(price) FROM toy');
$results->execute();
for($i=0; $rows = $results->fetch(); $i++){
echo $rows['SUM(price)'];
}
?>
</th>
</tr>
</thead>
</table>
</form>
</div>
</body>
I appreciate your help...
No need to execute query for getting sum. You can do it with php
<table cellpadding="10" cellspacing="1">
<thead>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Code</th>
<th>Price</th>
</tr>
</thead>
<!-- populate table from mysql database -->
<?php $sum = 0; ?>
<?php while($row = mysqli_fetch_array($search_result)):?>
<tbody>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['code'];?></td>
<td><?php echo $row['price'];?></td>
<?php
$sum += $row['price'];
?>
</tr>
</tbody>
<?php endwhile;?>
<thead>
<tr>
<th> Total </th>
<th>
<?php
echo $sum;
?>
</th>
</tr>
</thead>
</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.

What is the best way to write heredocs in php and how to keep function max 20 lines

Following is the code of Class Admin that contains two method
listRecord();
Details($id);
The code is working fine but i don't like this code because function is huge and coupled and i don't want to use heredocs in function Kindly guide me what is the best way to write heredocs and how to separate heredocs in separate function.
Admin Class
Class Admin{
public function listRecord() {
$query = "select Report_type, Report_Id, Report_Reults,Patient_name, Patient_Address, "
. "Patient_Phone from Patient join Reports "
. "on Patient.Patient_ID = Reports.Patient_ID ";
$result = $this->conn->query($query);
if ($result->num_rows > 0) {
// output data of each row
echo <<<HTML
<h2>Report's Data</h2>
<table>
<tr>
<th>Patient Name</th>
<th>Patient Address</th>
<th>Patient Phone</th>
<th>Report Type</th>
<th>Report Results</th>
</tr>
<tr>
HTML;
while ($row = $result->fetch_assoc()) {
echo"
<tr>
<td> $row[Patient_name] </td>
<td> $row[Patient_Address] </td>
<td> $row[Patient_Phone] </td>
<td> $row[Report_type] </td>
<td> $row[Report_Reults] </td>
<td><a href='../Admin/AdminData.php?del_id=$row[Report_Id]'>Delete</a></td>
<td><a href='../Admin/AdminData.php?report_id=$row[Report_Id]'>Details</a></td>
</tr>";
}
echo "</table>";
}
}
}
public function Details($id) {
$this->data_id = $id;
$query = "select Report_type, Report_Id, Report_Reults,Patient_name, Patient_Address, "
. "Patient_Phone from Patient join Reports WHERE "
. "Reports.Report_Id=$this->data_id and Patient.Patient_ID=$this->data_id ";
$result = $this->conn->query($query);
if ($result->num_rows > 0) {
// output data of each row
echo <<<HTML
<h2>$row[Patient_name] Data</h2>
<table>
<tr>
<th>Patient Name</th>
<th>Patient Address</th>
<th>Patient Phone</th>
<th>Report Type</th>
<th>Report Results</th>
</tr>
<tr>
HTML;
while ($row = $result->fetch_assoc()) {
echo"
<tr>
<td> $row[Patient_name] </td>
<td> $row[Patient_Address] </td>
<td> $row[Patient_Phone] </td>
<td> $row[Report_type] </td>
<td> $row[Report_Reults] </td>
</tr>";
}
echo "</table>";
}
}
Try saving the HTML for both functions in different files and including them when required. You could use .phtml format for these files, which is HTML with some PHP.
if ($result->num_rows > 0) {
require_once FILE_PATH; // Path to the specific file needed in the function.
}

Categories