开发者

List in 2 columns

I have a list of names and i wanted them in 2 columns like this. also if the first column continued in the second column i want to show first letter and this (continued...) .

#                               C (continued...)
1                               Congo
2                               Cook Islands
3                               Costa Rica
5                               Cote D'Ivoire
6                               Croatia

A                               D
Afghanistan                     Denmark
Africa                          Djibouti
Albania                         Dominica
Algeria
American Samoa

B
Bahamas, The
Bahrain
Bangladesh
Barbados
Bouvet Island
Brazil
Bulgaria

C
Cambodia
Cameroon
Canada
Cape Verde
Caribbean
Chad
Chile
China
Christmas Island
Colombia
Comoros

This is the code have now but it not showing the C (continued...) on the second column

$alphabet = null;
while($row1 = mys开发者_JAVA百科ql_fetch_array($locals)) {
if($alphabet != substs($row1['RSTOWN'],0,1)) {
echo strtoupper(substr($row1['RSTOWN'],0,1));
$alphabet = substr($row1['RSTOWN'],0,1);
}
echo '<li class="forward">
<a href="townpubs.php?RSTOWN='.$row1['RSTOWN'].'" rel="external">'
.$row1['RSTOWN'].
'<small class="listcounter">'.$row1['PubCount'].'</small>
</a>
</li>';


select the names, say number of names = n, calculate halfn = n/2

store in array, if you stored name with index halfn+1,

check if previous entry has the same letter and if so store a "continued..." in the list.

print the list from 1 to truncate(number of entrys in list + 1)/2 to account for odd number of entries.

on each line print [i] and [i+halfn]


I'm not going to make an example on your own code. But I will explain the methods. You simply have to implement your sql to them.

Live example: http://kopli.pri.ee/stackoverflow/6794308.php

Full version of code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>List in 2 columns - Kalle H. Väravas</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <style>
        html, body {margin: 0px; padding: 0px;}
        .container {width: 200px; float: left;}
    </style>
</head>
<body>
<?php

$countries = array(
    'Afghanistan',
    'Africa',
    'Albania',
    'Algeria',
    'American Samoa',
    'Bahamas, The',
    'Bahrain',
    'Bangladesh',
    'Barbados',
    'Bouvet Island',
    'Brazil',
    'Bulgaria',
    'Cambodia',
    'Cameroon',
    'Canada',
    'Cape Verde',
    'Caribbean',
    'Chad',
    'Chile',
    'China',
    'Christmas Island',
    'Colombia',
    'Comoros',
    'Congo',
    'Cook Islands',
    'Costa Rica',
    'Cote D\'Ivoire',
    'Croatia',
    'Denmark',
    'Djibouti',
    'Dominica'
);

$countries_count = count($countries);
$breaking_point = round($countries_count / 2);
$foreach_counter = 0;
$current_letter = '';

echo '<div class="container">';
foreach ($countries as $country) {
    $foreach_counter++;
    if ($country[0] != $current_letter) {
        echo '<b>' . ($current_letter = $country[0]) . '</b><br />';
    }
    echo $country . '<br />';
    if ($foreach_counter == $breaking_point) {
        echo '</div><div class="container">';
    }
}
echo '</div>';

?>
</body>
</html>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