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.
Related
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>`
So, I'm connected to a database and I'm pulling data from two different tables within same database. I'm running foreach to generate rows, but the table renders not the way I want. I want the 2nd foreach to create a 2nd column to the right of the first one and all those dynamic rows to go downward.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/db.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row text-center">
<h3>Schedule of Ms. Kim & Ms. Morly</h3>
</div>
<?php
include 'database.php';
$pdo = Database::connect();
//Start of table Kim - ID=10
$sql = 'SELECT * FROM kim ORDER BY id';
$q = $pdo->prepare($sql);
$q->execute(array($schd_tut_id));
$data = $q->fetch(PDO::FETCH_ASSOC);
$sql2 = 'SELECT tut_img FROM tbl_tutors_profile WHERE tut_profile_id=10 ';
$q2 = $pdo->prepare($sql2);
$q2->execute(array($tut_profile_id));
$data2 = $q2->fetch(PDO::FETCH_ASSOC);
$imgkim = $data2['tut_img'];
//End of Table Kim
//Start of table Morly - ID=16
$sql3 = 'SELECT * FROM morly ORDER BY id';
$q3 = $pdo->prepare($sql3);
$q3->execute(array($schd_tut_id));
$data3 = $q3->fetch(PDO::FETCH_ASSOC);
$sql4 = 'SELECT tut_img FROM tbl_tutors_profile WHERE tut_profile_id=16 ';
$q4 = $pdo->prepare($sql4);
$q4->execute(array($tut_profile_id));
$data4 = $q4->fetch(PDO::FETCH_ASSOC);
$imgmorly = $data4['tut_img'];
//End of table Morly
?>
<table class="table table-striped table-bordered">
<thead>
<th style="width:20px;">Time</th>
<th><img class="img-responsive" src="<?php echo $imgkim ?>"</th>
<th><img class="img-responsive" src="<?php echo $imgmorly ?>"</th>
<th><img class="img-responsive" src="<?php echo $imgmorly ?>"</th>
</thead>
<tbody>
<?php
//tutor kim
foreach ($pdo->query($sql) as $row ) {
echo '<tr>';
//echo '<td hidden>'. $row['id'] . '</td>';
//echo '<td hidden>'. $row['schd_tut_id'] . '</td>';
echo '<td>'. $row['time'] . '</td>';
if($row['status']=='available')
{ echo '<td>'. 'available';}
else {echo '<td>'. $row['status'] . '</td>';}
}
//echo '</tr>';
//tutor morly
foreach ($pdo->query($sql3) as $row ) {
//echo '<tr>';
//echo '<td hidden>'. $row['id'] . '</td>';
//echo '<td hidden>'. $row['schd_tut_id'] . '</td>';
echo '<td>'. $row['time'] . '</td>';
if($row['status']=='available')
{ echo '<td>'. 'available';}
else {echo '<td>'. $row['status'] . '</td>';}
echo '</tr>';
}
Database::disconnect();
?>
</tbody>
</table>
</div>
</div> <!-- /container -->
</body>
</html>
Something like this should work, might need a bit of fiddeling.
Basically just store the values in an array and then print the array.
<?php
$row1 = [];
$row2 = [];
//table 1
foreach ($pdo->query($sql) as $row ) {
$row1[] = $row['status'];
}
//table 2
foreach ($pdo->query($sql2) as $row ) {
$row2[] = $row['status'];
}
echo "<table>";
foreach ($row1 as $key => $row) {
echo "<tr><td>" . $row[$key] . "</td><td>" . $row2[$key] . "</td></tr>";
}
echo "</table>";
Database::disconnect();
?>
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
this is a code block to load data from database into a table structure in the page. the select statement is used to load data. but in the page it display the following error message mysqli error message
<div class="container">
<div class="row">
<h3>Company Overview </h3>
</div>
<div class="row">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Company</th>
<th>Overview</th>
<th>Address</th>
<th>Contact_Number</th>
<th>Created By</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php include('sessions.php');
$login_session;
$sql = 'SELECT * FROM company_overview where created_by LIKE "' . $login_session . '" '; ?>
<?php $result = mysqli_query($conn, $sql); ?>
<?php while ($row = mysqli_fetch_array($result)) {
?>
<?php echo '<tr>'; ?>
<?php echo '<td>' . $row['company_name'] . '</td>'; ?>
<?php echo '<td>' . $row['company_overview'] . '</td>'; ?>
<?php echo '<td>' . $row['address'] . '</td>'; ?>
<?php echo '<td>' . $row['contact_no'] . '</td>'; ?>
<?php echo '<td>' . $row['created_by'] . '</td>'; ?>
<?php echo '<td width=250>'; ?>
<?php echo '<a class="btn btn-success" href="update_p.php">Update</a>'; ?>
<?php echo ' '; ?>
<?php echo '<a class="btn btn-danger" href="delete_promotion.php?id=' . $row['id'] . '">Delete</a>'; ?>
<?php echo '</td>'; ?>
<?php echo '</tr>'; ?>
<?php include('o_sessions.php'); ?>
<?php } ?>
<?php //mysqli_close($conn); ?>
</tbody>
</table>
</div>
</div> <!-- /container -->
add appropriate column name after WHERE statement and add % in like:
$sql = 'SELECT * FROM company_overview where column_name LIKE "%'.$login_session.'%" ';
Use mysqli_num_rows() to check your query contain result or not.
Use column name with WHERE and use %..% with LIKE.
Your code contains extra open close <?php ?> please correct it.
Please check comments and change your code. I have added mysqli_num_rows() and prevent your code from error. Other things you can do it by yourself.
<?php
include('sessions.php');
$login_session;
$sql = 'SELECT * FROM company_overview where id LIKE "%'.$login_session.'%" ';
$result=mysqli_query($conn,$sql);
if(mysqli_num_rows($result) > 0)
{
while($row=mysqli_fetch_array($result))
{
?>
<?php echo '<tr>';?>
<?php echo '<td>'. $row['company_name'] . '</td>';?>
<?php echo '<td>'. $row['company_overview'] . '</td>';?>
<?php echo '<td>'. $row['address'] . '</td>';?>
<?php echo '<td>'. $row['contact_no'] . '</td>';?>
<?php echo '<td>'. $row['created_by'] . '</td>';?>
<?php echo '<td width=250>';?>
<?php echo '<a class="btn btn-success" href="update_p.php">Update</a>';?>
<?php echo ' ';?>
<?php echo '<a class="btn btn-danger" href="delete_promotion.php?id='.$row['id'].'">Delete</a>';?>
<?php echo '</td>';?>
<?php echo '</tr>';?>
<?php include('o_sessions.php');?>
<?php } }?>
<?php //mysqli_close($conn);?>
</tbody>
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
I'm having some display problems here.
I have a "backend.php" file where I ask for two inputs.
These inputs are processed by "Addproducts.php" file and this file redirects to backend.php.
Backend.php also shows the current records in the database.
Here's the code for backend.php
<html>
<head>
<title>Inventory - Backend</title>
</head>
<body>
<form action="addproducts.php" method="post">
<table>
<tr>
<td>Product Name : </td>
<td><input type="text" name="pname"/></td>
</tr>
<tr>
<td>Product Quantity : </td>
<td><input type="text" name="productq"/></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><input type="submit" name="Add Product"/></td>
</tr>
</table>
</form>
<h2>Current Products</h2>
<?php
$db = mysql_connect('127.0.0.1', 'root', '') or die ('Unable to Connect.Check your connection parameters');
mysql_select_db('stock_inventory', $db) or die (mysql_error($db));
$query = 'SELECT * FROM PRODUCTS';
$result = mysql_query($query, $db) or die (mysql_error($db));
echo '<table>';
echo '<tr>';
echo '<th>Product ID </th>';
echo '<th>Producr Name </th>';
echo '<th>Product Stock </th>';
echo '</tr>';
while($row = mysql_fetch_assoc($result))
{
if(mysql_num_rows($result) > 0)
{
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
echo '<br/>';
echo '</table>';
}
else
{
echo "No products in the database";
}
}
?>
</body>
</html>
It displays something like this :-
Product ID Producr Name Product Stock
1 NewProduct 1
2HTC One5
3Samsung10
4Sony10
You see?
Only the first product is aligned, the rest are not.
How do I make them all align ?
Thanks.
The reason is you are closing your table tag within the loop, move it outside the loop like follows:
while($row = mysql_fetch_assoc($result))
{
if(mysql_num_rows($result) > 0)
{
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
}
else
{
echo "No products in the database";
}
}
echo '<br/>';
echo '</table>';
Update: A better fix (see Barmar's comment below):
if (empty(mysql_num_row($result))) {
echo "<tr><td colspan='3'>No products in the database</td></tr>";
} else {
while($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
}
}
echo '</table>';
Also start looking into using mysqli(http://php.net/manual/en/book.mysqli.php) or PDO (http://php.net/manual/en/book.pdo.php), mysql_ is deprecated!
I would suggest removing the
<br/>
from within your table. I believe that putting markup like line breaks inside of table markup will break the layout.