Home > Thinking out loud > Heck Yeah! Code can be fun sometimes!

Heck Yeah! Code can be fun sometimes!

April 16th, 2008

I was working on some coding for a statistics page for a website I’m running. I needed to get the number of titles in the library rated between a certain number and split by category (Fiction, Poetry, Non-Fiction).

Back in the day I would write out the query five times and assign variables for all the statistics but not today. Today I had a vision and it was code sweet!

After brushing up on arrays for a few minutes I harnessed the power of arrays and wrote out this code. (See if you can follow the logic flow. 8^)

function count_number_titles_in_rating_range_and_by_category() {
  global $database_connection;

  /*
  //– Written here for reference
  $titles_by_category = array(
                                    rated_6  => array(f=>”",nf=>”",p=>”",t=>”"),
                                    rated_7  => array(f=>”",nf=>”",p=>”",t=>”"),
                                    rated_8  => array(f=>”",nf=>”",p=>”",t=>”"),
                                    rated_9  => array(f=>”",nf=>”",p=>”",t=>”"),
                                    rated_10 => array(f=>”",nf=>”",p=>”",t=>”")
                                    ); // multi-dimensional array for title ratings by category and TOTAL(t)
  */ 

//– Loop as long as $i is less than 11
  for ($i=6;$i<11;$i++) {
    $query = “SELECT COUNT(tr.titleid),fl.category FROM title_rating as tr, files_in_library as fl WHERE fl.titleid=tr.titleid AND title_rating BETWEEN ‘$i’ AND ‘$i.99′ GROUP BY fl.category”;

    if ($i == “10″) {
       $query = “SELECT COUNT(pr.publishid),pf.categoryid FROM title_rating as tr, files_in_library as fl WHERE fl.publishid=tr.publishid AND tr.rating=’10′ GROUP BY fl.categoryid”;
    } // end check for the last value of 10

    $result = mysql_query($query);

    while($row = mysql_fetch_array($result)) {
      ($row['category'] == “F”) && ($f_count  = $row['COUNT(tr.titleid)']);
      ($row['category'] == “NF”) && ($nf_count  = $row['COUNT(tr.titleid)']);
      ($row['category'] == “P”) && ($p_count  = $row['COUNT(tr.titleid)']);

        $t_count = $f_count + $nf_count + $p_count; // get the TOTAL
        $rated_number = “rated_$i”;
        $rated_titles_by_category[$rated_number][f] = $f_count;
        $rated_titles_by_category[$rated_number][nf] = $nf_count;
        $rated_titles_by_category[$rated_number][p] = $p_count;
        $rated_titles_by_category[$rated_number][t] = $t_count;
    } // while loop

  } // end for loop

  return($titles_rated_by_category);
} // end count_number_titles_in_rating_range_and_by_category()

Notice that $i is a variable that continually rises to give me each of the MYSQL query selects needed to get the values in the database. I also use the ever increasing $i to reference the postion in the multi-dimensional array to set the correct values as they are being pulled from the data base. No if ($i == “6″) { do the titles rated 6 and above }, if ($i == “7″) { do the titles rated 7 and above}, etc etc, checking. It’s all automated. 

In 37 lines I wrote out an MYSQL query that pulls 15 different values and puts them into a multi-dimensional array that can be passed as a SINGLE object. That’s the beauty of this code.  Otherwise that last return would have looked something like this:

return(array($f_rated_6,$p_rated_6,$nf_rated_6,$t_rated_6, $f_rated_7,$p_rated_7,$nf_rated_7,$t_rated_7, $f_rated_8,$p_rated_8,$nf_rated_8,$t_rated_8, $f_rated_9,$p_rated_9,$nf_rated_9,$t_rated_9, $f_rated_10,$p_rated_10,$nf_rated_10,$t_rated_10));

And as an added bonus it only added 0.005 seconds to my overall code processing time. Statistics pages can be load heavy. So far I’m at 0.076 seconds for 62 unique statistical values pulled from the DB.

Ok. This is the geekest post ever. :D
PS( All tables, databases, variables and values have been modified from their original namings to protect the innocent - ie my server. :? Don’t be evil. Hacking is bad. M’kay.)

Don't keep it to yourself:
  • StumbleUpon
  • Reddit
  • Digg
  • Blogosphere News
  • Sphinn
  • del.icio.us
  • Facebook

Thinking out loud

  1. April 17th, 2008 at 23:39 | #1

    Man! That code is downright SEXY! :)

    The Irredeemable Shag
    http://onceuponageek.com

  1. No trackbacks yet.
totes handbags leather handbags totes tote style handbags low price handbag tote purse marc by marc jacobs miss marc tote handbag deals gwen stefani handbags tote hello kitty deluxe handbag clutch rhinestone tote brighton tote handbags quilted totes and handbags prada inspired tote handbags tanya lee handbag tote juicy couture handbag tote prada large tote handbag new prada purse handbag large tote black leather br1594 how to make handmade totes and handbags boston terrier handbags and totes discount purse handbag tote gucci handbags tote vachetta small tote handbag michael kors brookville black handbag shopper tote bag large totes and handbags maxx handbag crocodile tote laura handbag tote cole haan handbag frances tote discounted brand name womens handbags totes brand name womens handbags totes sharif handbags tote coach handbag patent tote f11500 maggi b handbags and totes tiny tote miniature handbags handbag tote symbols nwt tommy hilfiger patchwork madras plaid handbag tote christian dior handbags 44019 blue logo large boston tote bag cloth baseball handbag and purse tote cloth purses handbags totes handbags and totes animal print handbags and totes big inexpensive handbags tote vintage cole haan lunch bucket tote handbag carlos falchi zebra printer handbag tote ed hardy romy panther large tote handbag love ed hardy romy pink large tote handbag love womens tote handbag maxx new york black microfiber handbag tote virginia tech handbags and totes chinese laundry tote handbags gucci white ostrich tote handbag purses handbags totes in yellow yoke cross shoulder tote handbags handbags totes lucky brand quilted totes cat handbags cat handbags totes