News:

Wondering if this will always be free?  See why free is better.

Main Menu

Ordering

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

Previous topic - Next topic

Matthew K.

Hey there! With my latest project (Menu Editor), I finally needed to learn ordering things so that the menu order could be user-defined.

Here is what I have so far, which just isn't acting how it should. I can explain more if it's not something obvious that's wrong with the code.
if (empty($_POST['placement']))
{
$desired_position = $_POST['placement_button'];
foreach ($context['menu_editor']['items'] as $key => $value)
{
if ($value['before_id'] >= $desired_position)
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}menu_items
SET before_id={int:new_before_id}
WHERE id_button={int:id_button}',
array(
'new_before_id' => (int) ++$value['before_id'],
'id_button' => (int) $value['id_button'],
)
);
}
}
}


Thanks,
Labradoodle-360

live627

So what is before_id and id_button?

Or better yet, what does the mod do? Add buttons or edit existing buttons?

If the former (which I think it is) then before_id is the ID of the button to insert before, yeah? id_button is obvious.

Circular reasoning... what's wrong with your programme? That little code snippet doesn't answer that, although I did find something... it's the same problem I encountered when rewriting Dream Menu. I'll give a hint: IDs are not used in PHP arrays the same as in a SQL table. So I did not rely on a random ID...

Matthew K.

It doesn't really matter what the mod does. It's a full fledged Menu Editor; it'll allow any default buttons to be modified, or even deleted, or the order switched, or you can add your own buttons, the feature-set is only growing as I develop it.

before_id isn't really name obvious - Although it probably should be. That is the column that is used in the query to pull the array for ORDER BY before_id.

id_button is name obvious, and is the id of each button.

To be honest, I'm not positive what's wrong with it, sometimes it reacts partially how it should, other times not at all, which tells me I did something wrong in the code.

I put a LOT of thought into ordering, and this is what I came up with. I pick my placement method (this code is only for "Before"), then I pick a button, the value of the button is it's id. Since the columns id_button and before_id are almost identical, except for id_button is used as the identifier for each button, and it's an auto-increment field, while before_id changes to accommodate user-defined ordering.

So, the code below...if $_POST['placement'] is empty, which makes it a "Before - Button" then foreach button, it adds 1 to it's current "before_id" if the current before_id is greater than or equal to the $_POST['placement_button'] - the identifier. Which leaves the desired spot empty, and all the other buttons +1 so it should be laid out all nicely for the query below to place the current button into the empty spot and then the array to pull out using ORDER before_id.

Thank you for your time, John.

Best Regards,
Labradoodle-360
Quote from: live627 on March 03, 2011, 01:06:48 AM
So what is before_id and id_button?

Or better yet, what does the mod do? Add buttons or edit existing buttons?

If the former (which I think it is) then before_id is the ID of the button to insert before, yeah? id_button is obvious.

Circular reasoning... what's wrong with your programme? That little code snippet doesn't answer that, although I did find something... it's the same problem I encountered when rewriting Dream Menu. I'll give a hint: IDs are not used in PHP arrays the same as in a SQL table. So I did not rely on a random ID...

Matthew K.


Matthew K.

Okay, it's been a couple of days, and I'd really like to finish this up. Anyone have insight on this?

cicka

Quote from: Labradoodle-360 on March 07, 2011, 12:29:47 PM
Okay, it's been a couple of days, and I'd really like to finish this up. Anyone have insight on this?

Bring it to Arantor 's attention. He seems to know everything about coding and Smf.

Matthew K.

He has already stated he does not intend on answering this topic.
Quote from: cicka on March 07, 2011, 12:43:56 PM
Quote from: Labradoodle-360 on March 07, 2011, 12:29:47 PM
Okay, it's been a couple of days, and I'd really like to finish this up. Anyone have insight on this?

Bring it to Arantor 's attention. He seems to know everything about coding and Smf.

texasman1979

You have a logic error, coding is fine. From what im seeing, you need to load the list of button ids and sort/add/subtract the list. Ur over writing values. Try something like this: temp = a, a = b, b = temp. Thro an if in there to see if the before id is the index current, assign it and then increment index as desired, then save the config. Think sorting an array and youll have it.
SMF 2.0.4
SimplePortal 2.3.5

LOGIC is a FOUR letter word! :)


Matthew K.

I don't understand what you mean...is there really an error in my logic? I don't think there is...

texasman1979

I see better what you are after, the new buttons will be stored in the db with a place marker. I cant on my fone give you an example, but ill try and fix it tonight. You have an array of button ids. You need to by way of a table make those buttons load in a pptentially differrent order with perhaps new buttons placed appropriately. Make an array of size of new list of buttons. Assign all the button ids a sequential index 123... then assisn to each of those the in the order you wish the old and new button ids. Then when you call for the buttons, echo then using the sequential index as key. Therefore dinamically changing the order of appearence in the menu. You ofc have the new buttons in another table with their appropriate ids assigned in conjunction with the order table. I add to this if need be, but you do really nice work. I think youll get now what im thinking. I hope this helps.
SMF 2.0.4
SimplePortal 2.3.5

LOGIC is a FOUR letter word! :)


Matthew K.

Haha thank you :)

And I don't see how there is an error in my logic though...whenever a new button is added or modified, it adds +1 to every button including the id you want, and every button after.

So if I specify I want a button before button 5? if button 5 exists, it makes it button 6, if button 6 exists, button 6 becomes button 7, and so fourth. Then, there will be an empty space - button 5, which can then be inserted.

texasman1979

Ah i see, bare in mind im on a fone. Lol but just as a test, i would test it with a manua control and refine till it either works right or brakes again and youll be able to narrow where the fault is. And excellent idea, ill be using this mod im sure. :)
SMF 2.0.4
SimplePortal 2.3.5

LOGIC is a FOUR letter word! :)


Matthew K.

I'd still appreciate it at this point if someone could look over my shoulder at the ordering code and give suggestions as to why it's not working as I wrote it to.

texasman1979

New before id => int ++, ur not incrementing any value? What is (int)++?
SMF 2.0.4
SimplePortal 2.3.5

LOGIC is a FOUR letter word! :)


Matthew K.

'new_before_id' => (int) ++$value['before_id'],
Should I try removing (int) then, ah? It specifies that ++$value['before_id'] is an int.

Although, it already has to be an int, so I suppose it's not completely necessary.

ascaland

Quote from: texasman1979 on March 07, 2011, 05:27:56 PM
New before id => int ++, ur not incrementing any value? What is (int)++?

It means pre-increment, and all it is doing is casting the value to int.
And give me a couple minutes I will look at it.

Arantor

Yay for operator precedence.

This will be interpreted as:
* take the value of $value['before_id']
* silent cast to int or float if it won't fit in an int
* increment it
* explicit cast to int
* assign it to the array key

Is that what you want to do?

EDIT: Ninja'd
Holder of controversial views, all of which my own.


ascaland

Aha! Give this a go,
if (empty($_POST['placement']))
{
$desired_position = $_POST['placement_button'];
foreach ($context['menu_editor']['items'] as $key => &$value)
{
if ($value['before_id'] >= $desired_position)
{
$smcFunc['db_query']('', '
UPDATE {db_prefix}menu_items
SET before_id={int:new_before_id}
WHERE id_button={int:id_button}',
array(
'new_before_id' => (int) ++$value['before_id'],
'id_button' => (int) $value['id_button'],
)
);
}
}
}


Turns out when your using foreach while iterating the array, the $value is passed by value and not reference!

Arantor

Aside from the inevitable fact that it makes it harder to follow later and will confuse people, is it a problem that that code is PHP 5 only, and will leave a stray reference after the loop has completed?
Holder of controversial views, all of which my own.


Matthew K.

Project Evolution's solution did not work :P Only throws errors now.

What are your ideas, Arantor?

Advertisement: