Page 1 of 1

mySQL numrows Query

Posted: Thu Jun 25, 2009 11:19 am
by rabman
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi,

I am pretty new to PHP and am developing a PHP script to show stats for how many users have signed up on my site, posted articles and comments.
The following code does what I need, but is very long-winded. I would really appreciate if anyone has suggestions on how to cut it down.

Code: Select all

 
<?php
//Required Functions
function intra_pageheaders(){
    ?>
        
    <?php
}
 
function intra_pageContent(){
    global $intra_moduleId,$cols,$tid;
    $message="";
    doOps();
    $tid=1;
    ?>
        <h1>Website User Statistics</h1>
        <div id="box80">
        <?php tablePass(); ?>
        </div>
 
<?php
}
//Independant Functions
 
//Articles
function tablePass(){
    global $intra_moduleId,$xdb_mod,$xdb,$table,$cols,$tid;
    $num_nu_june2009 = GetNewUsersJune2009();
    $num_nu_may2009 = GetNewUsersMay2009();
    echo '<table width="100%"  border="0" cellspacing="0px" cellpadding="2px">';
        echo "<tr class='glow' style='font-weight:bold;'><td>Month</td><td>New Users</td><td>New Articles</td><td>New Comments</td></tr>";
        echo "<tr class='glow'><td>June 2009</td><td>".$num_nu_june2009."</td><td></td><td></td></tr>";
        echo "<tr class='glow'><td>May 2009</td><td>".$num_nu_may2009."</td><td></td><td></td></tr>";
        echo "<tr class='glow'><td>April 2009</td><td></td><td></td><td></td></tr>";
        echo "<tr class='glow'><td>March 2009</td><td></td><td></td><td></td></tr>";
        echo "<tr class='glow'><td>February 2009</td><td></td><td></td><td></td></tr>";
        echo "<tr class='glow'><td>January 2009</td><td></td><td></td><td></td></tr>";
    echo '</table>';
 
}
 
function GetNewUsersJune2009() {
    global $intra_moduleId,$xdb_mod,$table;
    $sql_nu_june2009="SELECT * FROM `site_users` WHERE `joined` <= '2009-06-01 00:00:00' AND `joined` >= '2009-05-1 00:00:00'";
    $new_users_june2009=@mysql_query($sql_nu_june2009);
    $num_nu_june2009=mysql_numrows($new_users_june2009);
    return $num_nu_june2009;
}
function GetNewUsersMay2009() {
    $sql_nu_may2009="SELECT * FROM `site_users` WHERE `joined` <= '2009-05-01 00:00:00' AND `joined` >= '2009-04-1 00:00:00'";
    $new_users_may2009=@mysql_query($sql_nu_may2009);
    $num_nu_may2009=mysql_numrows($new_users_may2009);
    return $num_nu_may2009; 
}
 
?>
 
Thanks, Rob.


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: mySQL numrows Query

Posted: Thu Jun 25, 2009 2:25 pm
by pickle
Don't write a separate function for each month - that'll be hell to maintain. Instead write a generic function that accepts either a date range (more flexible) or a month and year - then returns the number of users that connected in that time period. Call that function from a loop in tablePass() to dynamically show the number of users that joined in the past 3, 4, 7, 24, or however many months you care about.