我希望将数据库(类别列表)中的值显示到包含2列和x行数的表中。
我希望我的网页显示如下:
Apes Cats Apples Cherries Bats Tigers Berries Zebras
而不是
Apes Apples Bats Bears Cats Cherries Tigers Zebras
到目前为止,这是我的代码:
<table border="0" bordercolor="#FFCC00" style="background-color:#FFFFCC" width="400" cellpadding="3" cellspacing="3">
<?php
$query = "SELECT * FROM category ORDER BY cat_name";
$data = mysqli_query($dbc, $query);
while ($category = mysqli_fetch_array($data)) {
?>
<tr>
<td><?php echo $category['cat_name'] ?></td>
</tr>
<?php } ?>
</table>
答案 0 :(得分:1)
这是基本想法:
您可以通过num_rows获取数据的计数 除以2。 现在,您的除法结果将是行数。
输出行x和x + num行的循环回显值。例如,第1行的输出为:
<tr><td>$row[val1][data]</td><td>$row[val5][data]</td></tr>
所以,你的循环最终会输出:
val 1 | val 5
val 2 | val 6
val 3 | val 7
val 4 | val 8
当你的递增变量= num_rows时,循环应该结束。从那里应该非常简单。祝你好运。
答案 1 :(得分:0)
你可以在循环中添加另一个变量,例如$ i,并按如下方式递增:
<table border="0" bordercolor="#FFCC00" style="background-color:#FFFFCC" width="400" cellpadding="3" cellspacing="3">
<?php
$query = "SELECT * FROM category ORDER BY cat_name";
$data = mysqli_query($dbc, $query);
$i=1;
while ($category = $data->fetch_row()) {
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $category[1] ?></td>
</tr>
<?php
$i++;
} ?>
</table>
编辑:更新以获取每个结果的行数据,并假设cat_name是数组中的第二个项目,即$ category [1]。
答案 2 :(得分:0)
如果它不一定是一个表,你的浏览器支持CSS 3,那么你可以使用CSS列:
否则你需要先对结果进行排序(未经测试 - PHP不是我的强项):
<?php
$query = "SELECT * FROM category ORDER BY cat_name";
$data = mysqli_query($dbc, $query);
$col_count = 2;
$max_items_per_col = ceil( mysqli_num_rows ( $data ) / $col_count );
$cols = array(array());
$col = 0;
while ($category = mysqli_fetch_array($data)) {
if (count($cols[$col]) >= $max_items_per_col) {
$col++;
}
$cols[$col][] = $category['cat_name'];
}
?>
<table> <!-- all of those attributes should be CSS instead -->
<?php for ($i = 0; $i < $max_items_per_col; $i++) { ?>
<tr>
<?php foreach ($cols as $col) { ?>
<td><?php if(isset($col[$i])) echo $col[$i]; ?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
答案 3 :(得分:0)
<?php
$query = "SELECT * FROM category ORDER BY cat_name";
$data = mysqli_query($dbc, $query);
$midpoint = ceil($data->num_rows / 2);
$i=0;
while ($category = $data->fetch_array()) {
if ($i < $midpoint)
$left[$i] = "<td>" . $category['cat_name'] . "</td>";
else
$right[$i - $midpoint] = "<td>" . $category['cat_name'] . "</td>";
$i++;
}
print "<table>";
for ($j=0; $j < $i; $j++)
{
print "<tr>" . $left[$j];
if (array_key_exists($j, $right)) print $right[$j];
print "</tr>";
}
print "</table>";
答案 4 :(得分:0)
试试这个(虽然没有测试过):
<table border="0" bordercolor="#FFCC00" style="background-color:#FFFFCC" width="400" cellpadding="3" cellspacing="3">
<?php
$query = "SELECT * FROM category ORDER BY cat_name";
$data = mysqli_query($dbc, $query);
# Calculate total rows and half rows, rounded up
$full_row_count = mysqli_num_rows($data);
$half_row_count = ceil($full_row_count / 2);
$i = 0;
while ($i <= $half_row_count) {
# Set the result pointer for first column ...
mysqli_data_seek($data, $i);
$category_1 = mysqli_fetch_array($data);
# ... then calculate the offset for the second column ...
$col_2_offset = $i + $half_row_count;
# .. and make sure it's not larger than total rows - 1
if ($col_2_offset <= $full_row_count - 1) {
mysqli_data_seek($data, $col_2_offset);
$category_2 = mysqli_fetch_array($data);
} else {
$category_2 = array('cat_name' => '');
}
?>
<tr>
<td><?php echo $category_1['cat_name'] ?></td>
<td><?php echo $category_2['cat_name'] ?></td>
</tr>
<?php
$i++;
}
?>
</table>
希望这有帮助!