views:

678

answers:

7

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

A: 

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

lemon
How it is possible wiht array , becoz once we entered into loop, how to limit the result,
Bharanikumar
Bharanikumar: You would limit the resultset using the LIMIT clause of MySQL, not by iterating over a portion of the array.
phidah
+1  A: 

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>";
?>
naivists
Fine , but am tring to give spcae between columns, but its not work,But one smart idea,,
Bharanikumar
well, that's another question, you will have to do some css to format the two DIV elements to be aligned next to each other. I would do it like `#div1, #div2 {float:left; margin-left:50px; width:300px;}`
naivists
A: 
   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>';
}
Fivell
+2  A: 

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

Anurag
"Not sure how widely it is supported". The code explains itself ;-) "webkit" + "moz" is still a fairly small part of the users - of course depending on the users.
phidah
Sometimes I feel lucky for not having the luxury of being able to test in IE :)
Anurag
no css3 knowledge, i tried this code, but no worth...
Bharanikumar
@Bharanikumar: were you testing in Firefox or Safari (or any other Gecko or Webkit based browser)? It definitely doesn't work in IE (and perhaps others).
David Thomas
A: 

I prefer to minimize any early use of "echo", because tomorrow you will want to move this in a function or a method, where "echo" should be avoided. Moreover, with the "echo" in the loop you've lost the array structure inherent to databases, and you need this structure to manipulate your data. So I would rather fill an array, process it, then implode to output.

And I would use styled bullet points to display the items, because you apparently want to display a list of items. 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>"
gb
Throwing the result into a temporary array is a waste of resources.
phidah
this echo simply display array array..
Bharanikumar
@phidah, wasting resources is usually not a problem at this level according to my experience, but doing reusable code is. If the display of the items becomes complex, with links and ajax hooks, and if you want to reformat the list later, for example to display it in a mail, you will prefer to have it in an array that you implode later. It is also the way Alex Martelli advocates in "Python in a Nutshell". This advice helped me a lot when refactoring code, later.
gb
+1  A: 

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 the CSS3 Webkit/Moz only features are in my opinion very bad practice.

phidah
Use of browser-specific features depends on your intended audience (e.g. Internet public-face of a company vs corporate intranet can be very different), but you make a point that's very important to consider.
Roger Pate
That's a very valid point. However, browser inconsistencies will always be there as long as a single browser dominates, and websites will be built with those inconsistencies whether to gain access to that bleeding edge feature, or to cope with lack of that obvious feature. Which is why we need an abstraction layer, no matter how thin, to deal with these inconsistencies. That's the main reason why Javascript libraries like jQuery/MooTools are extremely valuable, and not because of those fancy cool animations. A similar abstraction is needed in handling CSS inconsistencies till the standard is..
Anurag
implemented, or in dealing with those everlasting minor quirks from browser to browser. And IMO a problem like the one in this question has everything to do with views and should be handled by that CSS abstraction layer instead of fixing it directly in code.
Anurag
@Roger Pate: Agreed, there are certain situations where you could use it (intranet, or your web app targeted only towards iPhone users (which of course use Webkit)). However, my note in the answer was regarding more 'general' sites. I fully support using for example CSS3 rounded corners for a bit of moz/webkit only eyecandy, but for columns which play a central role in the layout one should go for options that work in the browsers of the majority of your users.
phidah
A: 

My Problem fixed

Bharanikumar