PDA

View Full Version : PHP Shopping Cart Script Help



ca99uk
28-02-11, 21:49
Hello everyone,

I am currently working on the script taken from www.php (http://www.%3Cb%3Ephp%3C/b%3E)webcommerce.com I am only a beginner and have been using it in a little project for an online game just to help my mates be able to trade items and such.

I have managed to get almost everything sorted apart from one thing. The function which gets the categories for a drop down select box:




/*
Generate combo box options containing the categories we have.
if $catId is set then that category is selected
*/
function buildCategoryOptions($catId = 0)
{
$sql = "SELECT cat_id, cat_parent_id, cat_name
FROM tbl_category
ORDER BY cat_id";
$result = dbQuery($sql) or die('Cannot get Product. ' . mysql_error());

$categories = array();
while($row = dbFetchArray($result)) {
list($id, $parentId, $name) = $row;

if ($parentId == 0) {
// we create a new array for each top level categories
$categories[$id] = array('name' => $name, 'children' => array());
} else {
// the child categories are put int the parent category's array
$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);
}
}

// build combo box options
$list = '';
foreach ($categories as $key => $value) {
$name = $value['name'];
$children = $value['children'];

$list .= "<optgroup label=\"$name\">";

foreach ($children as $child) {
$list .= "<option value=\"{$child['id']}\"";
if ($child['id'] == $catId) {
$list.= " selected";
}

$list .= ">{$child['name']}</option>\r\n";
}

$list .= "</optgroup>";
}

return $list;
}


Basically the problem is that it only creates a category list of two levels e.g.

Parent
Child
and I wish to be able to have more sub categories e.g.

Parent
Child


Child



Child
Is there anyway of adapting this code so it can do something like that? Many thanks for any help.

Faz
01-03-11, 00:20
I have worked with this system before, and know that if you are using phpMyAdmin, you can specify a parent folder in the table 'tbl_category'( there is a value called 'parent_id' or similar) which is then output onto the site, I believe. Whether it works down to >2 levels, I am not sure.

Edit: just tried it, and although it outputs a bit funny, there is definite sub categories. How it works with regards to that drop down, I am not sure, but definitely try changing 'cat_parent_id' to the specified parent.

ca99uk
01-03-11, 08:38
Hi Faz,

thanks for your reply I think the database can handle this fine, but its just the code that is building the drop down list box (which I posted). The code below builds a menu navigation that shows all the categories and child cats and this works numerous levels ok, but I got no idea how to adjust the code from the first post so that the drop down list would work?



/************************************************** *******
* CATEGORY FUNCTIONS
************************************************** *******/

/*
Return the current category list which only shows
the currently selected category and it's children.
This function is made so it can also handle deep
category levels ( more than two levels )
*/
function formatCategories($categories, $parentId)
{
// $navCat stores all children categories
// of $parentId
$navCat = array();

// expand only the categories with the same parent id
// all other remain compact
$ids = array();
foreach ($categories as $category) {
if ($category['cat_parent_id'] == $parentId) {
$navCat[] = $category;
}

// save the ids for later use
$ids[$category['cat_id']] = $category;
}

$tempParentId = $parentId;

// keep looping until we found the
// category where the parent id is 0
while ($tempParentId != 0) {
$parent = array($ids[$tempParentId]);
$currentId = $parent[0]['cat_id'];

// get all categories on the same level as the parent
$tempParentId = $ids[$tempParentId]['cat_parent_id'];
foreach ($categories as $category) {
// found one category on the same level as parent
// put in $parent if it's not already in it
if ($category['cat_parent_id'] == $tempParentId && !in_array($category, $parent)) {
$parent[] = $category;
}
}

// sort the category alphabetically
array_multisort($parent);

// merge parent and child
$n = count($parent);
$navCat2 = array();
for ($i = 0; $i < $n; $i++) {
$navCat2[] = $parent[$i];
if ($parent[$i]['cat_id'] == $currentId) {
$navCat2 = array_merge($navCat2, $navCat);
}
}

$navCat = $navCat2;
}


return $navCat;
}

