![]() |
Query Loop within Query Loop .... Need Optimization help
Here's something I need help on :
PHP Code:
Is there anyway I can change things around to optimize what I am trying to do ? My logic for the code above is: I get the parent categories first ( those with parentcategory=0 meaning they are the parent categories already ), and for each of those parentcategories, query again for those categories whose parentcategory = the id of those parent categories. Obviously this isn't as efficient as it can be. As it is, this is probably the code portion contributing to the massive amount of queries on the page. Any help would be most appreciated. |
A possible solution was offered by rake here, but I don't quite get it.
A solution could potentially teach me how to deal with handling queries in nested while loops ( which I keep using now in most of my hacks because I do not know otherwise ..... ) Could anyone take the time to explain this to me or offer a better solution to my code, please ? Thanks ! :D |
You're best off storing a cache of the subcategories in your article_category table :)
|
I don't understand Dean :(
|
Ok do you know how serialization works? Basically when managing your categories from the admincp you run a query like this:
[sql] SELECT categoryid, title FROM category_table WHERE parentid = $categorid [/sql] Then look through it like so: PHP Code:
|
Still not too sure what you mean, even after going through php.net's serialize function.
Will read up more on this first, thanks for pointing me in some direction :) |
Well basically it takes an array and stores it as a string. You can place the string in the database and then unserialize it back into an array once you've pulled it back out the database. vB3 uses it in a lot of places so it's a good idea to read up a bit on how it works :)
Just write a test script: PHP Code:
|
take a look at that code, that should do what your code di, but just using one query:
PHP Code:
|
It works ! Thanks Stefan :D
But to understand the code better, could you explan what this bit does : PHP Code:
p.s. 36->11queries, 41->13queries, woot ! :D |
the $cat_cache array is a cache of all categories you have or better its an array of arrays of categories and looks like that:
$cat_cache 0 => array of categories with the parentid 0 1 => array of categories with the parentid 1 and so on. |
Change this:
PHP Code:
PHP Code:
|
Nice job with that one Xenon. ;)
|
thx Matt :)
|
you know i have one i need to optimise too, but i can't figure it out, although was thinking that i'd be easier to do it via the datastore as the query in the loop takes data provided by the loop in order to return a result and it's even got a loop in the loop :-S
it's from the vBFriends hack |
Quote:
|
Ok using similar code, I am trying to show a list of authors, and their respective written articles.
This is the code i am trying to use : PHP Code:
PHP Code:
foreach works with strings in the array too, so how come this is not accepted ? error is "Invalid argument supplied for foreach()". |
Hmm didn't analyze the script, but at first glance it looks like
PHP Code:
PHP Code:
|
Thanks Marco, but nope, I don't think that would work either.
Because, the $author['author'] is the result of the query on articles by authors, not the authors query. |
Yes i know, maybe i am wrong.
PHP Code:
$author_cache['jim'][0] = array('jim','doe','UK',3,......) $author_cache['john'][0] = array('john','smith','germany',4,......) now you ask for the entry "$author_cache['author']" unless there is an author named 'author', this would give no results. Maybe i am looking complete wrong now, but just shoot me ;) i am wearing a bullet proof vest :D |
yep you're right. You got me thinking ..
I am trying to find a way about it. If i were to perform another : PHP Code:
How could I use this in the foreach statement, any idea ? :) |
this would give you an array of all the rows in the author table.
You could enumerate them with a foreach ($authorsname as $key=>$values) this will give you the index to the array in $key and an array of associated values in $value. You then could use a $authorsname[$key]['name'] for example, or a $value['name']. |
lol, would really not know, didn't go into the function if this.
|
This thread is really messy now :D
But anyways, thanks to everyone for pointing me in the right direction and taking the time to explain stuff :) For the author list feature, this is the final code I am employing, just for anyone's reference. PHP Code:
pheww, finally :) |
Well done, and if i have time (and still remember ;)) i will see if i can find anymore optimisations.
|
All times are GMT. The time now is 04:43 PM. |
Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
![]() |
|
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|