English 中文(简体)
将数据库中的数值显示为html表
原标题:displaying values from database into an html table
  • 时间:2010-09-20 14:55:14
  •  标签:
  • php
  • html

i 希望在有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>
最佳回答

引人:

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

希望这一帮助!

问题回答

这里的基本思想是:

You get the count of the data via num_rows Divide by two. Now, the result of your division will be the number of rows.

产出一环x和x+南行的对应值。 例如,第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时,休息时间应当结束。 应当从那里简单明了。 Good luck.

你们可以通过如下方式,在休息和公正加薪方面增加另一个变量,即美元:

<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: 更新每个结果的浏览数据,假设目录——名称是阵列中的第二个项目,即3.1美元。

如果一定要打上桌子,用浏览器打上支持第三特别安全局的灯塔,那么你可以使用特别安全局的栏目:

http://jsfiddle.net/QKuDL/

否则,你需要首先对结果进行分类(未经测试——购买力平价不是我的强权):

<?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>";




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签