Datatables PHP MYSQL - How to display specific entries - php

I have a database with the following columns:
(okladka, imie, nazwisko, tytul, rodzaj, info, download, data)
My DB name: serwer144221_SwiatBook
Table name: bazaplikow
In the value "rodzaj" each entry contains one word: "audiobook" or "ebook"
I would like to display in the table only those entries where the word in "ebook" appears.
How to read all rows from a database table where the column "rodzaj" contains a value "ebook"
This is my Source Code Datatables:
<?php
$servername = "xyz";
$username = "xyz";
$password = "xyz";
$database = "serwer144221_SwiatBook";
// Create connection
$connection = new mysqli($servername, $username, $password, $database);
// Check connection
if ($connection->connect_error) {
die("Connection failed: " . $connection->connect_error);
}
// read all row from database table
$sql = "SELECT DISTINCT rodzaj * FROM bazaplikow";
$result = $connection->query($sql);
if (!$result) {
die("Invalid query: " . $connection->error);
}
?>
<section class="section-1">
<h3 align="center">Baza plików</h3>
<br />
<div class="table-responsive">
<table id="bazaplikow_data" class="table table-striped table-bordered" width="100%";>
<thead>
<tr>
<td align="center">Okładka</td>
<td align="center">Imię</td>
<td align="center">Nazwisko</td>
<td align="center">Tytuł</td>
<td align="center">Rodzaj</td>
<td align="center">Info</td>
<td align="center">Download</td>
<td align="center">Data</td>
</tr>
</thead>
<?php
while($row = mysqli_fetch_array($result))
{
echo '
<tr>
<td align="center"><img src="'.$row["okladka"].'" width="98" height="128"></td>
<td align="center">'.$row["imie"].'</td>
<td align="center">'.$row["nazwisko"].'</td>
<td align="center">'.$row["tytul"].'</td>
<td align="center">'.$row["rodzaj"].'</td>
<td align="center"><i class="fa fa-info-circle" aria-hidden="true"></i></td>
<td align="center"><i class="fa fa-download" aria-hidden="true"></i></td>
<td align="center">'.$row["data"].'</td>
</tr>
';
}
?>
</table>
</div>
</section>
<script>
$(document).ready(function () {
$('#bazaplikow_data').DataTable({
order: [[7, 'desc']],
});
});
</script>

Your query is wrong it should be
SELECT * from bazaplikow where rodzaj = 'ebook';

Related

Display info from database into HTML table in different cells

What I am trying to do is to assign a cell from my HTML table to a specific record from my database. I am doing a table with COLSPAN, so it is not the same number of columns in every row. The code for the table is something like this:
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<tbody>
<tr>
<td colspan="4">TITLE
</td>
</tr>
<tr>
<td colspan="2">tag1</td>
<td colspan="2">info from database</td>
</tr>
<tr>
<td >tag2</td>
<td>info from database</td>
<td>tag3</td>
<td>info from database</td>
</tr>
</tbody>
</table>
</body>
</html>
This would be the query
$con = new mysqli($servername, $username, $password, $database);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$sql = "SELECT * FROM table WHERE id=id ";
$result = $con->query($sql);
Any help would be great! Thanks
You can simply fetch your results and for every result you echo your table out
while($row = mysqli_fetch_assoc($result)) {
echo "
<tr>
<td colspan='4'>TITLE
</td>
</tr>
<tr>
<td colspan='2'>tag1</td>
<td colspan='2'>".$row['NAME OF THE ROW']."</td>
</tr>
<tr>
<td >tag2</td>
<td>".$row['NAME OF THE ROW']."</td>
<td>tag3</td>
<td>".$row['NAME OF THE ROW']."</td>
</tr>
";
}
Please use the given below code to show you db data in table
while($result = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['name']."</td>";
echo "<td>".$res['age']."</td>";
echo "<td>".$res['email']."</td>";
echo "<tr>";
}

Pass value to next page on table row click?

