カテゴリリストを優先的にデータベースに保存したい...
cat_id cat_name cat_priority
---------------------------------
1 Test1 1
2 Test2 2
3 Test3 3
4 Test4 4
Test1
優先度を3に設定する必要があるTest3
場合、Test2
優先度を設定したい場合は4
、優先度システムに従ってphp経由でデータをフェッチする方法と競合が発生します。どのように機能し、何をする必要がありますか。テーブル構造の変更。
アイテムの優先度を変更するたびに、それぞれに優先度を再割り当てする必要があります。私はあなたが重複した優先順位を望まないと思います。
//$category = ...
//get all categories
$categories = $this->db->order_by('cat_priority')->select('cat_id, cat_priority')->get();
$cat_to_change = $category['cat_id'];
$new_priority = 2;
$i_priority = 0;
$new_categories = array();
foreach($categories->result_array() as $row)
{
//this is the category we want to change, so change it
if($row['cat_id'] == $cat_to_change)
{
$row['cat_priority'] = $new_priority;
}
else
{
//increment the priority
$i_priority++;
//the new priority is owned by another cat
//so let it for the cat we want for him
if($row['cat_priority'] == $new_priority)
$i_priority++;
$row['cat_priority'] = $i_priority;
}
$new_categories[] = $row;
}
//save
$this->db->update_batch('categories', $new_categories, 'cat_id');
それをテストしませんでした。お役に立てば幸いです。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加