Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 General Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 07-06-2015, 05:46 PM
KGodel's Avatar
KGodel KGodel is offline
 
Join Date: May 2011
Location: Indiana
Posts: 332
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Using the vB Database Class

I am having strange issues with the vB_Database class. I have several custom pages which I use through the BF Easypages plugin (and some not) and I get different results.

When I add a tool to the ACP, it seems that most of the DB class functions work fine, however in my page, for instance, the following work:

PHP Code:
$vbulletin->db->query_read()
$vbulletin->db->query_read_slave() 
but the following won't work:

PHP Code:
$vbulletin->db->fetch_array() 
This leaves me to use the following to get the information, but I'd like to switch all of my code completely over to use the vB Database class so that I can upgrade our PHP version without causing a bunch of deprication issues.

PHP Code:
mysql_fetch_array() 
After reading the documentation, there don't seem to be rules governing what should and shouldn't work. Is it perhaps that I cannot run multiple queries this way before fetching the results? I'm left very confused. Is there an article that details the use of the various class functions?

Page code in question listed below:

PHP Code:
// Check for User ID
$userid filter_input(INPUT_GET'userid'FILTER_VALIDATE_INT);
if (
$userid == null) {
    
$userid $vbulletin->userinfo[userid];
}
// Check for Missing in Action Roster
// Page Start //
require_once(DIR '/custom/functions.php');
page_header("Achievement Point Helper");

// Define Usergroup Levels
$grouplevels = array();
// Group ID, Required Points, Rank Image Source
$grouplevels[] = array(353"<img src='images/ranks/v9/mem_stea.png' alt='Steadfast Member' />""Steadfast Member");
$grouplevels[] = array(518"<img src='images/ranks/v9/mem_seni.png' alt='Senior Member' />""Senior Member");
$grouplevels[] = array(5215"<img src='images/ranks/v9/mem_reno.png' alt='Renowned Member' />""Renowned Member");
$grouplevels[] = array(5330"<img src='images/ranks/v9/mem_visi.png' alt='Visionary Member' />""Visionary Member");

// Define Basic Membergroups
$membergroups = array(235515253);
$leadergroups = array(56171824385054555658);

echo 
"
              <table width='100%'>
                <tbody>
                  <tr>
                    <td>"
;

