sort data from mysql table using php - php

Can you help me how to sort my data from MySQL table ?
I want to sort is by using the table head :
<?php
if(isset($_POST['search'])) {
$valueTosearch = $_POST['searchvalue'];
$query = "SELECT * FROM `admin` WHERE CONCAT(`id`, `name`, `gender`, `user_group`, `date_registered`) LIKE '%".$valueTosearch."%'";
$search_result = filterTable($query);
} else {
$query = "SELECT * FROM `admin`";
$search_result = filterTable($query);
}
function filterTable($query)
{
include('config/dbconnect.php');
$filter_result = mysqli_query($con, $query);
return $filter_result;
}
?>
<form method='post' action=''>
<div><input type = 'text' name = 'searchvalue' placeholder="search by name">
<span>
<div style='margin-bottom:3px; margin-top:3px'>
<input id='gradient' class='search-btn' type = 'hidden' name = 'search' value = 'search'>
</div>
</span>
<div style="height: auto">
<table id='responsive_table'>
<thead>
<tr>
<th scope="col">name</th>
<th scope="col">sex</th>
<th scope="col">user group</th>
<th scope="col">date register</th>
</tr>
</thead>
<?php while($row = mysqli_fetch_array($search_result)): ?>
<tbody>
<tr>
<td scope="row" data-label='name'><?php echo $row['name']; ?></td>
<td data-label='sex'><?php echo $row['gender']; ?></td>
<td data-label='user group'><?php echo $row['user_group']; ?></td>
<td data-label='date register'><?php echo $row['date_registered']; ?></td>
</tr>
</tbody>
<?php endwhile; ?>
</table>
</div>
</form>

Why don't you use order by clause:
SELECT * FROM table ORDER BY column;
Order By Reference

You can modified your query as below for sorting:
sql > select * from <table name> order by <column name>;
default sorting is ascending order else for descending you can do like
sql > select * from <table name> order by <column name> desc;

If you could use JQuery, it's very simple, you have just to add the following javascript code:
$( document ).ready(function() {
$("#responsive_table").DataTable({
ordering: true,
searching: true
});
});
for a complete example see the following code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css">
<script>
$( document ).ready(function() {
$("#responsive_table").DataTable({
ordering: true,
searching: true
});
});
</script>
</head>
<body>
<form method='post' action=''>
<div style="height: auto">
<table id='responsive_table'>
<thead>
<tr>
<th scope="col">name</th>
<th scope="col">sex</th>
<th scope="col">user group</th>
<th scope="col">date register</th>
</tr>
</thead>
<tbody>
<tr>
<td scope="row" data-label='name'>HERE</td>
<td data-label='sex'>Your</td>
<td data-label='user group'>data</td>
<td data-label='date register'>loaded</td>
</tr>
<tr>
<td scope="row" data-label='name'>via</td>
<td data-label='sex'>PHP</td>
<td data-label='user group'>loop</td>
<td data-label='date register'>bye</td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
</html>

Related

DataTable in php

