How to create table in foreach - php

This is my php code
<?php
include_once 'conectDB.php';
$sql ='SELECT CHECKLISTS.USER_ID, CHECKLISTS.ADMIN_ID, PROGRAMS.PROGRAM_NAME
FROM CHECKLISTS
LEFT JOIN COMPUTERS
ON CHECKLISTS.USER_ID = COMPUTERS.COMPUTER_NAME
LEFT JOIN ADMINS
ON CHECKLISTS.ADMIN_ID = ADMINS.USER_ID
LEFT JOIN CHECKLIST_PROGRAMS
ON CHECKLISTS.ID = CHECKLIST_PROGRAMS.CHECKLIST_ID
LEFT JOIN PROGRAMS
ON PROGRAMS.ID = CHECKLIST_PROGRAMS.PROGRAM_ID
';
$result = $connection->query($sql);
$checklists = array();
if ($result->num_rows > 0){
while($row = $result->fetch_assoc()) {
$checklists [] = $row;
}
} else {
echo '0 results';
}
$connection->close();
?>
And this is HTML CODE
in this foreach I don't know how to query and create table :(
<html>
<head>
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
<title>C.I.G. Check List</title></head>
<body>
<div class="container">
<div class="row" style="margin-top:10px;">
<div class="col-md-offset-2 col-md-8">
<div class="row" style="margin-top:20px;">
<div class="col-md-4"><h3>C.I.Gruop Checklist</h3></div>
<div class="col-md-6" style="margin-top:19px; font-size:20px;">New Item</div>
</div></div>
</div>
<div class="col-md-2"></div>
<div class="row" style="margin-top:20px;">
<div class="col-md-offset-1 col-md-9">
<!-- ส่วนแสดงผล -->
<?php
foreach ($checklists as $checklist) {
print_r($checklist);
}
?>
</div>
</div>
</div
</body>
</html>

You need to do like this:-
<table>
<?php
foreach ($checklists as $checklist) {
echo '<tr><td>'.$checklist.'</td></tr>';
}
?>
</table>

Creating table in foreach is simple. Try this:
<table>
<thead>
<tr>
<th>Check List</th>
</tr>
</thead>
<tbody>
<?php
foreach ($checklists as $checklist) {
?>
<tr>
<?php
echo "<td>" . $checklist . "</td>";
?>
</tr>
<?php
}
?>
</tbody>
</table>

One option can be:
<table>
<? foreach ($checklists as $checklist) { ?>
<tr>
<td>
<? echo $checklist; ?>
</td>
</tr>
<? } ?>
</table>
Within the loop, everything you write is repeated as many times as the checklist items

Related

PHP Output (PDO & CRUD)

I need help with my PHP script.
I have 3 Tables in my Database (its German so sorry if you dont understand ^^):
-firma (in english company)
-produkt (in english product)
-firmaprodukt (in english companyproduct)
If I click on a company, the product image should be output (from the "Produkt" database) and the appropriate URL (from the "firmaprodukt").
So far I've managed that when you click on a company, the ID is displayed (example: "localhost/TESTING//index?1=") but I'm struggling with the output.
That's my index.php script for the output:
<div class="col-xl-6">
<div id="login_content">
<div class="scrollP">
<?php
$fpresults = $fpCrud->getPicUrl($FirmenID);
?>
<div class="col-12 d-flex justify-content-center heading-div">
<h3> <?= $result['firmenname']?> </h3>
</div>
<table border="0">
<tbody>
<?php
foreach ($fpresults as $fpresult){
?>
<tr>
<div class="box customer-box" data-parent="#login_card" id="project_reg_box">
<img src="dashboard/TESTING/<?= $fpresult['ProduktLogo']?>" height="100%" >
</div>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
And that's in my crud.php:
public function getPicUrl($FirmenID) {
$stmt = $this->conn->query(
"SELECT fp.FirmenID, f.firmenname, p.ProduktLogo, fp.url
FROM firma f
JOIN firmaprodukt fp ON fp.FirmenID = f.FirmenID
JOIN produkt p ON fp.ProduktID = p.ProduktID
WHERE fp.FirmenID = :FirmenID");
$stmt->execute(array(':FirmenID' => $FirmenID));
$data = $stmt->fetch();
return $data;
}
In my formprocess.php I don't have anything for that.
My solution:
index.php
<?php
if (isset($_GET['firmaprodukte'])) {
$FirmenID = (int) $_GET['firmaprodukte'];
$firmaprodukte = $fpCrud->getPicUrl($_GET['firmaprodukte']);
$firmenresults = $firmaCrud->getFirma($_GET['firmaprodukte']);
?>
<div class="col-xl-6">
<div class="scrollP">
<div id="login_content">
<div id="login_card" class="container">
<?php
$fpresults = $fpCrud->getPicUrl($FirmenID);
?>
<div class="col-12 d-flex justify-content-center heading-div">
<h3> <?= $firmenresults['firmenname']?> </h3>
</div>
<table border="0">
<tbody>
<?php
foreach ($fpresults as $fpresult){
?>
<tr>
<div class="box customer-box" data-parent="#login_card" id="project_reg_box">
<img src="dashboard/TESTING/<?= $fpresult['ProduktLogo']?>" height="80vh" max-width="200vh">
</div>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php } ?>
Crud.php
public function getPicUrl($FirmenID) {
$stmt = $this->conn->prepare("SELECT fp.FirmenID, f.firmenname, p.ProduktLogo, fp.url FROM firma f JOIN firmaprodukt fp ON fp.FirmenID = f.FirmenID JOIN produkt p ON fp.ProduktID = p.ProduktID WHERE fp.FirmenID = :FirmenID");
$stmt->execute(array(':FirmenID' => $FirmenID));
$data = $stmt->fetchAll();
return $data;
}
formprocess.php
if(isset($_POST['firmaprodukte'])) {
$FirmenID = $_POST['FirmenID'];
if($crud->getPicUrl($FirmenID)) {
header('location: FirmaProdukt.php');
exit;
} else{
header('location: FirmaProdukt.php');
exit;
}
}

PHP Displaying data from database into a table when a link is clicked

Just to start off, I am new to php so i might have missed something obvious so please bear with me.
I have hyperlinks (planes, ships trains etc) and when I click the hyperlink "planes" i want all of the planes records to be displayed in a table. When I click a different vehicle i want it to refresh the table with new data.
The problem is, when i click the link "trains" it does not refresh the table and display relevant data, it keeps the same data. How do i tell php when i click the link "planes" i want to display all the records with that productLine.
Thanks for any help
As you can see in the pic, i clicked train but it still displays vintage cars
Here is my code:
<?php
require_once('dbconfig.php');
//get productLine
if (!isset($productLine)) {
$productLine = filter_input(INPUT_GET, 'productLine', FILTER_VALIDATE_INT);
if ($productLine == null || $productLine == FALSE) {
$productLine = 'Trains';
}
}
//get all product lines
$query = 'SELECT * FROM productlines';
$statement = $db->prepare($query);
$statement->execute();
$productLines = $statement->fetchAll();
$statement->closeCursor();
//Get products for product line
$queryProducts = 'SELECT * FROM products WHERE productLine = :productLine ORDER BY productCode';
$statement1 = $db->prepare($queryProducts);
$statement1->bindValue(':productLine', $productLine);
$statement1->execute();
$products = $statement1->fetchAll();
$statement1->closeCursor();
?>
<!DOCTYPE html>
<html>
<head>
<title>Classic Models Online</title>
<link rel="stylesheet" type="text/css" href="main.css" />
</head>
<body>
<header><h1>ClassicModels Online</h1>
<p>Classic models for all automobile enthusiasts</p>
</header>
<main>
<h1>Classic Models Product List</h1>
<aside>
<!--Display list of product lines-->
<h2>Product Lines</h2>
<nav>
<ul>
<?php foreach ($productLines as $productLine) : ?>
<li>
<a href=".?productLine=<?php echo $productLine['productLine']; ?>">
<?php echo $productLine['productLine']; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</nav>
</aside>
<br>
<section>
<!--Display a table of products for product line-->
<h2><?php echo $productLine['productLine']; ?></h2>
<table>
<tr>
<th>Code</th>
<th>Name</th>
<th>Scale</th>
<th>Price</th>
<th>Total Sold</th>
<th> </th>
</tr>
<?php foreach ($products as $product) :?>
<tr>
<td> <?php echo $product['productCode']; ?> </td>
<td> <?php echo $product['productName']; ?> </td>
<td> <?php echo $product['productScale']; ?> </td>
<td> <?php echo $product['MSRP']; ?> </td>
<td> <?php echo $product['quantityInStock']; ?> </td>
<td> <form action="update_product.php" method="post">
<input type="hidden" name="productCode" value="<?php echo $product['productCode']; ?>">
<input type ="hidden" name="productLine" value="<?php echo $product['productLine']; ?>">
<input type="submit" value="Update">
</form> </td>
</tr>
<?php endforeach; ?>
</table>
<p>Add Product</p>
</section>
</main>
<footer>
<p>© <?php echo date("Y"); ?> Classic Models Online.</p>
</footer>
</body>
</html>
You need to get the productLine variable from the query string you have in the url. You need to add something like
if(isset($_GET['productLine'])){
$productLine = $_GET['productLine'];
}
Just do if(isset($_GET["productLine"])){//do your stuff}

data insert into database automatically in php

I have a problem which is the user when write in my comments form is insert successfully but when I refresh the page it will insert the last comments again , I read the solution in this link how to stop data automatically insert into database in php
but does not work for me
this is my codes I would appreciate for your help :)
file viewhospital.php contain include comments.php file
--look at the bottom of the codes--
<?php
include ('header.php');
if(!isset($_GET['hospital_id'])){
echo '<div class="alert alert-danger" role="alert"><b>You should choose hospital before opening this page!</b></div>';
include ('footer.php');
die();
}
include ('setting.php');
$sql = 'select * from hospital where hid = '. $_GET['hospital_id'];
$result = $conn->query($sql) or die(mysql_error($conn));
$hospital = null;
if ($result->num_rows > 0) {
$hospital = $result->fetch_assoc();
} else {
die('Could not find hospital!');
}
$sql = 'select * from doctor where hospital_id = '. $_GET['hospital_id'];
$doctor_result = $conn->query($sql) or die(mysql_error($conn));
$conn->close();
?>
<div class="row">
<div class="col-md-6">
<p class="text-center">
<img src="<?php echo $hospital['image']; ?>" class="img-thumbnail" style="height: 400px;">
</p>
</div>
<div class="col-md-6">
<p class="text-center">
<img class="img-thumbnail" src="https://maps.googleapis.com/maps/api/staticmap?center=<?php echo $hospital['location']; ?>&zoom=13&size=400x400&maptype=roadmap&markers=color:blue%7Clabel:S%7C<?php echo $hospital['location']; ?>&key=AIzaSyD59nHXpZgqZwjJvsAcPe2CYcIEWoaQ9yY" style="height: 400px;">
</p>
</div>
</div>
<div class="row">
<div class="col-md-12">
<h1 class="page-header">
<?php echo $hospital['name']; ?>
</h1>
<p>
<?php echo $hospital['description']; ?>
</p>
<p>
Address: <?php echo $hospital['address']; ?>
</p>
<p>
Phone: <?php echo $hospital['phone']; ?>
</p>
<p>
Go To Hospital
</p>
<p>
Online Appointment
</p>
</div>
</div>
<!--<div class="row">
<div class="col-md-12 text-center">
<div class="btn-group" role="group" aria-label="...">
<a type="button" class="btn btn-info">Edit</a>
<a type="button" class="btn btn-danger">Remove</a>
<a type="button" class="btn btn-primary" href="doctor_form.php?hospital_id=<?php echo $hospital['hid']; ?>">Add Doctor</a>
</div>
</div>
</div>-->
<div class="row">
<div class="col-md-12">
<table class="table table-striped">
<caption>Doctors:</caption>
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Field</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
if ($doctor_result->num_rows > 0) {
while($row = $doctor_result->fetch_assoc()) {
?>
<tr>
<th scope="row">
<?php echo $row['did'];?>
</th>
<td>
<?php echo $row['name'];?>
</td>
<td>
<?php echo $row['field'];?>
</td>
<td>View</td>
</tr>
<?php
}
}else{
?>
<tr>
<th scope="row"></th>
<td>No doctors found</td>
<td></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
<?php
include ('comments.php');
include ('footer.php');
?>
the comments.php file
<?PHP
# comments PHP code
date_default_timezone_set('Asia/Riyadh');
function setComments (){
if (isset($_POST['submitComments'])){
include('setting.php');
//$uid = $_POST['uid'];
$date = $_POST['date'];
$message = $_POST['message'];
$sql = "INSERT INTO comments ( date, message) VALUE ( '$date', '$message')";
$result = mysqli_query($conn,$sql);
}
}
function getComments (){
if (isset($_POST['submitComments'])){
include('setting.php');
$sql = "SELECT * FROM comments";
$result = mysqli_query($conn,$sql);
while ($row = $result->fetch_assoc()){
echo "<div class='comments-box'>";
echo $row['date']."<br>";
echo nl2br($row['message'])."<br><br>";
echo "</div>";
}
}
}
echo "
<form action='".setComments ()."' method='POST'>
<input type='hidden' name='uid' value=''>
<input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
<textarea name='message' class='form-control' rows='3'></textarea>
<br>
<button type='submit' name='submitComments' class='btn btn-primary'>Comments</button>
</form>
<br><br>
";
getComments ();
?>
When you refresh in the browser, you send the last request again. That request was the POST of the form. So the user (browser) is telling the code to insert another comment.
Generally this is handled by redirecting after posting a form, rather than re-displaying the form again. Move all of your logic for (and only for) inserting the new content to its own PHP file (something like addComment.php) and have the form post to that file. Then in that file ensure that there is no actual output except perhaps to display an error message if something goes wrong?) and just a redirect back to the page:
header("Location: viewhospital.php");
This will instruct the browser in the response to make a new GET request for viewhospital.php. So if the user reloads the browser, all they're doing is repeating that GET request.

how to send data to php file itself via a hyperlink

I am trying to make a shopping cart with php. I wanted to send id to a php code below in the file. I tried to do it with php_self. And I get these errors.
*Undefined index: movie_id in C:\xampp\htdocs\ITA_WEBSITE\shopping_cart.php on line 60
*mysqli_fetch_object() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\ITA_WEBSITE\shopping_cart.php on line 61
*Undefined index: cart in C:\xampp\htdocs\ITA_WEBSITE\shopping_cart.php on line 84
Here's the code
<?php
session_start();
include('dbconfig.php');
$result = mysqli_query($con,"SELECT * FROM movie_details");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Movie Store</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
<div class="container-fluid" id="maincontent">
<div class="row">
<?php while($product = mysqli_fetch_object($result)) { ?>
<div class="col-md-4 col-xs-12">
<div class="thumbnail customsthumbs">
<img src=<?php echo $product->image_path;?> alt=<?php echo $product->image_path;?>>
<div class="caption">
<h3 class="customh"><?php echo $product->movie_title;?></h3>
<p id="price">Price : $<?php echo $product->price;?></p>
<p style="text-align:center"><a href=<?php $_SERVER['PHP_SELF'].'?movie_id='?><?php echo $product->movie_id?> class="btn btn-primary custombtn" role="button">
<span class="glyphicon glyphicon-shopping-cart" aria-hidden="true"></span>Add To Cart</a>
</p>
</div>
</div>
</div>
<?php } ?>
</div>
<?php
include('dbconfig.php');
include('movie_products.php');
$result = mysqli_query($con, 'SELECT * FROM movie_details WHERE movie_id = '.$_GET['movie_id']);
$product = mysqli_fetch_object($result);
if(isset($_GET['movie_id'])){
$item = new Item();
$item->movie_id = $product->movie_id;
$item->movie_title = $product->movie_title;
$item->price = $product->price;
$item->quantity = 1;
$_SESSION['shopping_cart'][] = $item;
}
?>
<div class="container-fluid">
<div class="row">
<div id="cart">
<h3 style="text-align: center;font-size: 40px;padding-top: 5px;">Cart</h3>
<div id="display_cart">
<table border="1" id="display_cart_table">
<tr>
<th>Movie Title</th>
<th>Price</th>
<th>Action</th>
</tr>
<?php
$cart = unserialize(serialize($_SESSION['cart']));
for($i=0; $i<count($cart); $i++){
?>
<tr>
<td><?php echo $cart[$i]->movie_title;?></td>
<td><?php echo $cart[$i]->price;?></td>
<td></td>
</tr>
<?php } ?>
</table>
</div>
</div>
</div>
</div>
</body>
</html>
movie_products.php file
<?php
class Item{
var $movie_id;
var $movie_title;
var $price;
var $quantity;
}
?>
Note: I tried to do this by following a tutorial
Need some help !!
Look at the following statement,
<p style="text-align:center"><a href=<?php $_SERVER['PHP_SELF'].'?movie_id='?> ...
^ see here
You forgot to use echo here. It should be,
<p style="text-align:center"><a href=<?php echo $_SERVER['PHP_SELF'].'?movie_id='?>...
Also, wrap your code block inside an if block, like this:
// your code
if(isset($_GET['movie_id'])){
include('dbconfig.php');
include('movie_products.php');
$result = mysqli_query($con, 'SELECT * FROM movie_details WHERE movie_id = '.$_GET['movie_id']);
$product = mysqli_fetch_object($result);
if(isset($_GET['movie_id'])){
$item = new Item();
$item->movie_id = $product->movie_id;
$item->movie_title = $product->movie_title;
$item->price = $product->price;
$item->quantity = 1;
$_SESSION['shopping_cart'][] = $item;
}
?>
<div class="container-fluid">
<div class="row">
<div id="cart">
<h3 style="text-align: center;font-size: 40px;padding-top: 5px;">Cart</h3>
<div id="display_cart">
<table border="1" id="display_cart_table">
<tr>
<th>Movie Title</th>
<th>Price</th>
<th>Action</th>
</tr>
<?php
$cart = unserialize(serialize($_SESSION['cart']));
for($i=0; $i<count($cart); $i++){
?>
<tr>
<td><?php echo $cart[$i]->movie_title;?></td>
<td><?php echo $cart[$i]->price;?></td>
<td></td>
</tr>
<?php } ?>
</table>
</div>
</div>
</div>
</div>
<?php
}

Pulling content from MySql Database with PHP and display in 3 columns

I'm pulling content from MySQL database and I would like to display such content in 3 columns. I have managed to achieve it, but not all the content gets pulled. Even when I change it to 4 columns, it displays more content but still not all of them.
There must be a way to check how much content it is and display it correctly. Any help would be much appreciated.
This is what I have so far:
(I'm supposed to display 52 items from database, but only 38 or so get displayed)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div id="wrapper">
<div id="page">
<div id="content">
<div id="report_content">
<div id="top_bar">
<h1 class="report_title"></h1>
<h2 class="report_subtitle"></h2>
<div id="criteria"></div>
</div>
<div>
<div>
<div id="t1" class="results resultList">
<?php if ($row_detail_vendors) { $temp_name='' ; ?>
<table>
<?php $cols=4 ; do { ?>
<tr>
<?php for($i=1;$i<=$cols;$i++){ // All the rows will have $cols columns even if // the records are less than $cols $row_detail_vendors=m ysql_fetch_assoc($detail_vendors) ?>
<td> <a href="partner_info.php?idu=<?php echo $row_detail_vendors['shortcut']; ?>">
<?php echo ucfirst(strtoupper($row_detail_vendors['hotel_name'])); ?></a>
<br> <span><?php echo $row_detail_vendors['city']; ?>, <?php echo ucfirst($row_detail_vendors['country']); ?></span>
<br/>
<br/>
</a>
</td>
<?php } ?>
</tr>
</div>
<!--marketing-->
<?php } while ($row_detail_vendors=m ysql_fetch_assoc($detail_vendors)); ?>
<?php }?>
</div>
</div>
</div>
<!--marketing partners-->
</div>
<!--content-->
</div>
<!--page-->
</body>
Would advise:
<div id="t1" class="results resultList">
<table>
<?php
$cols=4;
$i = 1;
while($row_detail_vendors=mysql_fetch_assoc($detail_vendors)) {
if($i % $cols == 1){
echo "<tr>"; // start column wrapper
} ?>
<td><?php echo strtoupper($row_detail_vendors['hotel_name'])); ?>
<br /><span><?php echo $row_detail_vendors['city']; ?>, <?php echo ucfirst($row_detail_vendors['country']); ?></span>
<br/>
<br/>
</td>
<?php
if( $i % $cols == 0){
echo "</tr>"; // End column wrapper
}
$i++;
} ?>
</table>
</div>
<!--marketing-->

Categories