I have a table that is not currently clickable. On row click, I would like to go to a detail page from this table, passing the id value in the process.
I understand I need to make an href on the table row and somehow pass the id value on click, but am not clear on how to do this. Relevant table and php:
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-body">
<div class="table-responsive m-t-40">
<table id="example23" class="display nowrap table table-hover table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<thead><tr><th title="Field #1">id</th>
<th title="Field #2">Organization</th>
<th title="Field #3">xxx</th>
<th title="Field #4">xxx</th>
<th title="Field #5">xxx</th>
<th title="Field #6">xxx</th>
</tr>
</thead>
<tbody>
<?php
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
echo '<script>console.log("Connection successful!")</script>';
}
$SELECT = mysqli_query($conn,"SELECT * FROM `organization`");
if($SELECT != false)
{
while($rows = mysqli_fetch_array($SELECT))
{
echo "
<tr>
<td>".$rows["id"]."</td>
<td>".$rows["name"]."</td>
<td>".$rows["xxx"]."</td>
<td>".$rows["xxx"]."</td>
<td>".$rows["xxx"]."</td>
<td>".$rows["xxx"]."</td>
</tr>
";
}
}else{
echo "
<tr>
<td colspan='3'>Something went wrong with the query</td>
</tr>
";
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
How do I make my table rows clickable (to go to the detail page) and how do I pass the id value from the table on click to the detail page?
you need to make href tag and pass the id like this
click
in your case it should be
<td>click</td>
your while loop should be like this
while($rows = mysqli_fetch_array($SELECT)){ ?>
<tr>
<td>click</td>
<td><?php echo $rows["name"]; ?></td>
<td><?php echo $rows["xxx"]; ?></td>
<td><?php echo $rows["xxx"]; ?></td>
<td><?php echo $rows["xxx"]; ?></td>
<td><?php echo $rows["xxx"]; ?></td> <td>".$rows["xxx"]."</td>
</tr>
<?php }
<td>".$rows["xxx"]."</td>
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-body">
<div class="table-responsive m-t-40">
<table id="example23" class="display nowrap table table-hover table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr><th title="Field #1">id</th>
<th title="Field #2">Organization</th>
<th title="Field #3">xxx</th>
<th title="Field #4">xxx</th>
<th title="Field #5">xxx</th>
<th title="Field #6">xxx</th>
</tr>
</thead>
<tbody>
<?php
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
echo '<script>console.log("Connection successful!")</script>';
}
$SELECT = mysqli_query($conn,"SELECT * FROM `organization`");
if($SELECT != false)
{
while($rows = mysqli_fetch_array($SELECT))
{
echo "
<tr onclick="window.location='detail.php?id=$rows["id"]';">
<td>".$rows["id"]."</td>
<td>".$rows["name"]."</td>
<td>".$rows["xxx"]."</td>
<td>".$rows["xxx"]."</td>
<td>".$rows["xxx"]."</td>
<td>".$rows["xxx"]."</td>
</tr>
";
}
}else{
echo "
<tr>
<td colspan='3'>Something went wrong with the query</td>
</tr>
";
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
You can set onclick() function to your tag using plain javascript.. try this.
You need to change your while loop as below: add window.location on tr click
<?php
while($rows = mysqli_fetch_array($SELECT))
{
echo "
<tr onclick=\"window.location='detail.php?id=".$rows["id"]."'\">
<td>".$rows["id"]."</td>
<td>".$rows["name"]."</td>
<td>".$rows["xxx"]."</td>
<td>".$rows["xxx"]."</td>
<td>".$rows["xxx"]."</td>
<td>".$rows["xxx"]."</td>
</tr>
";
}?>

Use Select option as search with dynamic fields

I am making a site linked with a database. It sorts transactions for budgeting reasons. For the store search section, I have a select drop down menu. I have linked up the data so that the drop down only shows stores that are in the database and automatically adds them as they change dynamically.
My issue is that I need a way to actually use the select options as search terms. Here is the initial page.
<!DOCTYPE html>
<html>
<head>
<title>Output 1</title>
</head>
<body>
<h1>Required Output 1</h1>
<h2>Transaction Search</h2>
<form action="output1out.php" method="get">
Search Store:<br/>
<input type="text" name="StoreName">
<br/>
<input name="Add Merchant" type="submit" value="Search">
</form>
<?php
require_once 'login.php';
$connection = mysqli_connect($db_hostname, $db_username,$db_password, $db_database);
if(mysqli_connect_error()){
die("Database Connection Failed: ".mysqli_connect_error()." (".mysqli_connect_errno().")");
};
$query = "SELECT * from PURCHASE";
//echo $query;
$result = mysqli_query($connection,$query);
if(!$result) {
die("Database Query Failed!");
};
$distinct = "SELECT DISTINCT StoreName FROM PURCHASE";
$distinctResult = mysqli_query($connection,$distinct);
if(!$result) {
die("Database Query Failed!");
};
echo '<select name="search_string" />'."\n";
while ($row = mysqli_fetch_assoc($distinctResult)){
echo '<option value="'.$row["StoreID"].'">';
echo $row["StoreName"];
echo '</option>'."\n";
};
echo '</select>'."\n";
mysqli_free_result($result);
mysqli_close($connection);
?>
Here is the output page.
<?php
$transaction = $_REQUEST["StoreName"];
require_once 'login.php';
$connection = mysqli_connect($db_hostname, $db_username,$db_password, $db_database);
$sql = "SELECT * FROM PURCHASE WHERE StoreName LIKE '%".$transaction."%'";
$result = $connection->query($sql);
?>
Purchases Made From <?php echo $transaction ?>
<table border="2" style="width:100%">
<tr>
<th width="15%">Item Name</th>
<th width="15%">Item Price</th>
<th width="15%">Purchase Time</th>
<th width="15%">Purchase Date</th>
<th width="15%">Category</th>
<th width="15%">Rating</th>
</tr>
</table>
<?php
if($result->num_rows > 0){
// output data of each row
while($rows = $result->fetch_assoc()){ ?>
<table border="2" style="width:100%">
<tr>
<td width="15%"><?php echo $rows['ItemName']; ?></td>
<td width="15%"><?php echo $rows['ItemPrice']; ?></td>
<td width="15%"><?php echo $rows['PurchaseTime']; ?></td>
<td width="15%"><?php echo $rows['PurchaseDate']; ?></td>
<td width="15%"><?php echo $rows['Category']; ?></td>
<td width="15%"><?php echo $rows['Rating']; ?></td>
</tr>
<?php
}
}
?>
I can get regular typing search to work but I can't think of a way to search using the same method. Is it possible?

How to display rows with same id but different row values?

I'm not sure how to describe it in words so i'll show you:
View scores table
I'm trying to display rows that has the same id, and display their values but in different tables. As seen in the picture, I have to make a table like that for every row that has the same ID. The main purpose of it is to compare their scores from before, when they first took the exams, to what the score they got now from the same set of exams.
This is what my database looks like right now. They have different scoreIDs and other row values but has the same id_applicant for it refers that these scores are of this certain applicant. Database Snippet
This is what my code for it looks like. (id_applicant is the one that they will have the same id)
<!-- VIEW SCORES -->
<div style="background-color: #E9E9E9" class="tab-pane" id="ViewScoresTab">
<div class="list-group">
<div id="view-scores">
<div class="form-group" style="margin-left:20px">
<br>
<div id="inputhere"></div>
<h1><span class="label label-default">View Scores</span></h1>
<br><br>
<table>
<tr>
<th bgcolor= '#00BFFF'>TEST</th>
<th bgcolor= '#FFD700'>SCORES</th>
<th bgcolor= '00FF00'>REMARKS</th>
</tr>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "offsourcing";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if(isset($ids_holder)){
$q = "SELECT * FROM scores WHERE id_applicant = $ids_holder";
$result = mysqli_query($conn, $q);
$rowcount = mysqli_num_rows($result);
for(int x=0;x!=rowcount;x++){
//some code for here
}
while($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
echo
"<tr>
<td class= 'test'> Grammar </td>
<td> $row[2] </td>
<td> $row[8] </td>
</tr>
<tr>
<td class= 'test'> Customer Service </td>
<td> $row[3] </td>
<td> $row[9] </td>
<tr>
<td class= 'test'> Logical Reasoning </td>
<td> $row[4] </td>
<td> $row[10] </td>
</tr>
<tr>
<td class= 'test'> Idioms </td>
<td> $row[5] </td>
<td> $row[11] </td>
</tr>
<tr>
<td class= 'test'> Call Listening (1) </td>
<td> $row[6] </td>
<td> $row[12] </td>
</tr>
<tr>
<td class= 'test'> Call Listening (2) </td>
<td> $row[7] </td>
<td> $row[13] </td>
</tr>";
mysqli_close($conn);
}
}
?>
</table>
<br><br>
<br><br>
<br><br>
</div>
</div>
</div>
</div>
</body>
</html>
I'm not exactly sure how to do it yet so any idea?
Instead of targeting the number in the Array, directly target the column.
So replacing your while loop with something like:
while($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
echo
'<tr>
<td class= 'test'> Grammar </td>
<td>'.$row['GrammarScore'].'</td>
</tr>
<tr>
<td class= 'test'> Customer Service </td>
<td>'.$row['CustomerServiceScore'].'</td>
</tr>'; //etc }
Is this what you mean? You said something about comparing the older scores to the newer ones, but you didn't really post anything about how you're calling these records, assuming they're on a different table?

PHP Include Page which shows information based on value of Page 1

Have been struggling with making this work. Not sure where I'm going wrong.
main page: pkg_list_30d.php
<?php
$conn = new mysqli('host', 'user', 'pwd', 'db');
// check connection
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
// check connection
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
// SELECT sql query
$sql = "SELECT pkg.*, i.isotope AS pkgisotope
FROM tbl_packagereceipt pkg
INNER JOIN tbl_isotopes i
on
pkg.isotope = i.isoID
GROUP BY pkg.pkgID
ORDER BY `datereceived` DESC
LIMIT 5";
// perform the query and store the result
$result = $conn->query($sql);
// if the $result contains at least one row
if ($result->num_rows > 0) {
// output data of each row from $result
echo '
<table width="568" border="0" cellspacing="1" cellpadding="1">
<tr>
<td width="80" > </td>
<td width="110"> </td>
<td width="108"> </td>
<td width="150"> </td>
<td width="120" > </td>
</tr>
<tr>
<td> </td>
<td><strong>Date</strong></td>
<td><strong>Package #</strong></td>
<td><strong>Isotope</strong></td>
<td> </td>
</tr>
</table>';
$c = false;
while($row = $result->fetch_assoc())
{
echo '<table width="568" border="0" cellspacing="1" cellpadding="1">
<tr style="background:',(($c=!$c)? '#eee' : '#ddd' ),'">
<td width="80"> </td>
<td width="110">'.date('d-M-Y', strtotime($row['datereceived'])).'</td>
<td width="108">'.$row['pkgnumber'].'</td>
<td width="150">'.$row['pkgisotope'].'</td>
<td width="120">' . '<a class="gegevens2"
href="../patientinjection/record_inj_form.php?id=' . $row['pkgID'] . '"> ' .
"Add Patient". '</a>' . '</td>
</tr>
</table>
<br />';
echo include 'pkg_patient.php';
}
}
else {
echo 'All packages returned.';
}
$conn->close();
?>
Directly under the table within the while() I would like it to display the patients for that result. I have tried doing an include of a page, but it just shows only the top row of the main page (instead of maybe 5 or 6 rows based on query results). And it shows the number 1 under it.
The code on the pkg_patient.php is:
<?php
$conn = new mysqli(removed);
// check connection
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
// check connection
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
// SELECT sql query
$pkgID = (int)$_GET[$id];
$sql = "SELECT pdi.*, radp.radiopharmaceutical AS radp, pkp.initials
FROM tbl_patientdoseinformation pdi
INNER JOIN tbl_isotopes i
ON
pdi.isotope = i.isoID
INNER JOIN tbl_radpharmaceuticals radp
ON pdi.isotope = radp.isotopeID
INNER JOIN tbl_packagepersonnel pkp
ON pdi.adminby = pkp.pkgpersonnelID
WHERE pdi.pkgnumberID='" . $pkgID . "'
GROUP BY pdi.patientdoseID
ORDER BY `datetimestated` DESC";
// perform the query and store the result
$result = $conn->query($sql);
// if the $result contains at least one row
if ($result->num_rows > 0) {
// output data of each row from $result
echo '<table width="1103" border="0" cellspacing="1" cellpadding="1">
<tr>
<td colspan="9"></td>
</tr>
<tr>
<td width="80" > </td>
<td width="110"> </td>
<td width="108"> </td>
<td width="161"> </td>
<td width="84"> </td>
<td width="151"> </td>
<td width="83"> </td>
</tr>
<tr>
<td> </td>
<td><strong>Date</strong></td>
<td><strong>Case No.</strong></td>
<td><strong>Radiopharmaceutical</strong></td>
<td><strong><div align="center">Dose</div></strong></td>
<td><strong><div align="right">State Date/Time</div></strong></td>
<td><strong><div align="right">Initials</div></strong></td>
</tr>
</table>
<br />';
$c = false;
while($row = $result->fetch_assoc())
{
echo '<table width="1103" border="0" cellspacing="1" cellpadding="1">
<tr style="background:',(($c=!$c)? '#eee' : '#ddd' ),'">
<td width="80">' . '<a class="gegevens" href="edit_inj_form.php?id=' .
$row['patientdoseID'] . '"> ' . "Edit". '</a>' . '</td>
<td width="117">'.date('d-M-Y', strtotime($row['datetimestated'])).'</td>
<td width="108">'.$row['patientID'].'</td>
<td width="161">'.$row['radp'].'</td>
<td width="84"><div align="right">'.$row['dose'].' mCi</div></td>
<td width="180"><div align="right">'.date('d-M-Y H:i',
strtotime($row['datetimestated'])).'</div></td>
<td width="83"><div align="right">'.$row['initials'].'</div></td>
</tr>
</table>';
}
}
else {
echo ' ';
}
$conn->close();
?>
You don't need to re-run the query in pkg_patient.php, all the variables from pkg_list_30d.php are accessible from pkg_patient.php once you've included it...
In pkg_list_30d.php on line 68 you use
echo include 'pkg_patient.php';
Instead you should echo the data you're wanting to display on pkg_patient.php and include it using include pkg_patient.php on pkg_list_30d.php

Categories