Jump to content
Larry Ullman's Book Forums

Specific Coffees On Index Page


Recommended Posts

I bought your E-Commerce and your PHP 6 And MysQL 5, both of which are excellent books. I want to tweak the coffees site by making the index page listing in the Welcome to Our Online Coffee House box the specific coffees only without sales items. All specific coffees products to be listed at the same time in a format similar to Figure 8.4. What should I change in the Index, Home, Browse, and List_coffees pages?

  • Upvote 1
Link to comment
Share on other sites

Here is what you do.

 

1. Set your MySQL tables:

 

For example, see my table below that I used in this site http://www.heartpond...store/index.php

Here is the link to an image of my table setup in MySQL http://www.heartpond...tore/table.html

You will see two images. The first is an image of the products table fields. The next is an image of the id, product_name, price, details, category, subcategory, date_added.

 

As you can see, you can put whatever name for category and subcategory in the values. You can put in Tim's Coffee for category and put in black coffee for subcategory. Remember the category and subcategory names for the html form you will have on your products page with the drop down list.

 

Once you have your table setup, that is category and a subcategory, you will need to think of what info you want to be displayed on the first page. For example, you could have a drop down list of

 

Tim's Coffee

Megan's Coffee

Ryan's Coffee

 

Because the idea is to have the user choose a category. Now based on what category they choose, the next page the user will be taken to is the page of the subcategories of that category the user chose. Hope this makes sense.

 

So how do we display this info. Well, pull the info from the MySQL table. Look at the website example http://www.heartpond...store/index.php again. We will use the Ford and Dodge example. Know that Trucks and Cars are the main categories.

Basically, set a variable equal to nothing. Then select all from your table that houses the category and subcategory fields which is products in this case. Next, count the rows returned. Next do a mysql_fetch_array and make an array of the values set to a variable which is then set to a variable $dynamicList ..

 

 

 

include "storescripts/connect_to_mysql.php";

$dynamicList = "";

$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC 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"];

$price = formatMoney($price);

$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));

$dynamicList .= '<table width="95%" border="0" cellspacing="0" cellpadding="6">

<tr>

<td width="17%" valign="top"><a href="product.php?id=' . $id . '"><img style="border:#666 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="127" height="102" border="1" /></a></td>

<td width="83%" valign="top">' . $product_name . '<br />

$' . $price . '<br />

<a href="product.php?id=' . $id . '" style="color:#99BB00">View Product Details</a></td>

</tr>

</table>';

}

} else {

$dynamicList = "We have no products listed in our store yet";

}

mysql_close();

?>

 

 

 

Now we have all of our info pulled and ready to use dynamically. Now we need to do something with the data from the tables.

 

Create a form on the page that you want your products shown. This page is my index.php page which displays all the products, so you would have to adjust your select all from whatever in your MySQL query.

 

 

Select Make:

<form method="post" action="" >

<select name="category">

<option value="">select</option>

<option value="Trucks">Trucks</option>

<option value="Cars">Cars</option>

</select>

<br/>

<input type="submit" value="change" name="Submit"/>

</form>

 

Use the jquery to do the magic below:

 

 

 

<script type="text/javascript">

$(document).ready(function() {

$(':submit').bind("click", function(e){

e.preventDefault();

var make = $("select").val();

window.location = "/develop php-online store/" + make.toLowerCase() + ".php";

});

});

</script>

 

So this will get you started. Oh, don't forget to echo your $dynamiclist in the html body to show your mysql queried data.

 

So on the index page, you will have the categories to choose from. Next make a page of your option value such as trucks.php. On trucks.php, it will have select name category. Hope you get the picture. Then create a page ford.php and it will be the subcategories.

 

You can see how the pages will add up after a while with a big inventory. For right now I am learning and this is the only way I know how to accomplish what you are asking. Please everyone, suggest better ways of accomplishing the same task with less pages and coding.

Link to comment
Share on other sites

 Share

×
×
  • Create New...