Mysql - Split data into div [closed]
I have a table called "Names", it contains about 400 inputs. I want for every 50 names to be in one div.
<div id="1">name1, name2, ... name50</div>
<div id="2">name51,name52, ... name100</div> and so on ...
Any help?
This is easily achieved using the following script (hopefully I skipped the syntax errors):
<?php
//Get the names
$names_res = mysql_query("SELECT names FROM Names");
//Set a counter
$counter = 0;
//Loop over the names
while( $name_arr = mysql_fetch_assoc($names_res) )
//Let's see now...
if( ($counter % 50 == 0) || $counter == 0 )
echo '<div id="', ( ($counter == 0)? 1 : ($counter/50)+1 ) , '">';
//Output the name
echo $name_array['name'] , ( ($counter % 49 === 0 )? '' : ', ' );
//Close the div if neccesary
if( ($counter % 49 == 0) || $counter == 0 ) echo '</div>';
//Yes, we do want to increment it
$i++;
}
?>
Good luck!
Something like this should at least be a good start:
for ($n = 0; $n< 140; $n++) {
$names[] = 'name'.$n;
}
$n = 0;
$div = 0;
foreach ($names as $name) {
if ($n%50 == 0) {
$div++;
echo '<div id="'.$div.'">';
}
echo $name . ', ';
if ($n%50 == 49) {
echo '</div>';
}
$n++;
}
echo '</div>';
精彩评论