Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 02-18-2003, 11:24 PM
Dark Jim's Avatar
Dark Jim Dark Jim is offline
 
Join Date: Apr 2002
Location: Netherlands
Posts: 174
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Need help reducing queries

I made and submitted a hack "User Icons 1.1" just recently here but it needs quite a lot of queries on a few pages. For example it displays icons next to the user names on the who's online. The code in online.php looks like this currently:
PHP Code:
  $allusers$DB_site->query("SELECT user.username, session.location, session.lastactivity, user.userid, user.usergroupid, user.invisible, session.host, user.showemail, user.receivepm
                              FROM session, user
                              "
iif($WOLguests" LEFT JOIN user USING (userid) "",user") ."
                              WHERE session.lastactivity > 
$datecut
                              "
iif(!$WOLguests" AND session.userid = user.userid""") ."
                              ORDER BY user.username
                              "
);

  
$moderators $DB_site->query("SELECT DISTINCT userid FROM moderator");
  while (
$mods $DB_site->fetch_array($moderators)) {
    
$mod[$mods[userid]] = 1;
  }

  
$count 0;
  while (
$user $DB_site->fetch_array($allusers)) {
    if (
$user['userid']) { // Reg'd Member
      
$key $user['userid'];
      if ((
$userinfo["$key"]['lastactivity'] < $user['lastactivity']) or !$userinfo["$key"]['lastactivity']) {
        
$userinfo["$key"]['realname'] = $user['username'];
        
      
$iconavatarurl=geticonavatarurl($user['userid']);
      if (
$iconavatarurl!="") {
        
$iconavatar "<img src=\"$iconavatarurl\" border=\"0\" alt=\"\">";
      } else {
        
$iconavatar "";
      }  
      if (
$bbuserinfo[userid]>and !($bbuserinfo[showiconavatars])) {
        
$iconavatar "";
      }       
        
        if (
$user['usergroupid'] == and $highlightadmin) {
          
$userinfo["$key"]['username'] = "$iconavatar<font color={whosonlineowner}><b><i>$user[username]</i></b></font>"
Note the function "geticonavatarurl" which looks like this:
PHP Code:
// ###################### Start geticonavatarurl #######################
function geticonavatarurl($userid) {
  global 
$DB_site,$session;

  if (
$iconavatarinfo=$DB_site->query_first("SELECT user.iconavatarid,iconavatarpath,NOT ISNULL(iconavatardata) AS hascustom,customiconavatar.dateline
                                         FROM user
                                         LEFT JOIN iconavatar ON iconavatar.iconavatarid=user.iconavatarid
                                         LEFT JOIN customiconavatar ON customiconavatar.userid=user.userid
                                         WHERE user.userid='
$userid'")) {
    if (
$iconavatarinfo[iconavatarpath]!="") {
      return 
$iconavatarinfo[iconavatarpath];
    } else if (
$iconavatarinfo['hascustom']) {
      return 
"iconavatar.php?userid=$userid&dateline=$iconavatarinfo[dateline]";
    } else {
      return 
'';
    }
  }

Is it possible to merge those 2 parts of code into 1 so it only needs 1 query to display the who's online instead of 1+ 1 for each icon. I tried some stuff myself but I'm really lost here.
Reply With Quote
  #2  
Old 02-18-2003, 11:32 PM
Xenon's Avatar
Xenon Xenon is offline
 
Join Date: Oct 2001
Location: Bavaria
Posts: 12,878
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

try this:
PHP Code:
$allusers$DB_site->query("SELECT user.username, user.iconavatarid,iconavatarpath,NOT ISNULL(iconavatardata) AS hascustom,customiconavatar.dateline,session.location, session.lastactivity, user.userid, user.usergroupid, user.invisible, session.host, user.showemail, user.receivepm
                              FROM session, user
                              LEFT JOIN iconavatar ON iconavatar.iconavatarid=user.iconavatarid
                                         LEFT JOIN customiconavatar ON customiconavatar.userid=user.userid
                                         
                              "
iif($WOLguests" LEFT JOIN user USING (userid) "",user") ."
                              WHERE session.lastactivity > 
$datecut
                              "
iif(!$WOLguests" AND session.userid = user.userid""") ."
                              ORDER BY user.username
                              "
);

  
$moderators $DB_site->query("SELECT DISTINCT userid FROM moderator");
  while (
$mods $DB_site->fetch_array($moderators)) {
    
$mod[$mods[userid]] = 1;
  }

  
$count 0;
  while (
$user $DB_site->fetch_array($allusers)) {
    if (
$user['userid']) { // Reg'd Member
      
$key $user['userid'];
      if ((
$userinfo["$key"]['lastactivity'] < $user['lastactivity']) or !$userinfo["$key"]['lastactivity']) {
        
$userinfo["$key"]['realname'] = $user['username'];
      if (
$user[iconavatarpath]!="") {
           
$iconavatarurl=$user[iconavatarpath];
} else if (
$user['hascustom']) {
           
$iconavatarurl="iconavatar.php?userid=$userid&dateline=$iconavatarinfo[dateline]";
} else {
$iconavatarurl='';
}
      if (
$iconavatarurl!="") {
        
$iconavatar "<img src=\"$iconavatarurl\" border=\"0\" alt=\"\">";
      } else {
        
$iconavatar "";
      }  
      if (
$bbuserinfo[userid]>and !($bbuserinfo[showiconavatars])) {
        
$iconavatar "";
      }       
        
        if (
$user['usergroupid'] == and $highlightadmin) {
          
$userinfo["$key"]['username'] = "$iconavatar<font color={whosonlineowner}><b><i>$user[username]</i></b></font>"
Reply With Quote
  #3  
Old 02-18-2003, 11:55 PM
Dark Jim's Avatar
Dark Jim Dark Jim is offline
 
Join Date: Apr 2002
Location: Netherlands
Posts: 174
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hmm I get a different error then when I tried something. This is what I get:
Quote:
Database error in vBulletin 2.3.0:

Invalid SQL: SELECT user.username, user.iconavatarid,iconavatarpath,NOT ISNULL(iconavatardata) AS hascustom,customiconavatar.dateline,session.locati on, session.lastactivity, user.userid, user.usergroupid, user.invisible, session.host, user.showemail, user.receivepm
FROM session, user
LEFT JOIN iconavatar ON iconavatar.iconavatarid=user.iconavatarid
LEFT JOIN customiconavatar ON customiconavatar.userid=user.userid

LEFT JOIN user USING (userid)
WHERE session.lastactivity > 1045618523

ORDER BY user.username

mysql error: Not unique table/alias: 'user'

mysql error number: 1066
Reply With Quote
  #4  
Old 02-19-2003, 10:54 AM
Xenon's Avatar
Xenon Xenon is offline
 
Join Date: Oct 2001
Location: Bavaria
Posts: 12,878
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

it's this line of your original script which pruduces the error:
PHP Code:
". iif($WOLguests, " LEFT JOIN user USING (userid", ",user") ." 
take it out..
Reply With Quote
  #5  
Old 02-19-2003, 07:46 PM
Dark Jim's Avatar
Dark Jim Dark Jim is offline
 
Join Date: Apr 2002
Location: Netherlands
Posts: 174
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Ack! That's no good. Suddenly all my members where online and all looking at the same forum. O_o Maybe I should add a query instead...
Reply With Quote
  #6  
Old 02-19-2003, 09:18 PM
Xenon's Avatar
Xenon Xenon is offline
 
Join Date: Oct 2001
Location: Bavaria
Posts: 12,878
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

change this:
PHP Code:
WHERE session.lastactivity $datecut 
into this:
PHP Code:
WHERE session.userid user.userid AND session.lastactivity $datecut 
Reply With Quote
  #7  
Old 02-19-2003, 11:37 PM
Dark Jim's Avatar
Dark Jim Dark Jim is offline
 
Join Date: Apr 2002
Location: Netherlands
Posts: 174
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks that works after I also changed
PHP Code:
$iconavatarurl="iconavatar.php?userid=$userid&dateline=$iconavatarinfo[dateline]"
to
PHP Code:
$iconavatarurl="iconavatar.php?userid=$user[userid]&dateline=$iconavatarinfo[dateline]"
Guests do not show up anymore though because of the removed line I assume. :/
Reply With Quote
  #8  
Old 02-22-2003, 01:38 PM
Dark Jim's Avatar
Dark Jim Dark Jim is offline
 
Join Date: Apr 2002
Location: Netherlands
Posts: 174
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I can't get it to show up guests. =/ Is it even possible with my hack included?
Reply With Quote
Reply

Thread Tools
Display Modes

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 07:47 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04117 seconds
  • Memory Usage 2,286KB
  • Queries Executed 13 (?)
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
  • (8)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (8)post_thanks_box
  • (8)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (8)post_thanks_postbit_info
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)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_postinfo_query
  • fetch_postinfo
  • 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
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete