Commodity expansion Classification and commodities
Build table
drop table if exists p40_goods_cat;
CREATE TABLE P40_goods_cat
(
cat_id mediumint unsigned NOT NULL comment ' class ID ',
goods_id mediumint unsigned not NULL comment ' Product ID ',
Key cat_id (cat_id),
Key goods_id (goods_id)
) Engine=innodb default Charset=utf8 comment ' Commodity extension classification ';
- Add a drop-down box for an extended category to the form
-
<tr>
<td class= label > Extension Category: <input onclick= "$ (' #cat_list '). Append ($ (' #cat_list '). Find (' Li '). EQ (0). Clone ()); " Type= "button" id= "Btn_add_cat" value= "Add a"/></TD>
<td>
<ul id= "cat_list" ;
<li>
<select name= "ext_cat_id[";
<option value= "" > select Category </ Option>
<?php foreach ($catData as $k + $v):,
<option value= "<?php echo $v [' id '];?>" >& lt;? PHP echo str_repeat ('-', 8* $v [' Level ']). $v [' Cat_name ']; ></option>
<?php Endforeach;;
</select>
</li>
</ul
</td>
</tr>
<tr>
- Modify the product model, after the product has been added, get the ID of the product to receive the extended Category ID array in the form to insert into the classification table
Print the form first
/**
* This method is called after the product is added, where $data[' ID ' is the ID of the newly added item
*/
protected function _after_insert ($data, $option)
{
/************ Processing Extended Classification *****************/
$ecid =i (' post.ext_cat_id ');
if ($ECID)
{
$gcModel =d (' Goods_cat ');
foreach ($ecid as $k = $v)
{
if (empty ($v))
Continue
$gcModel->add (Array (
' cat_id ' = $v,
' goods_id ' = $data [' id '],
));
}
}
- Add one more column to the list of items: "Extended category name"
To modify the search method in the product model, connect the table:
/************** fetch data from a page ***************/
/**
* SELECT A.*,b.brand_name from P39_goods a left JOIN P39_brand B on a.brand_id=b.id
*/
$data = $this->order ("erby erway")//Sort
->field (' A.*,b.brand_name,c.cat_name ')
->alias (' a ')
->join (' LEFT join __brand__ B on a.brand_id=b.id
Left JOIN __category__ C on a.cat_id=c.id
Left JOIN __goods_cat__ D on a.id=d.goods_id
Left JOIN __category__ E on d.cat_id=e.id ')
->where ($where)//Search
->limit ($pageObj->firstrow ', '. $PAGEOBJ->listrows)//Page Flip
->select ();
Because there are multiple classifications for a commodity, multiple copies of the product appear after the list:
Solutions
Group
->where ($where)//Search
->limit ($pageObj->firstrow ', '. $PAGEOBJ->listrows)//Page Flip
->group (' a.id ')
->select ();
You can call the Group_concat function to bring together values from the same group
$data = $this->order ("erby erway")//Sort
->field (' A.*,b.brand_name,c.cat_name,group_concat (e.cat_name) ext_cat_name')
Gruop_concat is used by default , connections can be used:
$data = $this->order ("erby erway")//Sort
->field (' A.*,b.brand_name,c.cat_name,group_concat (e.cat_name SEPARATOR "<br/>") Ext_cat_name ')
->alias (' a ')
Search by category when searching for a product in categories
- Modify the search method in the product model to add the category searches to the code that expands the taxonomy
Add a method to the commodity model: Search by category ID for all product IDs"consider extended classification and main classification"
/**
* Take out the id[of all items in a category considering the main classification and the extended classification "
*
* @param unknown_type $catId
*/
Public Function Getgoodsidbycatid ($catId)
{
First remove the ID of all subcategories
$catModel = D (' category ');
$children = $catModel->getchildren ($catId);
and sub-categories together
$children [] = $catId;
/*************** take out the main category or expand the assortment of items in these categories ****************/
Remove the item ID under the main category
$gids = $this->field (' id ')->where (Array (
' cat_id ' = Array (' in ', $children),
))->select ();
Remove the ID of the item under the extended category
$gcModel = D (' Goods_cat ');
$gids 1 = $gcModel->field (' DISTINCT goods_id id ')->where (Array (
' cat_id ' = = Array (' in ', $children)
))->select ();
Merge the ID of the main category and the product ID of the extended category into a two-dimensional array "two are not empty when merging, otherwise take out an array that is not empty"
if ($gids && $gids 1)
$gids = Array_merge ($gids, $gids 1);
ElseIf ($gids 1)
$gids = $gids 1;
Two-dimensional to one-dimensional and go heavy
$id = Array ();
foreach ($gids as $k = $v)
{
if (!in_array ($v [' id '], $id))
$id [] = $v [' id '];
}
return $id;
}
- Modify the search method in the product model the actual way to search for goods
Search by main category
$catId =i (' get.cat_id ');
if ($catId)
{
First remove the ID of all subcategories
$gids = $this->getgoodsidbycatid ($catId);
Search for all items under these categories
$where [' a.cat_id ']= array (' in ', $gids);
}
Modification and deletion of commodity classification
Delete ----Therelated data in the Extended classification table is also deleted when the item is deleted
protected function _before_delete ($option)
{
$id = $option [' WHERE '] [' id ']; The ID of the item to delete
/************** Deleting extended classifications ********************/
$gcModel =d (' Goods_cat ');
$gcModel->where (Array (
' goods_id ' =>array (' eq ', $id),
))->delete ();
Modify--"
- Copy the extended category button from the added form.
Remove Extended Categories
$gcModel =d (' Goods_cat ');
$gcData = $gcModel->field (' cat_id ')->where (Array (
' goods_id ' = Array (' eq ', $id),
))->select ();
Setting page information
$this->assign (Array (
' Gcdata ' = $gcData,
Loop output in a form
Also consider the default output of a drop-down box if there is no original classification
<tr>
<TD class= "label" > Extension Category: <input onclick= "$ (' #cat_list ')." Append ($ (' #cat_list '). Find (' Li '). EQ (0). Clone ()); " Type= "button" id= "Btn_add_cat" value= "Add a"/></td>
<td>
<ul id= "Cat_list" >
<!--If you have the original classification of the loop output, otherwise the default output a drop-down box--
<?php if ($gcData):?>
<?php foreach ($gcData as $k 1 = $v 1):?>
<li>
<select name= "ext_cat_id[" >
<option value= "" > select Category </option>
<?php foreach ($catData as $k + = $v):
if ($v [' id '] = = $v 1[' cat_id '])
$select = ' selected= ' selected ';
Else
$select = ";
?>
<option <?php echo $select?> value= "<?php echo $v [' id '];?>" ><?php Echo str_repeat ('-', 8* $v [' Leve L ']). $v [' Cat_name '];?></option>
<?php Endforeach;?>
</select>
</li>
<?php Endforeach;?>
<?php Else:?>
<li>
<select name= "ext_cat_id[" >
<option value= "" > select Category </option>
<?php foreach ($catData as $k = $v):?>
<option <?php echo $select?> value= "<?php echo $v [' id '];?>" ><?php Echo str_repeat ('-', 8* $v [' Leve L ']). $v [' Cat_name '];?></option>
<?php Endforeach;?>
</select>
</li>
<?php endif;?>
</ul>
<span style= "color: #F00; font-size:16px;font-weight:bold;" > If you want to delete a category, set it to "SELECT category"! </span>
</td>
</tr>
- After submitting the form, clear the original classification data, and then add the modified as a new one again
Code to add a taxonomy when copying directly
protected function _before_update (& $data, $option)
{
$id = $option [' WHERE '] [' id ']; The ID of the item to be modified
/************ Processing Extended Classification *****************/
$ecid =i (' post.ext_cat_id ');
$gcModel =d (' Goods_cat ');
Delete the original categorical data first
$gcModel->where (Array (
' goods_id ' =>array (' eq ', $id),
))->select ();
if ($ECID)
{
$gcModel =d (' Goods_cat ');
foreach ($ecid as $k = $v)
{
if (empty ($v))
Continue
$gcModel->add (Array (
' cat_id ' = $v,
' goods_id ' = $id,
));
}
}
Commodity expansion Classification and commodities