简体   繁体   English

将数据库中的值显示到html表中

[英]displaying values from database into an html table

i want to display values from a database (a list of categories) into a table that has 2 columns and x number of rows. 我想将数据库(类别列表)中的值显示到具有2列和x行数的表中。

I want my web page to display like this: 我希望我的网页显示如下:

Apes Cats
Apples Cherries
Bats Tigers
Berries Zebras

Instead of 代替

Apes Apples
Bats Bears
Cats Cherries
Tigers Zebras

Here is my code so far: 到目前为止,这是我的代码:

<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>

Here's the basic idea: 这是基本思想:

You get the count of the data via num_rows Divide by two. 您可以通过num_rows除以二获得数据计数。 Now, the result of your division will be the number of rows. 现在,划分的结果将是行数。

Output a loop echoing value for row x and x+ num rows. 输出第x行和x + num行的循环回显值。 For example the output of line 1 would be : 例如,第1行的输出为:

<tr><td>$row[val1][data]</td><td>$row[val5][data]</td></tr>

So, your loop would ultimately output: 因此,您的循环最终将输出:

val 1  |  val 5
val 2  |  val 6
val 3  |  val 7
val 4  |  val 8

The loop should end when your incrementing variable = num_rows. 当您的递增变量= num_rows时,循环应结束。 Should be pretty straightforward from there. 从那里应该很简单。 Good luck. 祝好运。

You can add an another variable say, $i, to the loop and just increment this through as follows: 您可以在循环中添加另一个变量,例如$ 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>

EDIT: Updated to fetch row data for each result and assume that cat_name is the second item in the array ie $category[1]. 编辑:更新以获取每个结果的行数据,并假设cat_name是数组中的第二项,即$ category [1]。

If it doesn't have to be a table and your fine with browsers that support CSS 3, then you can use CSS columns: 如果不必一定要有一个表,并且对支持CSS 3的浏览器没问题,那么可以使用CSS列:

http://jsfiddle.net/QKuDL/ http://jsfiddle.net/QKuDL/

Otherwise you'll need to sort the results first (untested - PHP is not my strong suit): 否则,您需要首先对结果进行排序(未经测试-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>
<?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>";

Try this (haven't tested it though): 试试这个(虽然没有测试过):

<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>

Hope this helps ! 希望这可以帮助 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM