Programming 101: How to Create Groupings

If you are new to programming, there may be certain solutions that escape you completely, depending on the problem you are trying to solve.  Sometimes it’s as simple as not knowing of a function or syntax.  Other times, it’s a little more complex because it involves logic.

What is Programming Logic?

Before we dive into code, I want to briefly explain what I mean when I say logicProgramming is essentially using code to creatively solve a problem.  Throughout your code could be a logical set of instructions that determines when to execute certain pieces. This is conditional logic.

Many times, the logic to solve a problem is pretty simple:

<?php 
if ( x == 2 ){
    echo 'This is the second record';  
}
?>

Using this kind of conditional logic is essential when you are learning how to program and, over time, will allow you to perform more complex statements.

What is a Grouping?

A grouping is where you sort a set of data into logical sections.  A great example of this is when you see an events calendar that is sorted by date.  There is a single heading for the date and each record that matches that date is listed underneath.

December 10, 2011

  • 12/10/2011 10:00 am – Mary’s Recital
  • 12/10/2011 12:00 pm – Bob’s Cookout

December 11, 2011

  • 12/11/2011 2:00 pm – Computer Science Meetup
  • 12/11/2011 5:00 pm – Art Festival

This sort of breakdown continues on until you get to the end of your data.

How do I make a Grouping automatically?

The above list is easy to create manually, but what do you do when you have hundreds or even thousands of events in a database?  Of course, that’s where programming is going to help us.

Here’s our pseudo code:

  • Query our events database – get all records for the current week
  • Loop through each record and print our information out to the screen
  • If this event’s date is not the same as the previous event’s date, print out our new date heading and continue

Here’s our actual code:

<!--?php<br /-->/* Setup our query - assuming we've already connected to our database */
$query = 'SELECT Title, StartDate, StartTime
FROM events
WHERE WEEK(StartDate) = "12-11-2011"
ORDER BY StartDate ASC, StartTime ASC';

$result = mysql_query( $query );

echo '
<ul>';

/* Loop through our events data */
while ( $row = mysql_fetch_array( $result ) ) {

    /* Assign each row to a variable */
    $title = $row['Title'];
    $date = $row['StartDate'];
    $time = $row['StartTime'];

    /* Handles the date grouping */
    if ( $last != $date ) {
        echo '
	<li class="date_heading">' . date( 'D M d, Y', strtotime( $date ) ) . '</li>
';

        $last = $date;
    }

    /* Output the event name and time below */
    echo '
	<li>' . $title . '-' . $time . '</li>
';
}

echo ';
?>

As you can see, with very little code, we can group our data. What’s even better is that the method can apply to any kind of grouping you need, not just dates! For example, you could have a directory of people and want to group alphabetically by last name (if ( $last != $last_name )). Or, you could have a blog archive page with categories grouped by name and the posts underneath (if ( $last != $category )). There are tons of possibilities.

If you plan to work with a lot of data that needs to be grouped into common headers, save this little snippet of code because it could come in handy.

About these ads