I have a question about array_chunk.
I have a list of about a thousand people’s names in a table named “people,” and the numerical key (field N) is arranged alphabetically by last name, like this…
N | URL
1 | Edward_Abbey
2 | John_Adams
[Intermediate Values]
1000 | Howard_Zinn
If I order my query by the numerical key (P.N), it then displays an alphabetized list of people in a single column. However, I’d like to display it in multiple columns of approximately the same height.
So I tried the array_chunk feature, and it basically works, but there are two problems. First, the alphabetical order is kind of weird. The first column begins with Edward_Abbey and ends with Henry_Wallace. The second column more or less starts over, beginning with Jack_Abramoff and ending with William_Wallace.
Instead, the second column should start where the first column ends; if the last value in the first column is Chuck_Henderson the first value in the second column should be Linda_Henry.
The second problem is that I can’t seem to dictate the number of columns with $how_many_chunks. At first, it displayed three columns, even if the value was $how_many_chunks = 2. Now it’s displaying four columns, regardless of the value I type in.
Can anyone tell me what’s wrong with my code? I’m new to PDO, so I could have made a mistake there. However, the select statement works fine when I paste it into phpMyAdmin > SQL.
Thanks.
$stm = $pdo->prepare("SELECT P.URL, P.Title, P.Site, P.Live,
PB.Common, ART.URL, ART.Site, ART.Brief, ART.Article
FROM people P
LEFT JOIN people_1_bio PB ON PB.URL = P.URL
LEFT JOIN people_articles ART ON ART.URL = P.URL
WHERE P.Site = 'PX' AND ART.Site = 'PX' AND ART.Brief !='' AND P.Live = 1
ORDER BY P.N");
$stm->execute(array(
));
while ($row = $stm->fetch())
{
$Common = $row['Common'];
$Roster[] = '<a href="/People/'.$URL.'">'.$Common.'</a><br>';
}
// Set how many chunks you want to break the array into.
$how_many_chunks = 2;
// Break the '$Roster' array into '$roster_chunks'
$roster_chunks = array_chunk($Roster, $how_many_chunks, true);
// Roll through the '$roster_chunks'
foreach ($roster_chunks as $roster_chunk_key => $roster_chunk_value) {
echo '<div style="width: 175px; font-size: 75%; text-align: left; float: left; margin-right: 15px;">';
echo join($roster_chunk_value, '' );
echo '</div>';
}