Hi all
I will try keep this short, I am working on a project doing a shopping cart, I have got a nice bit of code to help me alone from Ying Zhang (ying@zippydesign.com), which I am heavly modifing, but I have run into a problem which I ave no clue how to go about solving (newbie here)
I have a list of products in my database that I amble to pull out onto a webpage ok, but what I want to be able to do is add a row at the top with column headers that are clickable which will then put the list in order.
This is what I use at the moment but the problem is it does not put it in any order, I want to be able to click price and it put highest or lowest price first, same goes for Category which I would like it to list in alphbetical order.
<table cellpadding="3">
<tr>
<th>Action</th>
<th>Category</th>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th>Image</th>
<th>Info</th>
</tr>
<? while ($r = db_fetch_object($qid)) { ?>
<tr>
<td class="normal">[ <a title="Delete <? pv($r->name) ?>" href="<?=$ME?>?mode=del&id=<? pv($r->id) ?>">X</a>
]</td>
<td class="normal"><? pv($r->category) ?></td>
<td class="normal"><a title="Edit this category" href="<?=$ME?>?mode=edit&id=<? pv($r->id) ?>"><? pv($r->name) ?></a></td>
<td class="normal"><? pv($r->description) ?></td>
<td class="normal"><? pv($r->price) ?></td>
<td class="normal"><? pv($r->image) ?></td>
<td class="normal"><? pv($r->info) ?></td>
</tr>
<? } ?>
</table>
Here is the FUNCTIONS for the list:
global $CFG, $ME;
/* set default values for the reset of the fields */
$frm["categories"] = array($category_id);
$frm["newmode"] = "insert";
$frm["name"] = "";
$frm["description"] = "";
$frm["price"] = "";
$frm["qty"] = "";
$frm["on_special"] = "";
$frm["image"] = "";
$frm["info"] = "";
$frm["submit_caption"] = "Add Product";
/* build the categories listbox options, preselect the top item */
build_category_tree($category_options, $frm["categories"]);
include("templates/product_form.php");
}
function print_edit_product_form($id) {
/ print a product form so we can edit the selected product /
global $CFG, $ME;
/* load up the information for the product */
$qid = db_query("
SELECT name, description, price, on_special, image, info
FROM products
WHERE id = $id
");
$frm = db_fetch_array($qid);
/* load up the categories for the product */
$qid = db_query("
SELECT category_id
FROM products_categories
WHERE product_id = $id
");
$frm["categories"] = array();
while ($cat = db_fetch_object($qid)) {
$frm["categories"][] = $cat->category_id;
}
/* set values for the form */
$frm["newmode"] = "update";
$frm["submit_caption"] = "Save Changes";
/* build the categories listbox options, preselect the selected item */
build_category_tree($category_options, $frm["categories"]);
include("templates/product_form.php");
}
function delete_product($id) {
/ delete the product specified by $id, we have to delete the product and then
the appropriate entries from the products_categories table. this should be
wrapped inside a transaction, unfortunately MySQL currently does not support
transactions. */
global $CFG, $ME;
/* load up the information for the product */
$qid = db_query("
SELECT name
FROM products
WHERE id = $id
");
$prod = db_fetch_object($qid);
/* delete this product */
$qid = db_query("
DELETE FROM products
WHERE id = $id
");
/* delete this product from the products_categories table */
$qid = db_query("
DELETE FROM products_categories
WHERE product_id = $id
");
include("templates/product_deleted.php");
}
function insert_product($id, $frm) {
/ add a new subproduct under the parent $id. all the fields that we want are
going to in the variable $frm */
global $CFG, $ME;
$on_special = checked($frm["on_special"]);
/* add the product into the products table */
$qid = db_query("
INSERT INTO products (name, description, price, on_special, image, info)
VALUES ('$frm[name]', '$frm[description]', '$frm[price]', '$on_special', '$frm[image]', '$frm[info]')
");
/* get the product id that was just created */
$product_id = db_insert_id();
/* add this product under the specified categories */
for ($i = 0; $i < count($frm["categories"]); $i++) {
$qid = db_query("
INSERT INTO products_categories (category_id, product_id)
VALUES ('{$frm["categories"][$i]}', '$product_id')
");
}
}
function update_product($id, $frm) {
/ update the product $id with new values. all the fields that we want are
going to in the variable $frm */
global $CFG, $ME;
checked($frm["on_special"]);
/* update the products table with the new information */
$qid = db_query("
UPDATE products SET
name = '$frm[name]'
,description = '$frm[description]'
,price = '$frm[price]'
,on_special = '$frm[on_special]'
,image = '$frm[image]'
,info = '$frm[info]'
WHERE id = $id
");
/* delete all the categories this product was associated with */
$qid = db_query("
DELETE FROM products_categories
WHERE product_id = $id
");
/* add associations for all the categories this product belongs to, if
* no categories were selected, we will make it belong to the top
* category */
if (count($frm["categories"]) == 0) {
$frm["categories"][] = 0;
}
for ($i = 0; $i < count($frm["categories"]); $i++) {
$qid = db_query("
INSERT INTO products_categories (category_id, product_id)
VALUES ('{$frm["categories"][$i]}', '$id')
");
}
}
function print_product_list() {
/ read all the categories from the database and print them into a table. we
will use a template to display the listings to keep this main script clean */
global $CFG, $ME;
$qid = db_query("
SELECT p.id, p.name, p.description, p.price, p.image, p.info, c.name
AS category
FROM products p, products_categories pc, categories c
WHERE p.id = pc.product_id
AND c.id = pc.category_id
");
include("templates/product_list.php");
I don't know if it is my datebase setting or what, as I said earlier I don't have a clue, so if someone could be good enough to help me out or point me in the right direction that would be great.
Thanks in advance :-)
BTW, running Apache, MySQL, PHP4 etc etc on Win 2000 (runs sweet as a dream)