Hi i want to implement DataTable in the table to populate data from my db in my php page, the below code i used, but its not working, not getting the data table, instead i am getting the normal table only -
<head>
<link rel="stylesheet" type="text/css" href="../DataTables-1.10.7/media/css/jquery.dataTables.min.css">
<script src="../DataTables-1.10.7/media/js/jquery.dataTables.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="../DataTables-1.10.7/extensions/Responsive/css/dataTables.responsive.css">
<script src="../DataTables-1.10.7/extensions/Responsive/js/dataTables.responsive.js" type="text/javascript"></script>
</head>
<div id="example_wrapper" class="dataTables_wrapper" role="grid">
<div class="fg-toolbar ui-toolbar ui-widget-header ui-corner-tl ui-corner-tr ui-helper-clearfix">
<?php
$sql="select product_id,product_name,original_price,offer_price,product_rating,image_main,stock_in_hand from niad_products order by product_id desc";
$result=$linkID1->query($sql);
$c=#mysqli_num_rows($result);
if($c>=1){?>
<script>
$(document).ready(function() {
$('#example').DataTable( {
responsive: true
} );
} );
</script>
<div id="printable">
<table id="example" class="display" cellspacing="0" width="100%" style="color:#FFF">
<thead>
<tr>
<th>Image</th>
<th>Product Name</th>
<th>Original Price</th>
<th>Offer Price</th>
<th>Rating</th>
<th>Stock</th>
</tr>
</thead>
<tbody>
<?php while($row=mysqli_fetch_array($result)){?>
<tr>
<td>
<?php echo "<img width='100' height='100' src=../product-images/".$row['image_main'] ." />"; ?></td>
<td>
<?php echo $row['product_name']; ?>
</td>
<td>
<?php echo $row['original_price']; ?>
</td>
<td>
<?php echo $row['offer_price']; ?>
</td>
<td>
<?php echo $row['product_rating']; ?>
</td>
<td>
<?php echo $row['stock_in_hand']; ?>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<?php } else {echo "No records";} ?>
</div>
</div>
Can anyone guide me with my mistake. It will be very helpful. Thanks

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>

Data not being pulled when typing keyword in the search box

I have search feature that I setup. When I type the keyword in I get no records back and no error message. Just the table header. I see the department other in the database. When I type it in the keyword box I get nothing back.
<html>
<head>
<title></title>
</head>
<body>
<form name="frmSearch" method="get" action="">
<table width="599" border="1">
<tr>
<th>Keyword
<input name="txtKeyword" type="text" id="txtKeyword" value="<?php echo $_GET["txtKeyword"];?>">
<input type="submit" value="Search"></th>
</tr>
</table>
</form>
<?php
if($_GET["txtKeyword"] != "")
{
$serverName = "localhost";
$objConnect = new PDO( "sqlsrv:server=$serverName ; Database=maintenance", "TestUser", "test") or die("Error Connect to Database");
// Search By lanId or department
$objQuery = $objConnect->prepare("SELECT * FROM requests WHERE (lanId LIKE '%".$_GET["txtKeyword"]."%' or department LIKE '%".$_GET["txtKeyword"]."%' ) ");
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">lanId </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">department </div></th>
</tr>
<?php
while( $objResult = $objQuery->fetch(PDO::FETCH_ASSOC))
{
?>
<tr>
<td><div align="center"><?php echo $objResult["lanId"];?></div></td>
<td><?php echo $objResult["name"];?></td>
<td><?php echo $objResult["department"];?></td>
<?php
}
?>
</table>
<?php
}
?>
</body>
</html>
When you use prepare() statement you should also use execute() :
http://coursesweb.net/php-mysql/pdo-prepare-execute

Php MysQl Search script returns blank page

I have written a simple page script to loop through a dynamic table that gets data from the database. But when I click on search the page returns blank with no error. I'm trying to understand what's going on without success.
display_table.php
<?php
include('session.php');
if ($_SESSION['login_user']){
include 'includes/header.php';
$searchQ = "SELECT * FROM companytable";
if(isset($_POST['search'])){
$search_term = mysqli_real_escape_string($db, $_POST['search_box']);
$searchQ .="WHERE title ='{$search_term}' ";
$searchQ .="OR country ='{$search_term}' ";
$searchQ .="OR description ='{$search_term}' ";
$searchQ .="OR timezone ='{$search_term}' ";
}
$query = mysqli_query($db, $searchQ) or die(mysqli_error());
}
form
<form class="form" name="search_form" method="POST" action="display_table.php">
<input id="search_box" style="padding: 2px;" class="" type="text" name="search_box">
<input class="btn btn-default" type="submit" name="search" value="🔍">
</form>
table
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Type</th>
<th>Address</th>
<th>Country</th>
<th>Time Zone</th>
</tr>
<?php while($company=mysqli_fetch_array($result)){ ?>
<tr>
<td data-th="ID"><?=$company['id'];?></a></td>
<td data-th="Name"><?=$company['title'];?></td>
<td data-th="Description"><?=$company['description'];?></td>
<td data-th="Type"><?=$company['type'];?></td>
<td data-th="Address"><?=$company['address'];?></td>
<td data-th="Country"><?=$company['country'];?></td>
<td data-th="Time Zone"><?=$company['timezone'];?></td>
</tr>
<?php };?>
</table>
You need to change
<?php while($company=mysqli_fetch_array($result)){ ?>
to reference the mysqli result you created, $query:
<?php while($company=mysqli_fetch_array($query)){ ?>
You can use object-oriented
<?php
include('session.php');
if ($_SESSION['login_user']){
include 'includes/header.php';
$query = "SELECT * FROM companytable ";
if(isset($_POST['search_box'])){
$search_term = $db->real_escape_string($_POST['search_box']);
$query.=" WHERE title ='{$search_term}'
OR country ='{$search_term}'
OR description ='{$search_term}'
OR timezone ='{$search_term}';";
}
if(!$s = $db->query($query)){
die($db->error);
}
}
Table
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Type</th>
<th>Address</th>
<th>Country</th>
<th>Time Zone</th>
</tr>
<?php while($m = $s->fetch_object()){ ?>
<tr>
<td data-th="ID"><?=$m->id;?></a></td>
<td data-th="Name"><?=$m->title;?></td>
<td data-th="Description"><?=$m->description;?></td>
<td data-th="Type"><?=$m->type;?></td>
<td data-th="Address"><?=$m->address;?></td>
<td data-th="Country"><?=$m->country;?></td>
<td data-th="Time Zone"><?=$m->timezone;?></td>
</tr>
<?php
};
$s->free();
?>
</table>
So turn out I decided to go with sortable.js It not only takes care of search but also takes care of sorting the rows by clicking on headers. So if you are looking for a clean fix, that would be it.
form
<form class="form" name="search_form"> <input id="search" style="" class="form-control" type="text" name="search" placeholder="🔍 Search...">
table
<table id="table" class="sortable" >
<thead style="cursor:pointer;">
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Type</th>
<th>Address</th>
<th>Country</th>
<th>Time Zone</th>
</thead>
<tbody>
<?php while($company=mysqli_fetch_array($result)){ ?>
<tr>
<td data-th="ID" sorttable_customkey="2"><?=$company['id'];?></a></td>
<td data-th="Name"><?=$company['title'];?></td>
<td data-th="Description"><?=$company['description'];?></td>
<td data-th="Type"><?=$company['type'];?></td>
<td data-th="Address"><?=$company['address'];?></td>
<td data-th="Country"><?=$company['country'];?></td>
<td data-th="Time Zone"><?=$company['timezone'];?></td>
</tr>
<?php };?>
</tbody>
<tfoot></tfoot>
</table>
Search script
<script src="js/sorttable.js"></script> //load sortable.js
<script type="text/javascript">
var $search_rows = $('#table tr');
$('#search').keyup(function() {
var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
$search_rows.show().filter(function() {
var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
return !~text.indexOf(val);
}).hide();
});
</script>

php and oracle detele row [duplicate]

This question already exists:
PHP & Oracle database search data
Closed 9 years ago.
I am using this code for searching data from my table CUSTOMER but I am getting an error:
in my keyword text felid show this error----->>>>
Notice: Undefined index: txtKeyword in C:\xampp\htdocs\new_test\php_oracle_search.php on line 10
Code:
<html>
<head>
</head>
<body>
<?php
$txtKeyword = '';
if(isset($_GET['txtKeyword']) && strlen(trim($_GET['txtKeyword'])) > 0) {
$txtKeyword = trim($_GET['txtKeyword']);
}
?>
<form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>">
<table width="599" border="1">
<tr>
<th>Keyword
<input name="txtKeyword" type="text" id="txtKeyword" value=" =$_GET["txtKeyword"];?>">
<input type="submit" value="Search"></th>
</tr>
</table>
</form>
<?
if($_GET["txtKeyword"] != "")
{
$objConnect = oci_connect("system","dm","XE");
$strSQL = "SELECT * FROM CUSTOMER WHERE (NAME LIKE '%".$_GET["txtKeyword"]."%'
or EMAIL LIKE '%".$_GET["txtKeyword"]."%' ) ";
$objParse = oci_parse ($objConnect, $strSQL);
oci_execute ($objParse);
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="59"> <div align="center">Budget </div></th>
<th width="71"> <div align="center">Used </div></th>
</tr>
<?
while($objResult = oci_fetch_array($objParse,OCI_BOTH))
{
?>
<tr>
<td><div align="center"><?=$objResult["CUSTOMERID"];?></div></td>
<td><?=$objResult["NAME"];?></td>
<td><?=$objResult["EMAIL"];?></td>
<td><div align="center"><?=$objResult["COUNTRYCODE"];?></div></td>
<td align="right"><?=$objResult["BUDGET"];?></td>
<td align="right"><?=$objResult["USED"];?></td>
</tr>
<?
}
?>
</table>
<?
oci_close($objConnect);
}
?>
</body>
</html>
Use below code to validate txtKeyword before using it:
if(isset($_GET['txtKeyword']) && strlen(trim($_GET['txtKeyword'])) > 0) {
//handle
}
For the notice error inside textbox, do below:
Put following code after opening <body> tag.
$txtKeyword = '';
if(isset($_GET['txtKeyword']) && strlen(trim($_GET['txtKeyword'])) > 0) {
$txtKeyword = trim($_GET['txtKeyword']);
}
Then, wherever you want $_GET['txtKeyword'], use $txtKeyword instead.
Cannot explain much simpler than this.

Categories