问题
I am making Categories system management like WordPress.
As you can see in my database if i delete (or updating delete to 1) a category. All its child disappear fro the list.
Before delete ->
After delete ->
But this not happening in the WordPress case. It deletes the selected category and assign its child to the parent of deleted category. I want to do the same.
Step 1 -> get the parent of category which will be deleted.
Step 2 -> update parent field of all child's of selected category with the id which we will get from from step 1.
Step 3 -> delete the selected category.
Am i right? Is there any other way or shorter way to do that?
I think wordpress id doing the same A comment from taxonomy.php wordpress file (function wp_delete_term)
/**
* Fires immediately after a term to delete's children are reassigned a parent.
*
* @since 2.9.0
*
* @param array $edit_tt_ids An array of term taxonomy IDs for the given term.
*/
A workaround, may help some other users or devs, if you have any another or other solution, please let me know.
Getting categories from Database ->
Array "m" stores the proper shorted categories.
"m2" stores the Parent Cat id as key and its Children ids as value.
if (!function_exists('_get_categories')) {
function _get_categories($key = 0, $parent = 'term_parent', $db_key = 'term_id', $child = 'children', $child_id = 'children_id') {
$CI =& get_instance();
$m = array();
$m1 = array();
$cat_query = $CI->db->get_where('terms', array(
'term_active' => 1,
'term_delete' => 0,
'term_type' => 'category'
));
$input_data = $cat_query->result_array();
foreach ($input_data as $data) {
if($data['term_parent']==-null)
{
$data['term_parent']=0;
}
if(!isset($m[$data[$parent]]))
{
$m[$data[$parent]] = array();
}
if(!isset($m[$data[$db_key]]))
{
$m[$data[$db_key]] = array();
}
if(isset($m1[$data['term_parent']][$child_id]))
{
$m1[$data['term_parent']][$child_id] = $m1[$data['term_parent']][$child_id].','.$data['term_id'];
}
else{
$m1[$data['term_parent']][$child_id] = ','.$data['term_id'];
}
$m[$data[$parent]][] = array_merge($data, array($child => &$m[$data[$db_key]]));
}
return(array($m[$key],$m1));
}
}
Now printing categories list properly ->
function nested($data,$child_ids,$level,$urls) {
if (sizeof($data) > 0) {
foreach ($data as $entry) {
$childern_ids ="";
if(isset($child_ids[$entry['term_id']]['children_id']))
{
$childern_ids = $child_ids[$entry['term_id']]['children_id'];
}
?>
<tr role="row" class="odd">
<td>
<label>
<?php echo form_checkbox('category_active_table_checkbox_singal[]',$entry['term_id'].','.$entry['term_parent'].''.$childern_ids, false, array('class'=>'category_active_table_checkbox_singal')); ?>
</label>
</td>
<td class="sorting_1">
<?php
for($i=1;$i<$level;$i++)
{
echo " ";
}
?>
-- <?php echo $entry['term_name']; ?></td>
<td>
<?php echo $entry['term_time']; ?></td>
<td>
<a href="<?php echo $urls['category_edit_url'].'?category_id='.$entry['term_id']; ?>">Edit</a>
</td>
</tr>
<?php
nested($entry['children'],$child_ids,$level+1,$urls);
}
}
}
nested($active_category_records[0],$active_category_records[1],1,$urls);
Here nested function parameters. 1. Data (return m), 2. Child array (return m1), 3. Space level, 4. My custom need
Processing file -> (delete file)
$category_ids = $this->input->post('category_active_table_checkbox_singal[]');
$action_type = $this->input->post('table_multi_action_active_category_action_type');
$from_hidden_url = $this->input->post('return_url');
$cat_array = explode(",", $category_ids[0]);
echo $cat_id = $cat_array[0];
echo $cat_parent_id = $cat_array[1];
$data = array(
'term_parent' => $cat_parent_id
);
$where = array (
'term_parent' => $cat_id
);
$this->Category_model->category_update($data,$where);
$data1 = array(
'term_delete' => 1,
'term_active' => 0
);
$where1 = array (
'term_id' => $category_ids[0]
);
$this->Category_model->category_update($data1,$where1);
Here you can do whatever you want, Twist here is you also getting "Parent ID" and its "Children IDs" without any database query. Use explode and do anything
来源:https://stackoverflow.com/questions/46129056/deleting-a-item-from-multilevel-category-or-menu-list