用权重排序数组?
问题
现在我必须创建这个:
在我的数据库中;每个最爱都有自己的“权重”条目。
截至目前,它默认为“10”
假设我有一系列收藏夹,
权重相应地如下所示:
- 1
- 2
- 3
- 4
如果我要按“2”上的“向下”箭头,我必须为其权重添加 +1。
这将导致:
- 1
- 3
- 3
- 4
但我只想用最喜欢的“3”的重量交换最喜欢的“2”。
我需要一种方法来确保每个项目的权重按其隐含的方式发挥作用。 因为仅仅加或减 1 就会导致很多问题。
希望我解释得正确。
代码
写入数据库
/*
* Write Form data to database
*/
function f25_favorites_form_submit($form, &$form_state){
global $user;
$listOfPaths = f25_favorites_listOfPaths();
$selected = $form_state['values']['path'];
$data = array(
'uid' => $user->uid,
'path' => $selected,
'title' => $listOfPaths[$selected]['#title'],
'weight' => 10,
'timestamp' => time(),
);
drupal_write_record(f25_favorites, $data);
}
查询
/*
* Fetching Specific User's Favorites Data
*/
function f25_favorites_user_favorites() {
global $user;
if($user->uid > 0){
$userid = $user->uid;
$user_paths = db_query("SELECT * FROM f25_favorites foo WHERE foo.uid = '%s' ORDER BY foo.weight DESC", $userid);
$pathlist = array();
while ($data = db_fetch_object($user_paths)) {
$pathlist[] = $data;
}
return $pathlist;
}
}
我应该如何解决这个问题?
The problem
Right now I have to create this:
In my database; each favorite as its own 'weight' entry.
As of right now it defaults to '10'
Let's say I have an array of favorites,
the weights go like this accordingly:
- 1
- 2
- 3
- 4
If I were to press the 'down' arrow on '2' I'd have to add +1 to its weight.
Which would result in:
- 1
- 3
- 3
- 4
But I just want to switch favorite '2' with favorite '3's weight.
I need a way to ensure that when the weights for each item function as they're implied.
Since just adding or subtracting 1 will lead to a lot of problem.
Hopefully I explained it properly.
The code
Writing to the database
/*
* Write Form data to database
*/
function f25_favorites_form_submit($form, &$form_state){
global $user;
$listOfPaths = f25_favorites_listOfPaths();
$selected = $form_state['values']['path'];
$data = array(
'uid' => $user->uid,
'path' => $selected,
'title' => $listOfPaths[$selected]['#title'],
'weight' => 10,
'timestamp' => time(),
);
drupal_write_record(f25_favorites, $data);
}
The Query
/*
* Fetching Specific User's Favorites Data
*/
function f25_favorites_user_favorites() {
global $user;
if($user->uid > 0){
$userid = $user->uid;
$user_paths = db_query("SELECT * FROM f25_favorites foo WHERE foo.uid = '%s' ORDER BY foo.weight DESC", $userid);
$pathlist = array();
while ($data = db_fetch_object($user_paths)) {
$pathlist[] = $data;
}
return $pathlist;
}
}
How should I approach this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您最喜欢的所有权重不同,您只需在减少权重时将“起床”的权重字段与“下车”的权重互换即可,或者相反。不过你必须弄清楚代码
if all your favorites are weighted differently, you can simply swap the
weight
field of the one "getting up" with the one "getting down", or reverse, when decreasing weight. you'd have to figure out the code though因为只有很少的项目,所以我会将整个订单保存到数据库中,而不仅仅是已移动的内容。:我对 drupal 不太了解,但我认为这很容易完成,因为 drupal 是基于 php 和 javascript 的。
html中的每个元素都有自己的id/index(uid?)
使用javascript以重新排序的顺序获取项目列表(例如:
使用ajax将itm发送到您的php更新程序。并更新您的数据库,使用收到的位置而不是权重现在
你的数据库有这样的东西:
我推荐你jquery用于javascript工作,jquery有一个很好的可排序插件,但它对于 ajax 东西。
as there are only little items I would save the whole order to the database, not only what has been moved.: i don't know much about drupal but i think it would be easy to accomplish as drupal is php and javascript based.
each element in html has its own id/index (uid?)
use javascript to get list of your items in the reordered order (like:
send itm with ajax to your php updater. and update your db, use the position received and not weights.
now your db has something like this:
I recommend you jquery for the javascript work, jquery has a nice sortable plugin. but it is also awesome for the ajax stuf.