mySQL numrows Query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rabman
Forum Newbie
Posts: 1
Joined: Thu Jun 25, 2009 11:15 am

mySQL numrows Query

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: mySQL numrows Query

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply