My search won't work and I don't know why. I would like to search for clients or tickets in my database. When I click on search while the SEARCH field ( input) is empty I get all clients BUT when I type some thing into the field I get nothing. Here is the code :
<table class="table table-bordered" id="datatable">
<thead>
<tr>
<th width="6%">name</th>
<th width="6%">prenom</th>
<th width="12%">adresse</th>
<th width="3%">codepost</th>
<th width="6%">ville</th>
<th width="5%">telephone</th>
<th width="8%">email</th>
<th width="6%">type</th>
<th width="5%">Action</th>
</tr>
</thead>
<tbody>
<?php
require 'database.php';
$db = Database::connect();
$statement = $db->query("SELECT * FROM clients WHERE 'prenom' LIKE '%" . $_POST['prenom'] . "%' ORDER BY client_id DESC" );
while($item = $statement->fetch())
{
echo '<tr>';
// echo '<td>'. $item['id'] . '</td>';
echo '<td>'. $item['name'] . '</td>';
echo '<td>'. $item['prenom'] . '</td>';
echo '<td>'. $item['adress'] . '</td>';
echo '<td>'. $item['codepost'] . '</td>';
echo '<td>'. $item['ville'] . '</td>';
echo '<td>'. $item['telephone'] . '</td>';
echo '<td>'. $item['email'] . '</td>';
echo '<td>'. $item['type'] . '</td>';
echo '<td width=150>';
echo '<a class="btn btn-default " href="view.php?id='.$item['client_id'].'"><span class="glyphicon glyphicon-eye-open"></span></a>';
echo ' ';
echo '<a class="btn btn-primary" href="update.php?id='.$item['client_id'].'"><span class="glyphicon glyphicon-pencil"></span></a>';
echo ' ';
echo '<a class="btn btn-danger" href="deleteclient.php?id='.$item['client_id'].'"><span class="glyphicon glyphicon-remove"></span> </a>';
echo '</td>';
echo '</tr>';
}
Database::disconnect();
?>
</tbody>
</table>
i finaly did it .. thanks to who helped me (
Sam Onela ) here is the code if some one need ``it := `
name
prenom
adresse
codepost
ville
telephone
email
type
Action
prepare(" SELECT * FROM clients WHERE name LIKE ? ");
$req->execute(array('%' .$searchq. '%')) ;
$count = $req->rowCount();
echo " $count Résulta(s) trouvé pour <strong >$searchq></strong><hr/> " ;
while($data = $req->fetch()) {
echo '<tr>';
echo '<td>'. $data['name'] . '</td>';
echo '<td>'. $data['prenom'] . '</td>';
echo '<td>'. $data['adress'] . '</td>';
echo '<td>'. $data['codepost'] . '</td>';
echo '<td>'. $data['ville'] . '</td>';
echo '<td>'. $data['telephone'] . '</td>';
echo '<td>'. $data['email'] . '</td>';
echo '<td>'. $data['type'] . '</td>';
echo '<td width=150>';
echo '<a class="btn btn-default " href="view.php?id='.$data['client_id'].'"><span class="glyphicon glyphicon-eye-open"></span></a>';
echo ' ';
echo '<a class="btn btn-primary" href="update.php?id='.$data['client_id'].'"><span class="glyphicon glyphicon-pencil"></span></a>';
echo ' ';
echo '<a class="btn btn-danger" href="deleteclient.php?id='.$data['client_id'].'"><span class="glyphicon glyphicon-remove"></span> </a>';
echo '</td>';
echo '</tr>';
}
}
Database::disconnect();
?>
</tbody>
</table>`
Related
EDITED
I have an sql query and I want to highlight the table row with a red highlight so that if the transaction_date = 0000-00-00 that whole row is highlighted with red. Here is the snippet of the code. The transaction_date shows the date that a specific training course was taken and if the course wasn't taken then the date that will be returned will be 0000-00-00. Any help would be greatly appreciated. Thank you.
$sql = "SELECT DATE_FORMAT(date_fluids, '%m/%d/%Y ') AS transaction_date, first_name, last_name, supervisor_group, trade, t_id, c_id, department_number, date_fluids, fluids_refresh, fluid_period, employee_type
FROM peopleinfo WHERE
(last_name LIKE '%$value1%' )
and
(trade LIKE '%$value2%')
and
(t_id LIKE '%$value3%')
and
(c_id LIKE '%$value4%')
and
(department_number LIKE '%$value5%')
and
(date_fluids LIKE '%$value6%')
and
(date_lock LIKE '%$value7%')
and
(date_confine LIKE '%$value8%')
and
(date_fall LIKE '%$value9%')
and
(supervisor_group LIKE '%$value10%')
AND
(employee_type='1')
ORDER BY last_name ASC";
$query = mysqli_query($conn, $sql);
$row = (mysqli_fetch_array($query));
if (!$query) {
die ('SQL Error: ' . mysqli_error($conn));
}
while ($row = mysqli_fetch_array($query))
{
if ($row['transaction_date'] == 0000-00-00){
echo '<tr class="highlight">';
echo '<td >' . $row['first_name']. '</td>';
echo '<td >' . $row['last_name']. '</td>';
echo '<td >' . $row['supervisor_group']. '</td>';
echo '<td >' . $row['trade']. '</td>';
echo '<td >' . $row['t_id']. '</td>';
echo '<td >' . $row['c_id']. '</td>';
echo '<td >' . $row['department_number']. '</td>';
echo '<td >' . $row['transaction_date']. '</td>';
echo '<td >'. $row['fluid_period']. '</td>';
echo '</tr>';
} else {
echo '<tr >';
echo '<td >' . $row['first_name']. '</td>';
echo '<td >' . $row['last_name']. '</td>';
echo '<td >' . $row['supervisor_group']. '</td>';
echo '<td >' . $row['trade']. '</td>';
echo '<td >' . $row['t_id']. '</td>';
echo '<td >' . $row['c_id']. '</td>';
echo '<td >' . $row['department_number']. '</td>';
echo '<td >' . $row['transaction_date']. '</td>';
echo '<td >'. $row['fluid_period']. '</td>';
echo '</tr>';
}
}
mysqli_free_result($query);
mysqli_close($conn);
?>
</tbody>
</table>
I would just do:
echo '<tr' . ($row['transaction_date'] === '0000-00-00' ? 'class="highlight"' : '') . '>
and then use css to highlight it however you want.
Please help cant display image when I use php
$result = mysql_query("SELECT * FROM obs") or die(mysql_error());
echo "<center><table><tr><th width=200>Obs Number</th><th width=200>Name</th><th width=200>Purpose</th><th width=200>Date Of OB</th><th width=200>Destination</th><th width=200>Image</th></tr></center>";
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
//cant display image
echo '<tr> <input type="hidden" name="showimage[]" value="'. $row['OBS_NUMBER'].'" >';
echo '<td width=200>' . $row['OBS_NUMBER'] . '</td>';
echo '<td width=200>' . $row['NAME'] . '</td>';
echo '<td width=200>' . $row['PURPOSE'] . '</td>';
echo '<td width=200>' . $row['DATE_OF_OB'] . '</td>';
echo '<td width=200>' . $row['DESTINATION'] . '</td>';
echo '<td width=200> <img src="data:image/jpeg;base64,' . base64_encode($row['imagesobs']) . '" width="250"></td>';
echo '</tr>';
}
I am working on a project in which a tutor can save its class timing. He can see his timing according to the days. I used the code
$qry = mysqli_query($con, 'select * from users left join time_slot on users.id=time_slot.u_id where users.id=' . $id);
echo '<table class="table_class" border="2">
<tr>
<th class="details">id</th>
<th class="details">Date</th>
<th class="details">start time</th>
<th class="details">End Time</th>
</tr>';
while ($row = mysqli_fetch_array($qry)) {
echo '<tr>';
echo '<td class="details">' . $row['id'] . '</td>';
echo '<td class="details">' . $row['name'] . '</td>';
echo '<td class="details">' . $row['day'] . '</td>';
echo '<td class="details">' . $row['time_from'] . '</td>';
echo '<td class="details">' . $row['time_to'] . '</td>';
echo '</tr>';
}
echo '</table>';
But It show the multiple time if a tutor have multiple class in same day.
I want to show if he has 2 or more class on similar day(Monday) then all time slot show in a single row. Same this for all days of the week. How can I do it?
You can use GROUP_CONCAT function for this. Assuming your ddl is something like that
create table users(id bigint, name varchar(50));
create table time_slot(id bigint, u_id bigint, day datetime, time_from time, time_to time);
the sql would be as follows:
select u.id,u.name, ts.day,
group_concat(ts.time_from, ' - ', ts.time_to ORDER BY ts.time_from, ts.time_to)
from users u left outer join time_slot ts on u.id = ts.u_id
group by u.id, u.name, ts.day
order by u.name, ts.day
See fiddle.
I have did with some temp values.
if you want in same way to impliment then it is usefull for you.
copy the code and check here http://phpfiddle.org/
$obj1['id']='1';
$obj1['name']='a1';
$obj1['day']='asdadh';
$obj1['time_from']='1';
$obj1['time_to']='1';
$obj2['id']='2';
$obj2['name']='a2';
$obj2['day']='asdad';
$obj2['time_from']='1';
$obj2['time_to']='1';
$obj3['id']='3';
$obj3['name']='a2';
$obj3['day']='asdad';
$obj3['time_from']='1';
$obj3['time_to']='1';
$arr = Array();
$arr[]=$obj1;
$arr[]=$obj2;
$arr[]=$obj3;
echo '<table class="table_class" border="2">';
echo '<tr>';
echo '<th class="details">id</th>';
echo '<th class="details">name</th>';
echo '<th class="details">day</th>';
echo '<th class="details">start time</th>';
echo '<th class="details">End Time</th>';
echo '</tr>';
foreach($arr as $row)
{
echo '<tr>';
echo '<td class="details">' . $row['id'] . '</td>';
echo '<td class="details">' . $row['name'] . '</td>';
echo '<td class="details">' . $row['day'] . '</td>';
echo '<td class="details">' . $row['time_from'] . '</td>';
echo '<td class="details">' . $row['time_to'] . '</td>';
echo '</tr>';
}
echo '</table>';
echo "<br><br><br><br><br><br><br>";
$dates=Array();
$count=0;
foreach($arr as $id=>$row){
$val = $row['day'];
$key = array_search($val,$dates);
if(is_numeric($key)){
$arr[$key]['day']=$dates[$key].','.$val;
unset($arr[$id]);
}else{
$dates[$count]=$val;
}
$count++;
}
// new table
echo '<table class="table_class" border="2">';
echo '<tr>';
echo '<th class="details">id</th>';
echo '<th class="details">name</th>';
echo '<th class="details">day</th>';
echo '<th class="details">start time</th>';
echo '<th class="details">End Time</th>';
echo '</tr>';
foreach($arr as $row)
{
echo '<tr>';
echo '<td class="details">' . $row['id'] . '</td>';
echo '<td class="details">' . $row['name'] . '</td>';
echo '<td class="details">' . $row['day'] . '</td>';
echo '<td class="details">' . $row['time_from'] . '</td>';
echo '<td class="details">' . $row['time_to'] . '</td>';
echo '</tr>';
}
echo '</table>';
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm very new to PHP and I'm trying display a table like I did for PHP CRUD Grid(Look at image) but for some reason the foreach statement in index.php that I duplicated doesn't work. I get an error looking like this [
Invalid argument supplied for foreach() in Line 79
]1
I've also created a table using the following code
CREATE TABLE IF NOT EXISTS `category` (
`category_category_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`category_name` VARCHAR( 100 ) NOT NULL
) ENGINE = INNODB;
INSERT INTO `category` (`category_category_id`, `category_name`) VALUES
(1,'Drinks')
My data base looks like this on PHPmyadmin
This is the code that I used for the first grid:
include_once 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM product ORDER BY id DESC';
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td>'. $row['category_id'] . '</td>';
echo '<td>'. $row['brand'] . '</td>';
echo '<td>'. $row['name'] . '</td>';
echo '<td>'. $row['barcode'] . '</td>';
echo '<td>'. $row['price'] . '</td>';
and this is the code with the error that I'm trying to duplicate for the second grid
$pdo = Database::connect();
$sql = 'SELECT * FROM category ORDER BY id DESC';
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td>'. $row['category_name'] . '</td>';
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<h3>PHP CRUD Grid</h3>
</div>
<div class="row">
<p>
Create
</p>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Id</th>
<th>CategoryId</th>
<th>Brand</th>
<th>Name</th>
<th>Barcode</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php
include_once 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM product ORDER BY id DESC';
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td>'. $row['category_id'] . '</td>';
echo '<td>'. $row['brand'] . '</td>';
echo '<td>'. $row['name'] . '</td>';
echo '<td>'. $row['barcode'] . '</td>';
echo '<td>'. $row['price'] . '</td>';
echo '<td width=250>';
echo '<a class="btn" href="read.php?id='.$row['id'].'">Read</a>';
echo ' ';
echo '<a class="btn btn-success" href="update.php?id='.$row['id'].'">Update</a>';
echo ' ';
echo '<a class="btn btn-danger" href="delete.php?id='.$row['id'].'">Delete</a>';
echo '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<div class="container">
<div class="row">
<h3>PHP CRUD Grid</h3>
</div>
<div class="row">
<p>
Create
</p>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>CategoryId</th>
<th>Category Name</th>
</tr>
</thead>
<?php
include_once 'database.php';
$pdo = Database::disconnect();
$pdo = Database::connect();
$sql = 'SELECT * FROM category ORDER BY id DESC';
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td>'. $row['category_name'] . '</td>';
echo '<td width=250>';
echo '<a class="btn" href="readCategory.php?id='.$row['id'].'">Read</a>';
echo ' ';
echo '<a class="btn btn-success" href="updateCategory.php?id='.$row['id'].'">Update</a>';
echo ' ';
echo '<a class="btn btn-danger" href="deleteCategory.php?id='.$row['id'].'">Delete</a>';
echo '</td>';
echo '</tr>';
}
Database::disconnect();
?>
</tbody>
</table>
</div>
</div>
</body>
</html>
<!-- <div class="container">
<div class="row">
<h3>PHP CRUD Grid</h3>
</div>
<div class="row">
<p>
Create
</p>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>CategoryId</th>
<th>Catengory Name</th>
</tr>
</thead>
$sql2 = 'SELECT * FROM category ORDER BY id DESC';
foreach ($pdo->query($sql2) as $row) {
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td>'. $row['category_name'] . '</td>';
echo '<td width=250>';
echo '<a class="btn" href="readCategory.php?id='.$row['id'].'">Read</a>';
echo ' ';
echo '<a class="btn btn-success" href="updateCategory.php?id='.$row['id'].'">Update</a>';
echo ' ';
echo '<a class="btn btn-danger" href="deleteCategory.php?id='.$row['id'].'">Delete</a>';
echo '</td>';
echo '</tr>';
</div>
}
Database::disconnect();
?>
</tbody>
</table> -->
I think it's because you don't have an id field in table category ? You have category_category_id
CREATE TABLE IF NOT EXISTS `category` (
`category_category_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`category_name` VARCHAR( 100 ) NOT NULL
) ENGINE = INNODB;
So...
$sql = 'SELECT * FROM category ORDER BY category_category_id DESC';
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['category_category_id'] . '</td>';
echo '<td>'. $row['category_name'] . '</td>';
You're trying to order by "id" when it's actually "category_category_id" in your db table. Change all references from id to category_category_id in the code in your loop too.
I am fetching data from Mysql database and populating them in a table.
However, i cannot seem to make the cell autofit to contents. I have tried width as the property of the table but i cant get it to work
Would really appreciate your help. Thanks
Here's what i have done so far
<div id="page-content-wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<table class="table table-bordered" style="width:100%">
<thead>
<tr>
<th><center>ID</center></th>
<th>Name</th>
<th><center>Email</center></th>
<th>Number</th>
<th>Package</th>
<th>Flexibility</th>
<th >Date</th>
<th>Departuring From</th>
<th>Departure Date</th>
<th>Destination</th>
<th>Arrival Date</th>
<th>Price</th>
<th>Consolidator</th>
</tr>
</thead>
<tbody>
<?php
$query = 'SELECT * FROM queries';
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td>'. $row['name'] . '</td>';
echo '<td>'. $row['email'] . '</td>';
echo '<td>'. $row['contactnumber'] . '</td>';
echo '<td>'. $row['packagedetails'] . '</td>';
echo '<td>'. $row['flexibility'] . '</td>';
echo '<td>'. $row['datetoday'] . '</td>';
echo '<td>'. $row['departure'] . '</td>';
echo '<td>'. $row['dateofdeparture'] . '</td>';
echo '<td>'. $row['destination'] . '</td>';
echo '<td>'. $row['dateofarrival'] . '</td>';
echo '<td>'. $row['price'] . '</td>';
echo '<td>'. $row['vendor'] . '</td>';
echo '<td width=250>';
echo '<a class="btn btn-success" href="readquery.php?id='.$row['id'].'">Read</a>';
echo ' ';
echo '<a class="btn btn-success" href="updatequery.php?id='.$row['id'].'">Update</a>';
echo ' ';
echo '<a class="btn btn-danger" href="deletequery.php?id='.$row['id'].'">Delete</a>';
echo '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
I never see you echo </tr> or </td>. It would be helpful to give us an output of the HTML being generated by your while loop.
The problem was not with my table. To make it work, i increased the width of my container in which my table was inside, and it worked