Divide records into two columns

I have a student table that has about 5,000 entries in my database. I want to show these entries in two divs. How can I do this without executing the request twice; using only one request?

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

+4
source share
8 answers

Just find where the “middle” is and print 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 close the first DIV echo "</div><div id='div2'>"; } } echo "</div>"; ?> 
+7
source

Just use CSS3. Not sure how widely supported , but it will save a lot of headache and is much more effective in making changes.

Use column-count and column-width to control the number of columns and the width of each column. Here is a sample code and some impressive results. The prefix -webkit and -moz is still standardized in 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 want to see how it looks after all this hard work?

alt text http://i49.tinypic.com/e00m02.png


But what if there were 3 columns? No problems.

alt text http://i46.tinypic.com/2gy3fo6.png


But it cannot handle 4 columns that you would say:

alt text http://i50.tinypic.com/2w3dez8.png


Enough! I do not want to add them now

alt text http://i50.tinypic.com/2mn3p5s.png


God please stop!

alt text http://i49.tinypic.com/311pguq.png

+8
source

get_column() can help, it will compute a column for each element that you want to show.

this example script shows how to print in two columns, but you can change it in two minutes to fix your needs if you need a different number of columns.

 <?php // sample query, get members $query = mysql_query("select name from persons"); // count total number of items to show $total = mysql_num_rows($query); // initiate row counter $counter = 1; // initiate columns contents $column_1 = ''; $column_2 = ''; while($row = mysql_fetch_assoc($query)){ // caluculate column for current element from total items to be showed number of columns and current item $column = get_column($total, 2, $counter); if($column == 1){ $column_1 .= $row['name'].'<br>'; } if($column == 2){ $column_2 .= $row['name'].'<br>'; } $counter++; } // show content in two table comments echo "<table> <tr> <td>$column_1</td> <td>$column_2</td> </tr> </table>"; ?> 

and function:

 <?php /** * Calculate column number where an item should be displayed on a "newspaper style" * or "phoneguide style" report according its postion * used to put same number of items on each column * * receive 3 numbers: $vp_total_size: total number of items on report * $vp_columns : number of columns of report * $vp_element : element position for item (1 to $vp_total_size) * * by Marcos A. Botta <marcos DOT botta AT gmail DOT com> * 02/02/2007 * */ function get_column($vp_total_size, $vp_columns, $vp_element){ if($vp_element <= 0){ return 1; } if($vp_element < $vp_columns && $vp_columns >= $vp_total_size){ return $vp_element; } $vl_avg_items_by_column = $vp_total_size / $vp_columns; $vl_items_on_first_columns = ceil($vl_avg_items_by_column); $vl_items_on_last_columns = floor($vl_avg_items_by_column); $vl_column_limit = ($vl_avg_items_by_column - $vl_items_on_last_columns) * $vp_columns; $vl_allocated_items = 0; for($i=1;$i<$vp_columns;$i++){ if($i < $vl_column_limit || "$i" == "$vl_column_limit"){ $vl_items_on_current_column = $vl_items_on_first_columns; } else{ $vl_items_on_current_column = $vl_items_on_last_columns; } $vl_allocated_items += $vl_items_on_current_column; if($vp_element <= $vl_allocated_items){ return $i; } } return $vp_columns; } // get_column() ?> 

Good luck

+2
source

My implementation:

 <?php $students = array(1,2,3,4,5); $split = floor(count($students)/2); echo '<div id="parent"><div id="col-1">'; $i = 0; foreach($students as $student) { echo 'Student #' . $student . '<br />'; if($i == $split) { echo '</div><div id="col-2">'; } $i++; } echo '</div></div>'; 

Using CSS3 Webkit / Moz only features, in my opinion, are very bad practices.

+1
source

Why don’t you try this code, it is simple to use only css, it’s easy to understand, work in ie and mozilla ...

 <style type="text/css"> .ulcol { float: left; width: 400px; margin: 0; padding: 0; list-style: none; } .licol { float: left; width: 200px; /*half width of the ulcol width*/ margin: 0; padding: 0; } </style> <?php $query = mysql_query("select * from table_name") or die("Error Occured,check again"); echo '<ul class="ulcol">'; while($row = mysql_fetch_assoc($query)) { $vartitle = $row[db_row_title]; echo '<li class="licol">'; echo $vartitle echo '</li>'; } echo '</ul>'; ?> 
+1
source

Maybe split the array into two, then split them, and then print both halves on each div.

0
source

Try something like this

 // connection goes there $q = "SELECT `name` FROM students"; $result = mysql_query($q); $students = array(); while($row=mysql_fetch_assoc($result)) { $students[] = $row['name']; } $students_count = sizeof($students); // chunkes into a two parts , want more columns ? just change "2" to other number $students_chuncked = array_chunk($students,ceil($students_count/2),true); //now display foreach ($students_chuncked as $student_div_data){ echo '<div>',explode($student_div_data,'<br/>'),'</div>'; } 
0
source

I prefer to minimize any early use of echo because tomorrow you will want to move it to a function or method where you should avoid echo. Moreover, with the “echo” in the loop, you have lost the array structure inherent in databases, and you need this structure to manage your data. Therefore, I would prefer to populate the array, process it, and then decompose it into output.

And I would use stylized markers to display elements, because you apparently want to display a list of elements. In pseudo-php code:

 while row = fetch(sql) lines[] = "<li>row_data</li>" end // work on the lines array, eg insert "</ul><ul>" in the middle echo "<ul>".implode("\n",lines)."</ul>" 
0
source

All Articles