Ordering

Started by Matthew K., March 03, 2011, 12:12:37 AM

Previous topic - Next topic

Arantor

Seriously, without being funny, how did you manage to misread what I said?

Surely you know at the point of the issuing the update, what position you're putting the item in? Let's say you're going to put it into position 5 in the list.

You simply issue: UPDATE smf_menu_table SET order = order + 1 WHERE order >= 5

This will move everything that is *currently 5 or more* in the table down an entry. Then you insert the new menu item. No farting about in PHP, no foreach, none of that crap. You let MySQL deal with it all.

Alternatively if you *don't* know what the order value is at that point, I'd say you have bigger things to worry about structurally...
Holder of controversial views, all of which my own.


Matthew K.

#41
Topic resolved. :) Figured everything out.

Thanks for the help everyone.

Arantor

So what is the query actually being sent to the database? How do you know the right parameter was sent to it?

And you did run that update being moving the button, right?
Holder of controversial views, all of which my own.


Matthew K.

I was sending id_button instead of before_id, so it wasn't ordering them properly, so I just had to change the value being send in $_POST['placement_button'].

Here's what I ended up with so far.
/* Before */
if (empty($_POST['placement']))
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}menu_items
SET before_id = before_id + 1
WHERE before_id >={int:placement}',
array(
'placement' => $_POST['placement_button'],
)
);
}

Arantor

And does that work as expected?
Holder of controversial views, all of which my own.


Matthew K.

It does, although I still have to finish some other order coding so it inserts the proper value, right now, it went 1-3-4 and skipped two.

So not a big deal, still ordered them properly on the output, and the outcome is the same, but it's still something I'm going to fix.

Arantor

That would suggest it's running things out of order. Add this to your Settings.php file:
$db_show_debug = true;
Holder of controversial views, all of which my own.


Matthew K.

I actually think it was due to the fact I think it was starting at the first button being 0. When I manually changed before_id in the database to "1-2-3" and then changed around the order, I couldn't replicate the issue.

Arantor

It actually shouldn't matter.
Holder of controversial views, all of which my own.


Matthew K.

Okay, well, I am going to finish ordering up, and then test it again and see if I can produce any errors.

Arantor

In which case you should definitely use the tip I just posted so you can see what actual queries are being run in what order.
Holder of controversial views, all of which my own.


Matthew K.

I am already added $db_show_debug. :) Thanks Arantor, looks pretty darn useful.

Matthew K.

Okay, correct me if I am wrong...but don't I need to put something in place so if I move button 2 to place 4, they move down so there is not an empty place? If I am correct, what is the smartest way to accomplish that?

Arantor

Move it to the end then UPDATE them all to order = order - 1 WHERE order >= old pos
Holder of controversial views, all of which my own.


Matthew K.

#54
The problem once again was in the template side.

Advertisement: