vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   efficient (https://vborg.vbsupport.ru/showthread.php?t=64994)

sabret00the 05-11-2004 12:45 PM

efficient
 
is this code efficient?

PHP Code:

                while ($project_info $DB_site->fetch_array($projects))
                 {
                     
$project_info['text'] = nl2br(stripslashes($project_info['text']));
                     
extract($project_info);
                     
$time_posted vbdate('n-j-y, g:i:s a'$timestamp);
                     
// selects the number of times each confeession was rated
                     
$number_of_rates $DB_site->query_first("SELECT COUNT(*) AS votes
                     FROM project_rate
                     WHERE projectid = 
$projectid
                     "
);
                     
$num_rates number_format($number_of_rates['votes']);
                     
// selects the username of the person who made the confeession
                     
$who_done_it $DB_site->query_first("SELECT username
                     FROM user
                     WHERE userid = 
$project_info[userid]
                     "
); 


NTLDR 05-11-2004 12:54 PM

You really need to get those two queries within the while loop removed.

sabret00the 05-11-2004 04:44 PM

Any suggestions on an alternative?

NTLDR 05-11-2004 04:55 PM

Whats the $projects query? Its possible it can be integrated with that.

sabret00the 05-11-2004 05:03 PM

projects is pulling the text out one by one
where as as i have the second table is the project_rate table where i store each rating the projct text gets so later as i display it, i'm hoping to display the number of ratings each project i had on the page

project
Code:

projectid            text            timestamp            userid
project_rate
Code:

rateid          projectid          userid          timestamp          rate

kinda like where on the index of TS3 you got the number of comments displayed along with each of your news items.

sabret00the 05-12-2004 09:03 AM

heres the exact query
PHP Code:

            $projects $DB_site->query("
                SELECT projectid, text, timestamp
                FROM projects
                
$clause
                ORDER BY projectid DESC
                
$limit
            "
); 

i think i may be able to do a left join (the user table to the projects table) to irradicate the last query, but i heard that left joins are bad?

Xenon 05-12-2004 09:58 AM

why should left joins be bad?

but here, it's suggest a inner join :)

PHP Code:

$projects $DB_site->query(
        SELECT COUNT(*) AS votes, project_rate.projectid, 
            projects.text, projects.timestamp, user.username
        FROM project_rate
        INNER JOIN projects ON (project_rate.projectid = projects.projectid)
        INNER JOIN user ON (user.userid = projects.userid)
        
$clause
        GROUP BY project_rate.projectid
        ORDER BY project_rate.projectid DESC 
        
$limit 
"
); 
while (
$project_info $DB_site->fetch_array($projects))
{
    
$project_info['text'] = nl2br(stripslashes($project_info['text']));
    
$project_info['num_rates'] = number_format($project_info['votes']);
    
extract($project_info);
    
$time_posted vbdate('n-j-y, g:i:s a'$timestamp); 

that should work i think

sabret00the 05-12-2004 10:14 AM

dunno just read in another thread that left joins would cripple any highly active server.

Xenon 05-12-2004 10:22 AM

nah, it depends on which tables you join.

for example thread and post table shouldn't be joined, as these are bother very big ones.
on the other hand the session table and the user table could always be joined, as the session table is normally a very small one.

also it's always better to join instead of looping a query a lot :)

sabret00the 05-12-2004 10:26 AM

oh ok, thanks for explaining that and i'm just gonna try out the code now :)

sabret00the 05-12-2004 10:43 AM

i'm not getting errors, but heres the block in it's entirety

PHP Code:

        // sorts projects for main page
              
$projects $DB_site->query("
                 SELECT COUNT(*) AS votes, project_rate.projectid,
                 projects.text, projects.timestamp, user.username
                  FROM project_rate
                 INNER JOIN projects ON (project_rate.projectid = projects.projectid)
                 INNER JOIN user ON (user.userid = projects.userid)
                  
$clause
                  GROUP BY project_rate.projectid
                 ORDER BY project_rate.projectid DESC
                  
$limit 
              "
);
  
              if (!
$DB_site->num_rows($projects))
              {
                 eval(
print_standard_error('confessions_error'));
              }
              else
              {
                 
$totalconf number_format($DB_site->num_rows($projects));
                 while (
$project_info $DB_site->fetch_array($projects))
                  {
                     
$project_info['text'] = nl2br(stripslashes($project_info['text']));
                     
$project_info['num_rates'] = number_format($project_info['votes']);
                     
extract($project_info);
                     
$time_posted vbdate('n-j-y, g:i:s a'$timestamp); 
          eval(
'$projects_bits .= "' fetch_template("projects_bit") . '";');
                  } 
              } 

i dunno what i've done, but for some reason the page has stopped echoing out the $projects_bit template

sabret00the 05-12-2004 10:52 AM

actually now i'm getting the num_rows error, so i ran the query in phpmyadmin and it's not returning anydata, i 'm not quite sure how to fix it?

Xenon 05-12-2004 01:07 PM

the query will just return something, if there are votes in the DB i think.

so it may be needed to add one dummy vote for each project.

sabret00the 05-12-2004 01:09 PM

ok, i'll play with it :)

as it's not even returning results with votes.


All times are GMT. The time now is 05:49 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
  • Page Generation 0.05715 seconds
  • Memory Usage 1,773KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_code_printable
  • (4)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (14)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete