6

I have a mysql table that includes the fields:

Name - Year - Description

I'd like to display in order by year, but want to split into to columns by decade. My PHP skills are pretty weak, so I only know how to do it the long way where I make a separate query based on the year range:

<?php
echo "<div><h3>1950</h3>";
    $list1950 = mysql_query("SELECT * FROM people WHERE class_year1 > '1949' AND class_year1 < '1960' ORDER BY class_year1, last_name",$db);

    while ($thearray = mysql_fetch_array($list1950)) {
        echo "<div>$thearray[name] - $thearray[class_year1]<br />$thearray[description]</div>"; 
    }
echo "</div>";
echo "<h3>1960</h3><div>";
    $list1960 = mysql_query("SELECT * FROM people WHERE class_year1 > '1959' AND class_year1 < '1970' ORDER BY class_year1, last_name",$db);

    while ($thearray = mysql_fetch_array($list1960)) {
        echo "<div>$thearray[name] - $thearray[class_year1]<br />$thearray[description]</div>"; 
    }
echo "</div>";
?>

I know there's an easy/more efficient way to do this. Any help?

thanks

1 Answer 1

5

I would do something like this:

$list = mysql_query("SELECT * FROM people ORDER BY class_year1, last_name",$db);

$decade = false;
while ($thearray = mysql_fetch_array($list)) {

    // checks if decade is diferent, if so updates and prints it
    if( $decade != substr($thearray['class_year'], 2, 1) ) {
        // every time we change decade we print a DIV
        if($decade!==false) echo "</div>";

        $decade = substr($thearray['class_year'], 2, 1);
        echo "<div><h3>19".$decade."0</h3>";
    }

    // prints info for each row
    echo "<div>".$thearray['name']." - ".$thearray['class_year1']."<br />".$thearray['description']."</div>"; 

}

// we print another DIV in the end to close it right
echo "</div>";

This way you can easily update the function to show 1800's and 2000's decades and you don't have to hard-code it all the way.

Hope it helps!

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the response. I substr to use ['class_year'], 2, 1 which nows makes it grab the decade. But I realized there needs to be a </div> after all the entries that fall in a decade. What's the best way to do that?
@Voodoo updated code. Let me know if it works as intended. It's not very pretty. Ideally you would not have to encapsulate all entries in a DIV...
Thanks Frankie. This update works. Though I did have to keep the substr at 2, 1 (rather than 3, 1 as you wrote) to grab the correct year digit. Thanks again. And yes, now that I have it working I should switch from <div>s to a <li> approach.
@Voodoo glad it works! Thank you for the correction! I did it by heart and effectively missed the range! :) Edited the post accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.