if (
$userid != null) {

    
// Query DB for Post Content //
    
$threads_q $vbulletin->db->query_read("SELECT COUNT(*) FROM " TABLE_PREFIX "thread WHERE postuserid = $userid AND forumid NOT IN (13, 19, 28, 43, 47, 49, 71, 86, 126, 141,162, 167, 168, 178, 180, 181, 219, 222, 233, 237, 238, 239, 243, 254, 255, 256, 259, 267, 284, 285, 287, 301, 304, 306, 307, 312, 313, 314, 316, 319, 337, 338, 345, 346, 375, 381, 384, 385, 391, 396, 402)");
    
$recruits_q $vbulletin->db->query_read("SELECT COUNT(*) FROM " TABLE_PREFIX "user WHERE referrerid = $userid");
    
$awards_q $vbulletin->db->query_read("SELECT COUNT(*) FROM " TABLE_PREFIX "award_user WHERE userid = $userid AND award_id NOT IN (6, 7, 10, 20, 21, 23, 24, 25)");
    
$info_q $vbulletin->db->query_read("SELECT user.username, user.posts, user.joindate, user.usergroupid, user.membergroupids, user.iga_points, userfield.field49, userfield.field59 FROM " TABLE_PREFIX "user as user LEFT JOIN " TABLE_PREFIX "userfield as userfield ON (userfield.userid=user.userid) WHERE user.userid = $userid");


    
// Transfer to Arrays
    
$u_threads mysql_fetch_array($threads_q);
    
$u_recruits mysql_fetch_array($recruits_q);
    
$u_awards mysql_fetch_array($awards_q);
    
$u_info mysql_fetch_array($info_q);

    
// Get Counts
    
$username $u_info[0];
    if (
$username == null) {
        goto 
error;
    }
    
$posts $u_info[1];
    
$joindate = new DateTime(date("Y-m-d"$u_info[2]));
    
$currentdate = new DateTime();
    
$displaydate vbdate($vbulletin->options['dateformat'], $u_info[2], true);
    
//date
    
$datediff $currentdate->diff($joindate);
    
$datediffm $datediff->format('%m');
    
$datediffy $datediff->format('%y');
    
$datediff $datediffm + (12 $datediffy);
    
//end date
    
$usergroup $u_info[3];
    
$othergroups $u_info[4];
    
$igapoints $u_info[5];
    
$staffyesno $u_info[6];
    
$permvmyesno $u_info[7];
    
$threads $u_threads[0];
    
$recruits $u_recruits[0];
    
$awards $u_awards[0];
    
$totalpoints 0;

    
// Default Messages //
    
$nopoints "<font color='red'>No Points</font>";

    
// Set Points Output //
    // Post Points
    
$p10 $p25 $p50 $p100 $p250 $p500 $nopoints;

    if (
$posts >= 10) {
        
$p10 "<font color='green'>1 Point</font>";
        
$totalpoints += 1;
    }
    if (
$posts >= 25) {
        
$p25 "<font color='green'>1 Point</font>";
        
$totalpoints += 1;
    }
    if (
$posts >= 50) {
        
$p50 "<font color='green'>1 Point</font>";
        
$totalpoints += 1;
    }
    if (
$posts >= 100) {
        
$p100 "<font color='green'>2 Point</font>";
        
$totalpoints += 2;
    }
    if (
$posts >= 250) {
        
$p250 "<font color='green'>2 Points</font>";
        
$totalpoints += 5;
    }
    if (
$posts >= 500) {
        
$totalpoints += 3;
        
$p500 "<font color='green'>3 Points</font>";
    }

    
// Thread Points
    
$t10 $t20 $t50 $t100 $nopoints;

    if (
$threads >= 10) {
        
$t10 "<font color='green'>1 Point</font>";
        
$totalpoints += 1;
    }
    if (
$threads >= 20) {
        
$t20 "<font color='green'>1 Point</font>";
        
$totalpoints += 1;
    }
    if (
$threads >= 50) {
        
$t50 "<font color='green'>2 Points</font>";
        
$totalpoints += 2;
    }
    if (
$threads >= 100) {
        
$totalpoints += 3;
        
$t100 "<font color='green'>3 Points</font>";
    }

    
// Recruited Member Points
    
$r4 $r5 $nopoints;

    if (
$recruits >= && $recruits <= 4) {
        
$r4 "<font color='green'>$recruits Point(s)</font>";
        
$totalpoints += $recruits;
    } elseif (
$recruits >= 5) {
        
$r5points floor($recruits 5);
        
$r4 "<font color='green'>4 Points</font>";
        
$r5 "<font color='green'>$r5points Points</font>";
        
$totalpoints += ($r5points);
    }

    
// Membership Length Points
    
$m1 $m3 $m12 $nopoints;

    if (
$datediff >= 1) {
        
$m1 "<font color='green'>1 Point</font>";
        
$totalpoints += 1;
    }
    if (
$datediff >= 3) {
        
$m3 "<font color='green'>1 Point</font>";
        
$totalpoints += 1;
    }
    if (
$datediff >= 12) {
        
$m12 "<font color='green'>5 Points</font>";
        
$totalpoints += 5;
    }

    
// Award Points
    
$a $nopoints;

    if (
$awards 0) {
        
$apoints $awards;
        
$totalpoints += $apoints;
        
$a "<font color='green'>$apoints Points</font>";
    }

    
// Points for Positions
    
$staffpts $nopoints;

    if (
$staffyesno == "Yes") {
        
$staffpts "<font color='green'>5 Points</font>";
        
$totalpoints += 5;
    }
    
    
$igp 0;
    
$igpts $nopoints;
    
    
// Points for In-Game Activity
    
if ($igapoints >= 50) {
        
$igp += 1;
    }
    if (
$igapoints >= 100) {
        
$igp += 1;
    }
    if (
$igapoints >= 150) {
        
$igp += 1;
    }
    if (
$igapoints >= 200) {
        
$igp += 1;
    }
    if (
$igapoints >= 250) {
        
$igp += 1;
    }
    if (
$igapoints >= 300) {
        
$igp += 1;
    }
    if (
$igapoints >= 350) {
        
$igp += 1;
    }
    if (
$igapoints == 400) {
        
$igp += 1;
    }
    if (
$igp 0) { $totalpoints += $igp$igpts "<font color='green'>$igp Point(s)"; }

    
// End of Points //

    
echo "<p><strong>Username</strong>: $username <br/>
                 <strong>Threads</strong>: 
$threads <br/>
                 <strong>Posts</strong>: 
$posts<br/>
                 <strong>Recruits</strong>: 
$recruits <br/>
                 <strong>Join Date (mm-dd-YYYY)</strong>: 
$displaydate <br/>
                 <strong>Nominated Medals</strong>: 
$awards </p>";

    
// Begin Table //
    
echo "<table width='100%' border='0' cellspacing='2' cellpadding='0'>
                  <thead>
                    <tr>
                        <td width='50%' align='center'><strong>Achievement</strong></td>
                        <td width='50%' align='center'><strong>Points Available</strong></td>
                    </tr>
                  </thead>
                  <tfoot>
                    <tr>
                        <td><strong>Total Points</strong></td>
                        <td><font color='green'><strong>
$totalpoints Points</strong></font></td>
                    </tr>
                  </tfoot>
                  <tbody>
                  <tr>
                    <td>(1) Reaching 10 posts on the forums</td>
                    <td>
$p10</td>
                  </tr>
                  <tr>
                    <td>(1) Reaching 25 posts on the forums</td>
                    <td>
$p25</td>
                  </tr>
                  <tr>
                    <td>(1) Reaching 50 posts on the forums</td>
                    <td>
$p50</td>
                  </tr>
                  <tr>
                    <td>(2) Reaching 100 posts on the forums</td>
                    <td>
$p100</td>
                  </tr>
                  <tr>
                    <td>(2) Reaching 250 posts on the forums</td>
                    <td>
$p250</td>
                  </tr>
                  <tr>
                    <td>(3) Reaching 500 posts on the forums</td>
                    <td>
$p500</td>
                  </tr>
                  <tr>
                    <td>(1) Reaching 10 threads on the forums</td>
                    <td>
$t10</td>
                  </tr>
                  <tr>
                    <td>(1) Reaching 20 threads on the forums</td>
                    <td>
$t20</td>
                  </tr>
                  <tr>
                    <td>(2) Reaching 50 threads on the forums</td>
                    <td>
$t50</td>
                  </tr>
                  <tr>
                    <td>(3) Reaching 100 threads on the forums</td>
                    <td>
$t100</td>
                  </tr>
                  <tr>
                    <td>(1) Point for each member recruited (up to 4)</td>
                    <td>
$r4</td>
                  </tr>
                  <tr>
                    <td>(1) Point for each 5 new members recruited</td>
                    <td>
$r5</td>
                  </tr>
                  <tr>
                    <td>(1) Membership for 1 month</td>
                    <td>
$m1</td>
                  </tr>
                  <tr>
                    <td>(1) Membership for 3 months</td>
                    <td>
$m3</td>
                  </tr>
                  <tr>
                    <td>(5) Membership for 1 year</td>
                    <td>
$m12</td>
                  </tr>
                  <tr>
                    <td>(2) Points for each nominated medal awarded on the forums</td>
                    <td>
$a</td>
                  </tr>
                                  <tr>
                    <td>(5) Points for holding a staff position</td>
                    <td>
$staffpts</td>
                  </tr>
                                  <tr>
                    <td>(1) Point for each 50 in-game activity points</td>
                    <td>
$igpts</td>
                  </tr>
                  </tbody>
            </table>"
;

    echo 
"<p><font color='red'><strong>NOTE</strong></font>: This is not a complete list of your points. Some points, such as points for being a leader or administrator,
                   holding an rank in an in-game guild, points for playing with others divisions and groups, and points for division activity must be counted manually. You may
                   also lose points for any Justice Tickets on record.</p>"
;

    
// If Viewing your own stats, Check if you can level up
    
if ($userid == $vbulletin->userinfo[userid]) {

        
$newgroup $newrank $newranktitle "";

        
// If they are not in leadership
        
if (in_array($usergroup$membergroups)) {
            
// Go Through Each Rank and Check if They Can Achieve It
            
foreach ($grouplevels as $level) {
                
$groupid $level[0];
                
$reqpoints $level[1];
                
$rankcode $level[2];
                
$ranktitle $level[3];

                
/// If they have points to rank up
                
if ($totalpoints >= $reqpoints && $usergroup $groupid) {
                    if (
$groupid == 53) {
                        if (
$igp >= 2) {
                            
$newgroup $groupid;
                            
$newrank $rankcode;
                            
$newranktitle $ranktitle;
                            
$log = array($newgroup'P'$newranktitle$username);
                        }
                    } else {
                        
$newgroup $groupid;
                        
$newrank $rankcode;
                        
$newranktitle $ranktitle;
                        
$log = array($newgroup'P'$newranktitle$username);
                    }           
                }
            }
            
            
//Check for Visionary Member Decay
            
if ($usergroup == 53 && $igp && $permvmyesno == "No") {
                
$newgroup 52;
                
$newrank "<img src='images/ranks/v9/mem_reno.png' alt='Renowned Member' />";
                
$newranktitle "Renowned Member";
                
$log = array($newgroup'D'$newranktitle$username);
            }

            
// If they earned a new usergroup, change it
            
if ($newgroup != null) {
                
$userdata = & datamanager_init('User'$vbulletinERRTYPE_ARRAY);
                
$userdata->set_existing($vbulletin->userinfo);
                
$userdata->setr('usergroupid'$newgroup);
                
$userdata->setr('usertitle'$newrank);
                
// Set permanent VM if they have earned it via activity points.
                
if ($newgroup == 53 && $igp == 8) {
                        
$userdata->setr('field59''Yes');
                        
$userdata->setr('customtitle'0);
                }
                
$userdata->save();
                
                
// the "1" indicates to use the second line of the phrase specified for this task
                
log_cron_action(serialize($log), $nextitem1);

                
//$db->query_write("UPDATE " . TABLE_PREFIX . "user SET usergroupid = $newgroup, usertitle = $newrank WHERE userid = $userid");
                
echo "<p><img src='images/important.png' alt='Important!'/> You qualified for a new rank! Your rank is now $newranktitle! Congratulations</p>";
            }
        }
        
        
// If they are in leadership, check additional groups
        
if (in_array($usergroup$leadergroups)) {
            
// If they are in leadership, check additional groups
            // If no additional usergroup, set apprentice
            
if ($othergroups == null) {
                
$othergroups "2";
            }

            foreach (
$membergroups as $gid) {
                
$group = (string) $gid;
                if (
in_array($groupexplode(",",$othergroups)) !== false) {
                    
$secgroup $group;
                    
$secgroupid $gid;
                    break;
                }
            }


            
// Go Through Each Rank and Check if They Can Achieve It
            
foreach ($grouplevels as $level) {
                
$groupid $level[0];
                
$reqpoints $level[1];
                
$ranktitle $level[3];

                
// If they have points to rank up
                
if ($totalpoints >= $reqpoints && $secgroupid $groupid) {

                    if (
$groupid == 53) {
                        if (
$igp >= 2) {
                            
$newgroup = (string) $groupid;
                            
$newranktitle $ranktitle;
                            
$log = array($newgroup'S'$newranktitle$username);
                        }
                    } else {
                        
$newgroup = (string) $groupid;
                        
$newranktitle $ranktitle;
                        
$log = array($newgroup'S'$newranktitle$username);
                    }           
                }
            }

            
$othergroups str_replace($secgroup$newgroup$othergroups);

            
// If they earned a new usergroup, change it
            
if ($newgroup != null) {
                
$userdata = & datamanager_init('User'$vbulletinERRTYPE_ARRAY);
                
$userdata->set_existing($vbulletin->userinfo);
                
$userdata->setr('membergroupids'$othergroups);
                
// Set permanent VM if they have earned it via activity points.
                
if ($newgroup == 53 && $igp == 8) {
                        
$userdata->setr('field59''Yes');
                }
                
$userdata->save();
                
// the "1" indicates to use the second line of the phrase specified for this task
                
log_cron_action(serialize($log), $nextitem1);

                
//$db->query_write("UPDATE " . TABLE_PREFIX . "user SET usergroupid = $newgroup, usertitle = $newrank WHERE userid = $userid");
                
echo "<p><img src='images/important.png' alt='Important!'/> You qualified for a new rank! Your rank is now $newranktitle! Congratulations</p>";
                echo 
"<p>2nd Group: $secgroup and NewGroup = $groupid<p/>";
                echo 
$othergroups;
            }
        }
    }
} else {

    
error:
    echo 
"<p>Please enter a valid UserID.</p>";
}

// Footer
page_footer(); 
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 01:15 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.14378 seconds
  • Memory Usage 2,515KB
  • 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
  • (4)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)post_thanks_box
  • (1)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit_info
  • (1)postbit
  • (1)postbit_onlinestatus
  • (1)postbit_wrapper
  • (1)showthread_list
  • (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_threadedmode.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_threaded
  • showthread_threaded_construct_link
  • 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