MyBB Community Forums

Full Version: Using the query system of myBB with forearch instead of while?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

When working with database data, I tend to prefer to grab the array first and then work with that array later, i.e. I prefer to use foreach rather than a while.

part of the reason I want to do this is to be able to support sorting of the array which is not as easily done when you need to use a while to fetch the entire array to begin with. I know that I could build the array from the while and then play with it, but it seems a little inefficient. Foreach is generally in my opinion more efficient as well.

Is there a way to do database work with more of an ezSQL feel with mybb or are we more confined to the current method?

Cheers
If you need to sort the array, just put the order by in the original query.

Plus the query result is usually record based and foreach won't work to iterate the records in the query result object.
However array sorting allows me to manipulate that data set without hitting the database, this means I can manage a wider range of possibilities without explicitly building new queries
As it stands, the only way to do this I know of is to build the array using a while then manipulate it however you want before using a foreach. As you stated, it's not efficient doing this at all and so pavemen's recommendation of using sorting within the query is generally preferred.
(2012-08-10, 02:35 AM)Dannymh Wrote: [ -> ]However array sorting allows me to manipulate that data set without hitting the database, this means I can manage a wider range of possibilities without explicitly building new queries

but you need to hit the DB to get the data in th first place. at least sort the data the first time and save that one loop operation
(2012-08-10, 02:49 PM)pavemen Wrote: [ -> ]
(2012-08-10, 02:35 AM)Dannymh Wrote: [ -> ]However array sorting allows me to manipulate that data set without hitting the database, this means I can manage a wider range of possibilities without explicitly building new queries

but you need to hit the DB to get the data in th first place. at least sort the data the first time and save that one loop operation

its just not efficient for what I need and each loop has its place. In particular I can build a recursive category list without hitting the database multiple times when using a foreach.

Also I only need to hit the database once in order to later sort that array as I need within the same page, it also means programatically speaking I don't have to build different queries when I can use a simple array sort on the same data

Anyway it's not there so I will deal with what is there.

Thanks for your help

Just re-read my reply, sorry if I sounded ungrateful or arrogant in my reply. I had just returned from the dentists chair Smile

Thanks for your help. Now onto using the Paging classes Smile