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
}
Related
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}
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
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-->
I am getting this error:
Catchable fatal error: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, null given, called in /home/emscompl/public_html/pdf_email/dompdf/include/table_frame_decorator.cls.php on line 304 and defined in /home/emscompl/public_html/pdf_email/dompdf/include/frame.cls.php on line 726
Here is the dompdf code.
<?php
/*$dir = dirname(__FILE__);
require_once($dir.'/dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF();
$dompdf->load_html_file("http://ems-complete.com/pdf_email/patient-report.php");
$dompdf->render();
$dompdf->stream("file.pdf");*/
$course = $_REQUEST['course'];
$dir = dirname(__FILE__);
require_once($dir.'/dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF();
$dompdf->load_html(file_get_contents("http://ems-complete.com/courseroster/courseroster.php?course=".$course));
$dompdf->render();
// $dompdf->set_paper(2412, 3222);
$dompdf->stream("courseroster.pdf");
// exit(0);
?>
Here is the PHP HTML
<html>
<head>
<link type="text/css" media="screen" rel="stylesheet" href="stylesheets/courseroster.css" />
<link type="text/css" media="print" rel="stylesheet" href="stylesheets/print.css" />
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">
function PrintDivData(crtlid)
{
var ctrlcontent = document.getElementById(crtlid);
var printscreen = window.open('','','left=1,top=1,width=1,height=1,toolbar=0,scrollbars=0,status=0');
printscreen.document.write(ctrlcontent.innerHTML);
printscreen.document.close();
printscreen.focus();
printscreen.print();
printscreen.close();
}
</script>
<input type="button" name="Print" value="Print" onClick="javascript:PrintDivData('container');" Runat="Server" />
<?php
$course = $_REQUEST['course'];?>
<?php
$connect = mysql_connect('localhost','db','pass') or die(mysql_error());
$selectdb = mysql_select_db('emscompl_joom1283',$connect);
?>
<?php
$qry = "SELECT * FROM courses
WHERE id = $course";
$result = mysql_query($qry);
$row = mysql_fetch_array($result);
$qry2 = "SELECT s.fullname, cs.* FROM registered_users AS `s`
LEFT OUTER JOIN course_students AS cs ON s.userid = cs.userid
WHERE cs.courseid = $course
ORDER BY s.fullname";
$result2 = mysql_query($qry2);
echo "<a href=http://www.ems-complete.com/pdf_email/form_courseroster.php?course=".
$course.">Display PDF</a>";?>
<div class="container" id ="container">
<div class="header"><?php echo $row['coursename'];?> Roster</div>
<div class="coursestatus"><p>Course Status</p>
<p><?php echo $row['status'];?></p>
</div>
<div class="startdate"><p>Start Date</p>
<p><?php echo $row['startdate'];?></p>
</div>
<div class="enddate"><p>End Date</p>
<p><?php echo $row['enddate'];?></p>
</div>
<div class="dayofweek"><p>Class Held on:</p>
<p><?php echo $row['dayofweek'];?></p>
</div>
<div class="leadinstructor"><p>Lead Instructor</p>
<p><?php echo $row['leadinstructor'];?></p>
</div>
<div class="assistantinstructor"><p>Assistant Instructors</p></div>
<div class="enrolledstudents">
<p>Enrolled Sutdents</p>
<table width="760" border="0">
<tr>
<th width="223">Student Name</th>
<th width="102">Phone</th>
<th width="90">Email</th>
<th width="100">Status</th>
<th width="220">Document Report</th>
</tr>
<tr>
<?php while ($row2 = mysql_fetch_array($result2)) { ?>
<td><?php echo $row2['fullname'];?></td>
<td><?php echo $row2[''];?></td>
<td><?php echo $row2[''];?></td>
<td><?php if ($row2['studentstatus']== 0 ){
echo "Interested";
}
else if ($row2['studentstatus']== 1 ){
echo "Entrance Process";
}
else if ($row2['studentstatus']== 2 ){
echo "Incomplete Entrance Process";
}
else if ($row2['studentstatus']== 3 ){
echo "Enrolled";
}
else if ($row2['studentstatus']== 4 ){
echo "Course Completed";
}
else if ($row2['studentstatus']== 5 ){
echo "NREMT Skills Complete";
}
else if ($row2['studentstatus']== 6 ){
echo "NREMT Cognitive Complete";
}
else if ($row2['studentstatus']== 7 ){
echo "State Certified";
}?></td>
<td><?php echo "Document Report"
;?>
</td>
</tr>
<?php } ?>
</table>
<p> </p>
</div>
<div class="footer">
<p> </p>
</div>
</div>
</body>
</html>
In your HTML-generating script you have an opening TR outside the while loop that looks like it should be inside the loop (the closing tag is inside the loop). This will cause an invalid document structure, and apparently one which dompdf is unable to parse.
FYI, the first thing to do if dompdf won't parse a document is to run it through a validator.
I've tried to run a php file on xampp which is installed in Ubuntu and WinXP. Both result in error, but it is success when tried to run on xampplite in WinXP. Any special configuration is need to be set on the full xampp version? The php code is as the following:
<?php
require_once('database.php');
// Get category ID
if(!isset($category_id)) {
$category_id = $_GET['category_id'];
if (!isset($category_id)) {
$category_id = 1;
}
}
// Get name for current category
$query = "SELECT * FROM categories
WHERE categoryID = $category_id";
$category = $db->query($query);
$category = $category->fetch();
$category_name = $category['categoryName'];
// Get all categories
$query = 'SELECT * FROM categories
ORDER BY categoryID';
$categories = $db->query($query);
// Get products for selected category
$query = "SELECT * FROM products
WHERE categoryID = $category_id
ORDER BY productID";
$products = $db->query($query);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- the head section -->
<head>
<title>My Guitar Shop</title>
<link rel="stylesheet" type="text/css" href="main.css" />
</head>
<!-- the body section -->
<body>
<div id="page">
<div id="header">
<h1>Product Manager</h1>
</div>
<div id="main">
<h1>Product List</h1>
<div id="sidebar">
<!-- display a list of categories -->
<h2>Categories</h2>
<ul class="nav">
<?php foreach ($categories as $category) : ?>
<li>
<a href="?category_id=<?php echo $category['categoryID']; ?>">
<?php echo $category['categoryName']; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
<div id="content">
<!-- display a table of products -->
<h2><?php echo $category_name; ?></h2>
<table>
<tr>
<th>Code</th>
<th>Name</th>
<th class="right">Price</th>
<th> </th>
</tr>
<?php foreach ($products as $product) : ?>
<tr>
<td><?php echo $product['productCode']; ?></td>
<td><?php echo $product['productName']; ?></td>
<td class="right"><?php echo $product['listPrice']; ?></td>
<td><form action="delete_product.php" method="post"
id="delete_product_form">
<input type="hidden" name="product_id"
value="<?php echo $product['productID']; ?>" />
<input type="hidden" name="category_id"
value="<?php echo $product['categoryID']; ?>" />
<input type="submit" value="Delete" />
</form></td>
</tr>
<?php endforeach; ?>
</table>
<p>Add Product</p>
</div>
</div>
<div id="footer">
<p>© <?php echo date("Y"); ?> My Guitar Shop, Inc.</p>
</div>
</div><!-- end page -->
</body>
</html>
The database has been created without any problem. Thanks