开发者

Mysql - Split data into div [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed开发者_运维问答 11 years ago.

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>';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