我想制作一个列表并显示在双列表中

时间:2012-07-27 09:34:35

标签: php mysql html

我想创建一个从数据库中获取数据的列表。我希望将列表显示为double,表示一列和半秒列中的一半数据。我正在使用PHP MySQL和HTML。

4 个答案:

答案 0 :(得分:1)

这是一般性的想法,而不是全部为你写的。循环搜索结果,当你在中途结束当前列表并开始一个新列表时。

你可能需要围绕中途点,但你应该能够用它来做你想要的。使用CSS根据需要定位列表。下次发布一些代码虽然是第一位。

$query = YOUR_QUERY
$count = 0; // Set a counter
$total = mysql_num_rows($query); // Get the total

echo '<ul>';
while ($row = mysql_fetch_array($query)) { // Loop through the query
    echo '<li>'.$row['something'].'</li>'; // Output your li

    if ($count == ($total / 2) { // If you reach half way, start a new list
        echo '</ul><ul>';
    }
    $count++;
}
echo '</ul>';

答案 1 :(得分:0)

假设$ returned_data是一个关联数组,

   echo '<table>';
    foreach($returned_data as $k => $v)
    {
      echo '<tr>';
      echo    '<td>'.$k.'</td><td>'.$v.'</td>';
      echo '</tr>';
    }
    echo '</table>';

答案 2 :(得分:0)

您需要对记录进行计数,然后计算每列所需的行数:

$result = mysql_query("SELECT * FROM `my_table` WHERE `foo` = 'bar'");
$num_records = mysql_num_rows($result);
$num_columns = 2;
$rows_per_col = ceil($num_records / $num_columns);
$records = array();

while($row = mysql_fetch_array($result))
{
    $records[] = $row;
}

// Now get the columns in two arrays:
$col1 = array_slice($records, 0, ($rows_per_col - 1));
$col2 = array_slice($records, $rows_per_col);

// Now loop over the data:
echo '<div class="col">';
foreach($col1 as $row) 
{
    echo "\n\t", '<span class="row">', $row, '</span>';
}
echo '</div>';
echo '<div class="col">';
foreach($col2 as $row) 
{
    echo "\n\t", '<span class="row">', $row, '</span>';
}
echo '</div>';

然后你需要添加一些CSS来浮动列,并设置它们的宽度:

div.col {
    float: left;
    padding: 2%;
    width: 46%;
}
    div.col span.row {
        display: block;
        line-height: 20px;
    }

答案 3 :(得分:0)

尝试使用css

.list-half {
    display: inline-block;
    float: left; /* to pretend whitespace breaks */
    width: 50%;
}

输出:

<ul>
<?php foreach ( $data as $value ) { ?>
    <li class="list-half"><?php echo $value ?></li>
<?php } ?>
</ul>

别忘了清除floats