有人可以告诉我如何将mysql数据库生成的所有记录显示在两列中,因为我的数据库中的类别数都显示在一列中,我希望将它们显示在两列中,就像这样
这是我的 php 代码;
index.php
<?php
$dbh=mysql_connect("localhost","root","root") or die ('Cannot connedt to the Database' .mysql_errno());
mysql_select_db("booksdb");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Select a Company</title>
</head>
<body>
<?php
$res_query = mysql_query("SELECT A.cat_id as cat_id, count(A.cat_id) as cnt, B.category as category FROM books A, bookcatname B WHERE A.cat_id=B.cat_id GROUP BY A.cat_id");
while ($category = mysql_fetch_assoc($res_query) )
{
echo '<a href="page.php?cat_id='.$category['cat_id'].'">'.$category['category'].' ('.$category['cnt'].')</a><br />';
}
?>
</body>
</html>
page.php
<?php
$dbh=mysql_connect("localhost","root","root") or die ('Cannot connect to the Database' .mysql_errno());
mysql_select_db("booksdb");
if ( empty($_GET['cat_id']) )
{
header('Location: index.php');
exit();
}
$getCats = mysql_query("SELECT * FROM books WHERE cat_id = '".intval($_GET['cat_id'])."'");
echo '<ul>';
while ( $book = mysql_fetch_assoc($getCats) )
{
echo '<li>'.$book['title'].'<br />'.$book['author'].'<br />'.'</li><br />';
}
echo '</ul>';
?>
最佳答案
不使用任何 ul-li 或表格,仅使用 css,您可以执行以下操作:
div
{
column-count:2;
-moz-column-count:2; /* Firefox */
-webkit-column-count:2; /* Safari and Chrome */
}
你会得到类似的东西:
<div>
a b
c d
e f
</div>
编辑:看来这个 CSS 解决方案在 IE 中还不起作用。
所以我知道的最好的跨浏览器方法是使用 ul-li (将 li 宽度设置为 50%)
关于php - 以两列显示记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25198876/