/*
Get all top level categories
*/
function getCategoryList()
{
$sql = "SELECT cat_id, cat_name, cat_image
FROM tbl_category
WHERE cat_parent_id = 0
ORDER BY cat_name";
$result = dbQuery($sql);

$cat = array();
while ($row = dbFetchAssoc($result)) {
extract($row);

if ($cat_image) {
$cat_image = WEB_ROOT . 'images/category/' ."icon" . $cat_image . ".png";
} else {
$cat_image = WEB_ROOT . 'images/no-image-small.png';
}

$cat[] = array('url' => $_SERVER['PHP_SELF'] . '?c=' . $cat_id,
'image' => $cat_image,
'name' => $cat_name);

}

return $cat;
}

/*
Fetch all children categories of $id.
Used for display categories
*/
function getChildCategories($categories, $id, $recursive = true)
{
if ($categories == NULL) {
$categories = fetchCategories();
}

$n = count($categories);
$child = array();
for ($i = 0; $i < $n; $i++) {
$catId = $categories[$i]['cat_id'];
$parentId = $categories[$i]['cat_parent_id'];
if ($parentId == $id) {
$child[] = $catId;
if ($recursive) {
$child = array_merge($child, getChildCategories($categories, $catId));
}
}
}

return $child;
}

function fetchCategories()
{
$sql = "SELECT cat_id, cat_parent_id, cat_name, cat_image, cat_description
FROM tbl_category
ORDER BY cat_id, cat_parent_id ";
$result = dbQuery($sql);

$cat = array();
while ($row = dbFetchAssoc($result)) {
$cat[] = $row;
}

return $cat;
}

mrnothersan
01-03-11, 17:46
You could go for a paid one if you are looking to use this for long term. I think WHCMS is a good one (that may be the wrong name!) Sorry I cannot be of more help

:)

Faz
01-03-11, 22:58
Hi Faz,

thanks for your reply I think the database can handle this fine, but its just the code that is building the drop down list box (which I posted). The code below builds a menu navigation that shows all the categories and child cats and this works numerous levels ok, but I got no idea how to adjust the code from the first post so that the drop down list would work?



Sorry I was no help with regards to your problem! I post too late at night :P

I will have a look and a think maybe tomorrow, and see what can be done. May just need a simple loop to check whether there are sub-categories (simple... php... as if!!! :P )

also, you can embed your code in the following tags which helps when we look through it.

ok it didn't like that... use (php) and (/php) but with square brackets...

ca99uk
02-03-11, 00:15
Hi Faz,

edited the code brackets so hopefully showing right now :) And many thanks for looking at it much appreciated.

mrnothersan thanks for the input in regards to a good paid alternative, but really this is just for a little hobby site and doesnt need anything fancy if I get this one problem fixed than its all sorted... all it is is a little combo box... I spent hours getting the rest of the things working as should but just cant crack this ... argh hehe

ca99uk
27-03-11, 18:43
a little bump to see if anyones got any ideas still wracking my brians with it :)

ca99uk
31-03-11, 11:56
Hi chaps,

if this is not possible to modify the function so it displays

cat
--sub cat 1
-----sub cat 1.1
-----sub cat 1.1.1
--sub cat 2
-----sub cat2.1

is it possible to modify so it is just a flat list e.g.


cat
sub cat 1
sub cat 1.1
sub cat 1.1.1
sub cat 2
sub cat2.1

??

Smifis
31-03-11, 20:08
I'll look into this for you when i wake up a bit, but for now, I'm knackered!

ca99uk
31-03-11, 22:06
Thanks Smifis thats absolutly fantastic :)

ca99uk
15-04-11, 11:30
Hi Everyone I am still stuck on this its driving me crazy and the whole mini project I am doing is done apart from a category list select box.... argh

Ive read loads of stuff and tried to mash in different examples with this but just cant get it working :(

I am just too thick to figure it out :D

Is there anybody out there that is a genius and can solve this puzzle I got no more hair left to pull out :D