Trying to complete a tutorial and am trying to retrofit the styling.
I know the whole sheet of code may be anything from current,
but its helping me break down the parts and understand the whole.
That said, I am trying to insert the mysql data into divs instead of a table.
(for the thought of more responsive CSS design possibilities)
Any help would be awesome.
<?php
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
// Run a select query to get my letest 4 items
// Connect to the MySQL database
include "storescripts/connect_to_mysql.php";
$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 4");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$dynamicList .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">
<tr>
<td width="17%" valign="top"><img style="border:#666 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="77" height="102" border="1" /></td>
<td width="83%" valign="top">' . $product_name . '<br />
$' . $price . '<br />
View Product Details</td>
</tr>
</table>';
}
} else {
$dynamicList = "We have no products listed in our store yet";
}
mysql_close();
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The Shop</title>
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen" />
</head>
<body>
<div align="center" id="mainWrapper">
<?php include_once("template_header.php");?>
<div id="pageContent">
<table width="85%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="14%" valign="top"><h3></h3>
<p><br />
<br />
</p></td>
<td width="66%" align="center">
<h3>The Shop </h3>
<p><?php echo $dynamicList; ?><br />
</p>
<p><br />
</p></td>
<td width="20%" valign="top"><h3></h3>
<p></p></td>
</tr>
</table>
</div>
<?php include_once("template_footer.php");?>
</div>
</body>
</html>
I'm assuming this should get you headed in the right direction (Ignore the ... accordingly)
<?php
...
$dynamicList .= '
<div>
<img style="border:#666 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="77" height="102" border="1" />
</div>
<div>' . $product_name . '<br />
$' . $price . '<br />
View Product Details
</div>';
...
?>
...
<div id="pageContent">
<h3>The Shop </h3>
<?php echo $dynamicList; ?>
</div>
Related
Im trying to create a php cart that compute a correct input in the cart when i try to put only one product the total amount is correct but when in input another item the total amount is different
Is there something wrong with the code??
I've got a correct cart total when i add only 1 product in the cart
but when i add another when the cart total is different
below is the code i have so far?
<?php
session_start();
include("admin/php/connect_to_mysql.php");
include("admin/php/myFunctions.php");
if(!empty($_GET['prodid'])){
$pid = $_GET['prodid'];
$wasFound = false;
$i = 0;
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
$_SESSION["cart_array"]=array(0=>array("productID"=>$pid,"quantity"=>1));
}else{
foreach($_SESSION["cart_array"] as $each_product){
$i++;
while(list($key,$value)=each($each_product)){
if($key=="productID" && $value==$pid){
array_splice($_SESSION["cart_array"],$i-1,1,array(array("productID"=>$pid,"quantity"=>$each_product ['quantity']+1)));
$wasFound=true;
}
}
}
if($wasFound==false){
array_push($_SESSION["cart_array"],array("productID"=>$pid,"quantity"=>1));
}
}
header("location:shoppingcart.php");
exit();
}
//-------------------------------------------------------------------------------------------------
$submit = false;
if(isset($_POST['btnUpdate'])){
$submit = $_POST['btnUpdate'];
}
echo $submit;
if($submit == "Update"){
$x = 0;
$i=0;
//echo $_POST['txtQuan2'];
//echo $_POST['txtHoldProdId0'];
foreach($_SESSION["cart_array"] as $each_product){
$i++;
$quantity = $_POST['txtQuan'.$x];
$prodStock = $_POST['txtHoldQuan'.$x];
$prodAdjustId = $_POST['txtHoldProdId'.$x++];
if($quantity<1){ $quantity = 1; }
if($quantity>$prodStock){ $quantity = $prodStock; }
while(list($key,$value)=each($each_product)){
array_splice($_SESSION["cart_array"],$i-1,1,array(array("productID"=>$prodAdjustId,"quantity"=>$quantity)));
}
}
}
//-------------------------------------------------------------------------------------------------
if(!empty($_GET['cid']) || isset($_GET['cid'])){
$removeKey = $_GET['cid'];
if(count($_SESSION["cart_array"])<=1){
unset($_SESSION["cart_array"]);
}else{
unset($_SESSION["cart_array"]["$removeKey"]);
sort($_SESSION["cart_array"]);
}
}
//-------------------------------------------------------------------------------------------------
$cartTitle = "";
$cartOutput = "";
$cartTotal = "";
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
$cartOutput="<h2 align='center'> Your shopping cart is empty </h2>";
}else{
$x = 0;
$cartTitle .= '<form name="shoppingcart_form" action="shoppingcart.php" method="post" /><table width="700px" cellspacing="0" cellpadding="5">
<tr bgcolor="#CCCCCC">
<th width="220" align="left">Image </th>
<th width="140" align="left">Name </th>
<th width="100" align="center">Quantity </th>
<th width="60" align="center">Stock </th>
<th width="60" align="right">Price </th>
<th width="60" align="right">Total </th>
<th width="90"> </th></tr>';
$i = 0;
foreach($_SESSION["cart_array"] as $each_product){
$product_id = $each_product['productID'];
$sql=mysql_query("select * from tblproduct where prod_id='$product_id' limit 1");
while($row=mysql_fetch_array($sql)){
$prodNo = $row["prod_no"];
$prodID = $row["prod_id"];
$prodName = $row["prod_name"];
$prodPrice = $row["prod_price"];
$prodQuan = $row["prod_quan"];
}
$pricetotal=$prodPrice*$each_product['quantity'];
$cartTotal= number_format($pricetotal+$cartTotal,2);
$cartOutput .= '<tr><td><img style="border: 2px solid;" src="images/product/'.$prodNo.'.jpg" width="150" height="120" /></td>
<td>'.$prodName.'</td>
<td align="center"><input type="hidden" name="txtHoldProdId'.$i.'" value="'.$prodID.'" /><input name="txtQuan'.$i.'" type="text" value="'.$each_product['quantity'].'" style="width: 40px; text-align: center" /> </td>
<td align="center"><input type="hidden" name="txtHoldQuan'.$i.'" value="'.$prodQuan.'" /> '.$prodQuan .' pcs</td>
<td align="right">Php '.$prodPrice.'</td>
<td align="right">Php '.$pricetotal.'</td>
<td align="center"> <img src="images/remove_x.gif" alt="remove" /><br />Remove </td></tr>';
}
$_SESSION['checkoutCartTotal'] = $cartTotal;
$cartOutput .= '<tr>
<td colspan="3" align="right" height="40px">Have you modified your basket? Please click here to <input class="btn_upd" type="submit" name="btnUpdate" value="Update" /> </td>
<td align="right" style="background:#ccc; font-weight:bold"> Total: </td>
<td colspan="2" align="left" style="background:#ccc; font-weight:bold;">Php '.$cartTotal.' </td>
<td style="background:#ccc; font-weight:bold"> </td>
</tr>
</table>
<div style="float:right; width: 215px; margin-top: 20px;">
<div class="checkout">Proceed to Checkout</div>
</div></form>';
}
?>
<?php
if(!isset($_SESSION['name'])){
header("location:login.php");
exit();
}
?>
<!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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Manila Ensembles - Cart</title>
<link href="css/slider.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="css/ddsmoothmenu.css" />
<link rel="stylesheet" type="text/css" href="css/styles.css" />
<script language="javascript" type="text/javascript">
function clearText(field)
{
if (field.defaultValue == field.value) field.value = '';
else if (field.value == '') field.value = field.defaultValue;
}
</script>
</head>
<body id="subpage">
<div id="main_wrapper">
<?php include("header.php"); ?>
<!-- END of header -->
<div id="main_menu" class="ddsmoothmenu">
<ul>
<li>Home</li>
<li>Products</li>
<li><a class="selected" href="shoppingcart.php">Cart</a></li>
<li>Checkout</li>
<li>About</li>
</ul>
<br style="clear: left" />
</div> <!-- end of menu -->
<div class="cleaner h20"></div>
<div id="main_top"></div>
<div id="main">
<?php include("sidebar.php");?>
<!-- END of sidebar -->
<div id="content">
<?php echo $cartTitle; ?>
<?php echo $cartOutput; ?>
</div> <!-- end of content -->
<div class="cleaner"></div>
</div> <!-- END of main -->
<?php include("footer.php"); ?>
<!-- END of footer -->
</div>
<script type='text/javascript' src='js/logging.js'></script>
</body>
</html>
This question already has an answer here:
Echo inside an element attribute?
(1 answer)
Closed 10 years ago.
I have a php file which will show a list of images/products dynamically from the mysql database! This bit works just fine.
but I am trying to but the command in the iframe so I can create a slideshow affect using iframe!
but when I put the iframe around the command it doesn't work for some reason! it will open a 404 page on my 000web hosting server!!
here is my code:
<?php
include "config/connect_to_mysql.php";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 10");
$i = 0;
// Establish the output variable
$dyn_table = '<table border="0" cellpadding="6">';
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
if ($i % 5 == 0) { // if $i is divisible by our target number (in this case "5")
$dyn_table .= '<tr><td><div style="position:relative" class="shadow tr" id="products_holder"><div style="position:absolute;left:-10px;top:-12px;">
<img src="images/best.png" width="97" height="94" />
</div><img style=" margin:5px; border:#ccc 1px solid" src="inventory_images/' . $id . '.jpg" width="150" height="160"/></div></td>';
} else {
$dyn_table .= '<td><div style="position:relative" class="shadow tr" id="products_holder"><div style="position:absolute;left:-10px;top:-12px;">
<img src="images/best.png" width="97" height="94" />
</div><img style=" margin:5px; border:#ccc 1px solid" src="inventory_images/' . $id . '.jpg" width="150" height="160"/></div></div></td>';
}
$i++;
}
$dyn_table .= '</tr></table>';
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/main.css">
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
<script src="scripts/modernizr-2.6.1.min.js"></script>
<title>site.com</title>
<style type="text/css">
</style>
</head>
<body>
<div align="center" id="wrapper">
<?php include_once("temps/header.php"); ?>
<div id="content">
<div id="apDiv1">
<h2>Top pros</h2></div>
<table width="100%" border="0" cellspacing="0" cellpadding="17">
<tr>
<td width="2%" height="20"> </td>
<td width="98%" height="20"><p><?php echo $dyn_table; ?></p>
</tr>
</table>
<table align="center" style=" table-layout:fixed; background-image:url(images/secondSection.png);" height="82" width="100%" border="0" cellspacing="10" cellpadding="10">
<tr>
<td width="2%" height="31"> </td>
<td style="vertical-align:top" align="center" width="28%"><div id="apDiv555" class="top_btns">
<h6 class="shadow"> More Products</h6>
</div></td>
<td style="vertical-align:top" align="center" width="70%"><div id="apDiv55" class="top_btns">
<h6><span class="shadow"> Search By Clothing | Search By Retailers</span></h6>
</div></td>
</tr>
</table>
<table style="background-image: url(images/tbl_bg.png); background-repeat: repeat-x;" height="350px" width="100%" border="0" cellspacing="12" cellpadding="0">
<tr>
<td width="3%"></td>
<td width="97%"><iframe src="<?php echo $dyn_table; ?>" height="250" width="100%" ></iframe></td>
</tr>
</table>
</table>
<table height="100" width="100%" border="0" cellspacing="17" cellpadding="17">
<tr>
<td> </td>
</tr>
</table>
</div>
</div>
<?php include_once ("temps/footer.php"); ?>
</body>
</html>
any help would be grately appreciated as I am almost done with this project.
Th
Just loose the iFrame and echo the $dyn_table inside the table you already have?
Your are not aware of what an iframe means, maybe :
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/main.css">
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
<script src="scripts/modernizr-2.6.1.min.js"></script>
<title>site.com</title>
<style type="text/css">
</style>
</head>
<body>
<div align="center" id="wrapper">
<?php include_once("temps/header.php"); ?>
<div id="content">
<div id="apDiv1">
<h2>Top pros</h2></div>
<table width="100%" border="0" cellspacing="0" cellpadding="17">
<tr>
<td width="2%" height="20"> </td>
<td width="98%" height="20"><p><?php echo $dyn_table; ?></p>
</tr>
</table>
<table align="center" style=" table-layout:fixed; background-image:url(images/secondSection.png);" height="82" width="100%" border="0" cellspacing="10" cellpadding="10">
<tr>
<td width="2%" height="31"> </td>
<td style="vertical-align:top" align="center" width="28%"><div id="apDiv555" class="top_btns">
<h6 class="shadow"> More Products</h6>
</div></td>
<td style="vertical-align:top" align="center" width="70%"><div id="apDiv55" class="top_btns">
<h6><span class="shadow"> Search By Clothing | Search By Retailers</span></h6>
</div></td>
</tr>
</table>
<table style="background-image: url(images/tbl_bg.png); background-repeat: repeat-x;" height="350px" width="100%" border="0" cellspacing="12" cellpadding="0">
<tr>
<td width="3%"></td>
<td width="97%"><iframe src="my_iframe.php" height="250" width="100%" ></iframe></td>
</tr>
</table>
</table>
<table height="100" width="100%" border="0" cellspacing="17" cellpadding="17">
<tr>
<td> </td>
</tr>
</table>
</div>
</div>
<?php include_once ("temps/footer.php"); ?>
</body>
</html>
// Context on the my_iframe.php
<?php
include "config/connect_to_mysql.php";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 10");
$i = 0;
// Establish the output variable
$dyn_table = '<table border="0" cellpadding="6">';
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
if ($i % 5 == 0) { // if $i is divisible by our target number (in this case "5")
$dyn_table .= '<tr><td><div style="position:relative" class="shadow tr" id="products_holder"><div style="position:absolute;left:-10px;top:-12px;">
<img src="images/best.png" width="97" height="94" />
</div><img style=" margin:5px; border:#ccc 1px solid" src="inventory_images/' . $id . '.jpg" width="150" height="160"/></div></td>';
} else {
$dyn_table .= '<td><div style="position:relative" class="shadow tr" id="products_holder"><div style="position:absolute;left:-10px;top:-12px;">
<img src="images/best.png" width="97" height="94" />
</div><img style=" margin:5px; border:#ccc 1px solid" src="inventory_images/' . $id . '.jpg" width="150" height="160"/></div></div></td>';
}
$i++;
}
$dyn_table .= '</tr></table>';
echo $dyn_table;
?>
An iframe it's a totally different page, so you should use 2 pages....just like a propose, one for the html and one for the iframe....i hope it helps.
PS: I change the a href ;)
Saludos ;)
Trying to make a simple site where users can have different account levels.
For example, user with account type 'a' and 'b' will see a Link/button at the top which says basic and the users with account type 'c' will see a Link/button which says full member.
I have a code which will almost do what I need and it will save the users in different A/B/C categories in the database.
I just need to know what I have to do to be able to show different links to different account type as I explained above?
also, I know the code bellow is not the greatest codes but just need to achieve the results I am after for now with what I have.
<?php
session_start(); // Must start session first thing
// See if they are a logged in member by checking Session data
$toplinks = "";
if (isset($_SESSION['id'])) {
// Put stored session variables into local php variable
$userid = $_SESSION['id'];
$username = $_SESSION['username'];
$toplinks = '' . $username . ' •
Account •
Log Out';
} else {
echo 'Please log in to access your account';
exit();
}
?>
<?php
//Connect to the database through our include
include_once "scripts/connect_to_mysql.php";
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM members WHERE id='$userid'");
while($row = mysql_fetch_array($sql)){
$country = $row["country"];
$state = $row["state"];
$city = $row["city"];
$accounttype = $row["accounttype"];
$bio = $row["bio"];
}
// Give different options or display depending on which user type it is
if ($accounttype == "a") {
$userOptions = "You get options for Expert User";
} else if ($accounttype == "b") {
$userOptions = "You get options for Expert User";
} else if ($accounttype == "c") {
$userOptions = "You get options for Expert User";
} else {
$userOptions = "You get options for Super User";
}
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Member Account</title>
<link rel="stylesheet" type="text/css" href="style/style.css">
<style type="text/css">
<!--
body {
margin: 0px;
background-image: url(imgs/bgnoise.png);
background-repeat: repeat;
}
-->
</style>
</head>
<body>
<?php include_once("temps/template_header.php");?>
<table style="background-image: url(imgs/horizontal_nav_bg.jpg);" repeat="x" width="100%" border="0" cellpadding="0">
<tr>
<td height="94" ><table style=" margin-left:20px; " width="734" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="156"><p><img src="imgs/info.png" width="35" height="35" border="0" "/>Edit Information</p></td>
<td width="179"><img src="imgs/very.png" width="35" height="35" border="0" />Verify Your Identity</td>
<td width="126"><img src="imgs/profile.png" width="35" height="35" border="0" />View Profile</td>
<td width="138"><img src="imgs/basket.png" width="35" height="35" />Buy Bitcoin</td>
<td width="135"><img src="imgs/user.png" width="35" height="35" alt="user" /><?php echo "$username"; ?></td>
</tr>
</table> <h1> </h1></td>
</tr>
</table>
<table width="700" align="right" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><h3></h3></td>
</tr>
</table>
<table width=950" cellpadding="3" cellspacing="3" style="line-height:1.5em;">
<tr>
<td width="118" valign="top" bgcolor="#E4E4E4"><h6>YOUR ACCOUNT<br />
Edit Information <br />
Verify Your Identity<br />
View Profile</h6>
<h6><?php echo "$bio"; ?><br />
</h6></td>
<!-- See the more advanced member system tutorial to see how to place default placeholder pic until member uploads one -->
<td width="160" valign="top"><div align="center"><img src="memberFiles/<?php echo "$userid"; ?>/pic1.jpg" alt="Ad" width="150" /></div></td>
<td width="201" valign="top">
Country: <?php echo "$country"; ?> <br />
State: <?php echo "$state"; ?><br />
City: <?php echo "$city"; ?>
<table width="50" border="0" cellspacing="0" cellpadding="0">
<tr>
</tr>
</table> <br />
</td>
<td width="430" margin="left" style="margin-right:0px;" rowspan="2" valign="top"><div id="veri"><img src="imgs/verifynote.png" width="430" height="600" /> </div></td>
</tr>
<tr>
<td colspan="3" valign="top"><br />
<br />
<br />
<div style="overflow:hidden; width:100%px;">
<iframe width="565" height="400" scrolling="no" frameBorder="0"
src="http://bitcoin.clarkmoody.com/widget/chart/"
style="width:728px; height:270px; border:none; margin-left:-60px;"/>
</div>
</td>
</tr>
</table>
<?php include_once("temps/template_footer.php");?>
</body>
</html>`
Please be gentle as I am a newbie in PHP and just learning the curves.
Thanks
I am not sure if I got this correctly but if you want according to the user's group to display different links just do:
if ($accounttype == "a") {
echo 'Page for user a';
}
else if ($accounttype == "b") {
echo 'Page for user b';
} else if ($accounttype == "c") {
echo 'Page for user c';
} else {
echo 'Page for others';
}
Also I suggest you to use mysqli. It is pretty similar to mysql. http://php.net/manual/en/book.mysqli.php (Myqsl is being removed)
I'm creating a simple website for a business to display their products. On the index page I display the 3 newest products. What I need to do now is have a category page people can go to to view products in a certain category. I tried adapting the code from my index page but the products aren't showing up on my category.php.
I have a product_list.php with the different categories, when one is selected I'm trying to load category.php with the products from the category that was selected.
What I have:
product_list.php
<?php
include "storescripts/connect_to_mysql.php";
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
</style>
<meta name="Description" content="Lightweight aluminum boat docks, lifts, and accessories" />
<meta name="Keywords" content="Aluminum boat dock ladder lift water wheels" />
<script src="scripts/swfobject_modified.js" type="text/javascript"></script>
</head>
<title><?php echo $product_name; ?></title>
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen"/>
</style>
<body>
<div align="center" id="mainWrapper">
<?php include_once("template_header.php");?>
<table width="100%" border="0" cellspacing="0" cellpadding="15">
<tr>
<td valign="top" align="center"><table width="100%" border="1">
<tr>
<td align="center"><p>Aluminum Docks</p>
<p><img src="inventory_images/aluminum docks/PN99002-6_24x4_PKG_LG.jpg" width="100" height="64" alt="24X4" /></p></td> // There's a field in my DB called categories, and 'docks' is the value assigned to the products
<td align="center"><p>Floating Docks</p>
<p><img src="inventory_images/floating dock/100225279.jpg" width="100" height="60" alt="Floating Dock" /></p></td>
<td align="center"><p>Frame Docks</p>
<p><img src="inventory_images/frame dock/frameDock.jpg" width="100" height="64" alt="Frame Dock" /></p></td>
<td align="center"><p>Pipe Docks</p>
<p><img src="inventory_images/pipe dock/PN99002_16X4_SECTION_LG.jpg" width="100" height="64" alt="Pipe Dock" /></p></td>
</tr>
<tr>
<td align="center"><p>Boat Lifts</p>
<p><img src="inventory_images/boat lifts/GM1060_LG.jpg" width="100" height="64" alt="Boat Lift" /></p></td>
<td align="center"><p>Boat Lift Accessories</p>
<p><img src="inventory_images/boat lift acceessories/canopy_lg (1).png" width="100" height="64" alt="Boat Lift Accessory" /></p></td>
<td align="center"><p>Rollers & Caddies</p>
<p><img src="inventory_images/rollers and caddies/caddy270 (1).jpg" width="100" height="64" alt="Caddy" /></p></td>
<td align="center"><p>Accessories</p>
<p><img src="inventory_images/accessorries/2step_LG.png" width="100" height="64" alt="Accessory" /></p></td>
</tr>
</table>
</table>
<p> </p>
<?php include_once("template_footer.php");?>
</div>
</body>
</html>
in category.php I'm trying to create a variable called $dynamicList with all the products that have "dock" as a category. But when I echo out $dynamicList nothing is rendered.
<?php
// Run a select query to get my letest 6 items
// Connect to the MySQL database
include "storescripts/connect_to_mysql.php";
$category=$_GET['category'];
$sql = mysql_query("SELECT * FROM products WHERE category='$category' LIMIT 6");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$dynamicList .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">
<tr>
<td width="17%" valign="top"><img style="border:#666 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="100" height="64" border="1" /></td>
<td width="83%" valign="top">' . $product_name . '<br />
$' . $price . '<br />
View Product Details</td>
</tr>
</table>';
}
} else {
$dynamicList = "We have no products listed in our store yet";
}
mysql_close();
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
</style>
<meta name="Description" content="Lightweight aluminum boat docks, lifts, and accessories" />
<meta name="Keywords" content="Aluminum boat dock ladder lift water wheels" />
<script src="scripts/swfobject_modified.js" type="text/javascript"></script>
</head>
<title><?php echo $category; ?></title>
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen"/>
</style>
<body>
<div align="center" id="mainWrapper">
<?php include_once("template_header.php");?>
<table width="100%">
<tr>
<td valign="top"><p><?php echo $dynamicList;?><br />
</p>
<p> </p>
<p> </p> <h2> </h2></td>
</tr>
</table>
<p> </p>
<?php include_once("template_footer.php");?>
</div>
</body>
</html>
EDIT: $dynamicList is nested in a condition statement. Just write
$dynamicList = '';
before your query
What is the current output on the page? Are the # of rows returned in the query = 0?
You should also sanitize your $category input using mysql_real_escape_string()
WHERE category ='".mysql_real_escape_string($category)."' ...
You must sanitize first the strings using this function:
public static function real_escape($string)
{
if (null != $string) {
$string = (get_magic_quotes_gpc()) ? $string : addslashes($string);
return mysql_real_escape_string($string);
}
return $string;
}
Or better use third party script like phpDataMapper
$sql = mysql_query("SELECT * FROM products WHERE category='$category' LIMIT 6");
should be
$sql = mysql_query("SELECT * FROM products WHERE category='".$category."' LIMIT 6");
This is the only error I can discover for now
I've created a product catalogue with category - subcategory - individual product page. I pass a $category to the individual product page. This page lists various details about the item, including which category the item belongs to. Since the $category variable is already set, I thought Return to previous category would be enough to return it to the subcategory page but it doesn't, part of the category.php that handles this looks like this.
EDIT: Full code
product_list.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
include "storescripts/connect_to_mysql.php";
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
</style>
<meta name="Description" content="Lightweight aluminum boat docks, lifts, and accessories" />
<meta name="Keywords" content="Aluminum boat dock ladder lift water wheels" />
<script src="scripts/swfobject_modified.js" type="text/javascript"></script>
</head>
<title>Products</title>
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen"/>
</style>
<body>
<div align="center" id="mainWrapper">
<?php include_once("template_header.php");?>
<table width="100%" border="0" cellspacing="0" cellpadding="15">
<tr>
<td valign="top" align="center"><table width="100%" border="0" align="center">
<tr>
<td align="center" valign="middle"><p>Aluminum Docks</p>
<p><img src="inventory_images/aluminum docks/PN99002-6_24x4_PKG_LG.jpg" width="25%" height="64"/></p></td>
<td align="center"><p>Floating Docks</p>
<p><img src="inventory_images/floating dock/100225279.jpg" width="25%" height="64"/></p></td>
<td align="center"><p>Frame Docks</p>
<p><img src="inventory_images/frame dock/frameDock.jpg" width="25%" height="64" alt="Frame Dock" /></a></p></td>
<td align="center"><p>Pipe Docks</p>
<p><img src="inventory_images/pipe dock/PN99002_16X4_SECTION_LG.jpg" width="25%" height="64" alt="Pipe Dock" /></p></td>
</tr>
<tr>
<td align="center"><p>Boat Lifts</p>
<p><img src="inventory_images/boat lifts/GM1060_LG.jpg" width="25%" height="64" alt="Boat Lift" /></p></td>
<td align="center"><p>Boat Lift Accessories</p>
<p><img src="inventory_images/boat lift acceessories/canopy_lg (1).png" width="25%" height="64" alt="Boat Lift Accessory" /></p></td>
<td align="center"><p>Rollers & Caddies</p>
<p><img src="inventory_images/rollers and caddies/caddy270 (1).jpg"width="25%" height="64" alt="Caddy" /></p></td>
<td align="center"><p>Accessories</p>
<p><img src="inventory_images/accessorries/2step_LG.png" width="25%" height="64" alt="Accessory" /></p></td>
</tr>
</table>
</table>
<p> </p>
<?php include_once("template_footer.php");?>
</div>
</body>
</html>
**category.php**
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
// Run a select query to get my letest 6 items
// Connect to the MySQL database
include "storescripts/connect_to_mysql.php";
$category=$_GET['category'];
$sql = mysql_query("SELECT * FROM products WHERE category='$category' LIMIT 50");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
$dynamicList="";
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$dynamicList .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">
<tr>
<td width="17%" valign="top"><img style="border:#666 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="100" height="64" border="1" /></td>
<td width="83%" valign="top">' . $product_name . '<br />
$' . $price . '<br />
View Product Details</td>
</tr>
</table>';
}
} else {
$dynamicList = "We have no products listed in our store yet";
}
mysql_close();
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
</style>
<meta name="Description" content="Lightweight aluminum boat docks, lifts, and accessories" />
<meta name="Keywords" content="Aluminum boat dock ladder lift water wheels" />
<script src="scripts/swfobject_modified.js" type="text/javascript"></script>
</head>
<title><?php echo $category; ?></title>
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen"/>
</style>
<body>
<div align="center" id="mainWrapper">
<?php include_once("template_header.php");?>
<div align="left">
<p><?php echo $dynamicList;?>
</p>
<div id="target_product"></div>
<p> </p>
</div>
<?php include_once("template_footer.php");?>
</div>
</body>
</html>
**product.php**
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
include "storescripts/connect_to_mysql.php";
session_start();
//Check to see if the URL variable is set and exists in the database
if(isset($_GET['id'])){
$id=preg_replace('#[^0-9]#i','',$_GET['id']);
// Use this variable to see if the id exists, if yesthen get the product
// details, if no then exit the script and give message why.
$sql = mysql_query("SELECT * FROM products WHERE id ='$id' LIMIT 1");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
// Get all product details
while($row = mysql_fetch_array($sql)){
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
$quantity = $row["quantity_in_stock"];
$category = $row["category"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
}
}else{
echo "That item does not exist.";
exit();
}
}else{
echo "Data to render this page is missing.";
exit();
}
mysql_close();
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
</style>
<meta name="Description" content="Lightweight aluminum boat docks, lifts, and accessories" />
<meta name="Keywords" content="Aluminum boat dock ladder lift water wheels" />
<script src="scripts/swfobject_modified.js" type="text/javascript"></script>
</head>
<title><?php echo $product_name; ?></title>
<link rel="stylesheet" href="style/style.css" type="text/css" media="screen"/>
</style>
<body>
<div align="center" id="mainWrapper">
<?php include_once("template_header.php");?>
<table width="100%" border="0" cellspacing="0" cellpadding="15">
<tr>
<td width="20%" valign="top" ><img src="inventory_images/<?php echo $id;?>.jpg" alt="<?php echo $product_name; ?>" />
<td width="80%" valign="top"> <h3><?php echo $product_name; ?></h3>
<p>$<?php echo $price; ?><br/>
<?php echo $category; ?>
<br/>
<?php echo $details; ?><br/>
<?php echo $quantity;?>
<p>Return to previous category</p>
</table>
<p> </p>
<?php include_once("template_footer.php");?>
</div>
</body>
</html>
If you want all data that was containing your previous page, use SESSION superglobal for this purpose.