Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 01-09-2005, 05:55 AM
cinq's Avatar
cinq cinq is offline
 
Join Date: Oct 2002
Posts: 1,398
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Query Loop within Query Loop .... Need Optimization help

Here's something I need help on :

PHP Code:
# Categories and subcategories column
$parentcatquery $DB_site->query("SELECT * FROM " TABLE_PREFIX "articles_category WHERE parentcategory=0 ORDER BY categoryorder");
if (
$DB_site->num_rows($parentcatquery)>0)
{
    while(
$parentcatrow=$DB_site->fetch_array($parentcatquery))
    {
        
$parentid $parentcatrow["articles_categoryid"];
        
$catname $parentcatrow["categoryname"];
        
$parentcat $parentcatrow["parentcategory"];

        eval(
'$catlistbit .= "' fetch_template('vbArticles_categorybit') . '";');
        eval(
'$catlistbit .= "' fetch_template('vbArticles_categorybit_pre') . '";');

           
$subcatquery $DB_site->query("SELECT * FROM " TABLE_PREFIX "articles_category WHERE parentcategory = $parentid ORDER BY categoryorder");
        if (
$DB_site->num_rows($subcatquery)>0)
        {
            while(
$subcatrow=$DB_site->fetch_array($subcatquery))
            {
            
$subcatid=$subcatrow["articles_categoryid"];
            
$subcatname=$subcatrow["categoryname"];
            eval(
'$catlistbit .= "' fetch_template('vbArticles_childcatbit') . '";');
            }
        }
    eval(
'$catlistbit .= "' fetch_template('vbArticles_categorybit_post') . '";');
    }

I am trying to get the categories and then the subcategories of the parent category to display.
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.
Reply With Quote
  #2  
Old 01-09-2005, 05:57 AM
cinq's Avatar
cinq cinq is offline
 
Join Date: Oct 2002
Posts: 1,398
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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 !
Reply With Quote
  #3  
Old 01-09-2005, 10:58 AM
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Location: England
Posts: 9,071
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You're best off storing a cache of the subcategories in your article_category table
Reply With Quote
  #4  
Old 01-09-2005, 11:01 AM
cinq's Avatar
cinq cinq is offline
 
Join Date: Oct 2002
Posts: 1,398
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I don't understand Dean
Reply With Quote
  #5  
Old 01-09-2005, 11:22 AM
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Location: England
Posts: 9,071
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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:
while($subcats $DB_site->fetch_array($query))
{
       
$array .= $subscats;
}

$subcats2 serialize($array);

$DB_site->query("UPDATE category_table SET subnodes='" addslashes($subcats2) . "' WHERE categoryid=$categorid"); 
Reply With Quote
  #6  
Old 01-09-2005, 12:08 PM
cinq's Avatar
cinq cinq is offline
 
Join Date: Oct 2002
Posts: 1,398
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #7  
Old 01-09-2005, 12:28 PM
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Location: England
Posts: 9,071
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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:
$array = array('yes''no''why''word');
$temp serialize($array);
echo 
$temp;
$temp unserialize($temp);
print_r($temp); 
Reply With Quote
  #8  
Old 01-09-2005, 01:45 PM
Xenon's Avatar
Xenon Xenon is offline
 
Join Date: Oct 2001
Location: Bavaria
Posts: 12,878
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

take a look at that code, that should do what your code di, but just using one query:

PHP Code:
# Categories and subcategories column
$parentcatquery $DB_site->query("
    SELECT *
    FROM " 
TABLE_PREFIX "articles_category
    WHERE parentcategory=0 ORDER BY categoryorder
"
);
$categories $DB_site->query("
    SELECT *
    FROM " 
TABLE_PREFIX "articles_category
    ORDER BY categoryorder
"
);
$cat_cache = array();
while (
$cat $DB_site->fetch_array($categories))
{
    
$cat_cache[$cat['parentcategory']][] = $cat;
}
// parent categories
foreach ($cat_cache[0] AS $parentcatrow)
{
       
$parentid $parentcatrow["articles_categoryid"];
        
$catname $parentcatrow["categoryname"];
        
$parentcat $parentcatrow["parentcategory"];

        eval(
'$catlistbit .= "' fetch_template('vbArticles_categorybit') . '";');
        eval(
'$catlistbit .= "' fetch_template('vbArticles_categorybit_pre') . '";');

        
// now do subcategories
        
foreach ($cat_cache[$parentid] AS $subcatrow)
        {
            
$subcatid=$subcatrow["articles_categoryid"];
            
$subcatname=$subcatrow["categoryname"];
            eval(
'$catlistbit .= "' fetch_template('vbArticles_childcatbit') . '";');
        }
    eval(
'$catlistbit .= "' fetch_template('vbArticles_categorybit_post') . '";');

Reply With Quote
  #9  
Old 01-09-2005, 02:00 PM
cinq's Avatar
cinq cinq is offline
 
Join Date: Oct 2002
Posts: 1,398
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

It works ! Thanks Stefan

But to understand the code better, could you explan what this bit does :
PHP Code:
$cat_cache = array();
while (
$cat $DB_site->fetch_array($categories))
{
    
$cat_cache[$cat['parentcategory']][] = $cat;

How does the $cat_cache array look like ? I find it hard to visualize

p.s. 36->11queries, 41->13queries, woot !
Reply With Quote
  #10  
Old 01-09-2005, 02:09 PM
Xenon's Avatar
Xenon Xenon is offline
 
Join Date: Oct 2001
Location: Bavaria
Posts: 12,878
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:00 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04509 seconds
  • Memory Usage 2,286KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (5)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete