Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split records into two columns

Tags:

php

mysql

I have a "student" table, having around 5,000 records, in my DB. I want to display those records in two divs. How do I do that without executing the query twice; only using a single query?

display example http://www.freeimagehosting.net/uploads/f1c6bb41eb.gif

like image 293
Bharanikumar Avatar asked Jan 09 '10 08:01

Bharanikumar


2 Answers

Just use CSS3. Not sure how widely it is supported but saves a lot of headache and is a lot more powerful when making changes.

Use column-count, and column-width to control the number of columns and width of each column. Here's some sample code and some pretty impressive results. Prefix -webkit and -moz for now until its standardized across all browsers.

.multi-column {
    /* Standard */
    column-count: 2;
    column-width: 150px;
    /* Webkit-based */
    -webkit-column-count: 2;
    -webkit-column-width: 150px;
    /* Gecko-based */
    -moz-column-count: 2;
    -moz-column-width: 150px;
}

Applied to this <div>

<div class="multi-column">
    Ethelred of Wessex
    Louis XII of France
    George Frideric Handel
    George Washington
    Charles Deslandes
    Andrew Jackson
    Alfred Vail 
    William McKinley
    Woodrow Wilson
    Abdul-Aziz ibn Saud
    Fidel Castro
    Charles de Gaulle
    Leonardo da Vinci
</div>

Don't you wanna see how it looks like after all this hard work?

alt text


But what if there were 3 columns? No problem.

alt text


But there's no way it can handle 4 columns you'd say:

alt text


Enough! I gotta stop adding these now

alt text


God please make it STOP!!

alt text

like image 173
Anurag Avatar answered Oct 13 '22 01:10

Anurag


Just find where the "middle" is and output the end tag of the div tag and the start tag of the second div:

<? 
$rowcount = mysql_num_rows($recordset);
echo "<div id='div1'>";
$i = 0;
while ($d = mysql_fetch_object($recordset)) {
  echo $d->somefield;
  $i++;

  if ($i == floor($rowcount / 2)) {
      //we have reached the mid-point, let's close the first DIV
      echo "</div><div id='div2'>";
  }
}
echo "</div>";
?>
like image 22
naivists Avatar answered Oct 12 '22 23:10

naivists