Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting a PHP Array into Columns

Tags:

arrays

php

mysql

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

like image 571
Voodoo Avatar asked Aug 07 '10 01:08

Voodoo


People also ask

How do you sort an array in PHP?

PHP - Sort Functions For Arrayssort() - sort arrays in ascending order. rsort() - sort arrays in descending order. asort() - sort associative arrays in ascending order, according to the value. ksort() - sort associative arrays in ascending order, according to the key.

How can we sort an array without using sort method in PHP?

php function sortArray() { $inputArray = array(8, 2, 7, 4, 5); $outArray = array(); for($x=1; $x<=100; $x++) { if (in_array($x, $inputArray)) { array_push($outArray, $x); } } return $outArray; } $sortArray = sortArray(); foreach ($sortArray as $value) { echo $value . "<br />"; } ?>

How do I sort a 2d array in PHP?

Use the usort() function to sort the array. The usort() function is PHP builtin function that sorts a given array using user-defined comparison function. This function assigns new integral keys starting from zero to array elements.

How does Usort work in PHP?

The usort() function in PHP sorts a given array by using a user-defined comparison function. This function is useful in case if we want to sort the array in a new manner. This function assigns new integral keys starting from zero to the elements present in the array and the old keys are lost.


1 Answers

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!

like image 156
Frankie Avatar answered Oct 16 '22 01:10

Frankie