Jump to content

e-commerce Plaincart - help to modify to 3 levels categories


Recommended Posts

Plaincart is a simple PHP e-commerce shopping cart. It stores and retrieves 2 level category info in Mysql database as shown below

 

tbl_category

| cat_id | cat_parent_id | cat_name |

    12            0                  Female

    17            12                T-Shirt

 

function buildCategoryOptions without modification build a drop down like...

Female (level 1 - sex)

  >> T-Shirt (level 2 - group)

 

HELP: I wish to modify the PHP to a 3 level categories instead as shown below...

 

tbl_category

| cat_id | cat_parent_id | cat_name |

    12            0                  Female

    17            12                T-Shirt

    20            17                Goridano

 

function buildCategoryOptions should build a drop down like...

Female (level 1 - sex)

  >> T-Shirt (level 2 - group)

    >> Goridano (level 3 - brand)

 

Codes

I have tried to add in the codes to the following function which will do a 3 level categories poll, but somehow it is not working but in any case, i shall list my attempted codes below in case someone can correct my mistakes.

 

/*

  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, cat_description

      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, $cat_description) = $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); 

  }

 

<------- START of my add-in attempted codes ------->

  //else {

  // check if this is 2nd OR 3rd level category     

  //$sql = mysql_query("SELECT cat_parent_id

  //            FROM tbl_category

  //            WHERE cat_id = $parentId");

       

  //$row = mysql_fetch_assoc($sql);

     

  //if ($row["cat_parent_id"] == 0) {

  // we create a new array for 2nd level categories

  // added 'children' => array()

  //$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name, 'children2' => array()); 

  //}

       

  //else {

  // the child categories are put int the parent category's array

  //$categories[$row["cat_parent_id"]][$parentId]['children2'][] = array('id' => $id, 'name' => $name); 

  //$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name); 

  //}

  //}  // else

  } 

 

<------- END of my add-in attempted codes ------->

 

  // 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;

}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.