21,890
社区成员
发帖
与我相关
我的任务
分享
$result = mysql_query("SELECT p_class FROM Product group by p_class limit 40");//取得商品类别名称:p_class
mysql_data_seek($result, 0);
while ($row=mysql_fetch_row($result))
{
echo "<table bord='1'>\n";
echo "<tr>\n";
for ($i=0; $i<mysql_num_fields($result); $i++ )//外循环,实现分行显示p_class名
{
echo '<td align="center">';
echo "$row[$i]";
echo '</td>';
$result2 = mysql_query("SELECT * FROM Product where p_class=$row[$i] limit 0,4");
while ($row=mysql_fetch_row($result2))
{
echo '<table><tr><td align="right">';
for ($i=0; $i<mysql_num_fields($result2); $i++ )//内循环,实现显示:在不同的P_class下的所有商品名称,图片等内容
{
echo "<img src={$row['pic']} />";
}//内循环结束
echo '</td></tr></table>';
}//外循环结束
}
echo "</tr>\n";
}
echo "</table>\n";
mysql_free_result($result);
mysql_close($conn);
?>
<?php
//连接数据库
function connect() {
$connect = mysql_connect("localhost:3306", "root", "111111");
if (!$connect) {
die("服务器连接失败:" . mysql_error());
}
$conn = mysql_select_db("test", $connect) or die("数据库连接失败!<br/>");
mysql_query("set names utf8");
return $connect;
}
connect();
function sqlQuery($sql,$count){
$res = mysql_query($sql);
if(!$res)
die("SQL:{$sql}<br>Error:".mysql_error());
if(mysql_affected_rows() > 0){
$arr = array();
switch($count){
case 1:
$arr = mysql_fetch_array($res,MYSQL_ASSOC);
break;
case 2:
while($rows = mysql_fetch_array($res,MYSQL_ASSOC)){
array_push($arr,$rows);
}
break;
}
}else{
echo "查询失败<br>Error:".mysql_error();
}
return $arr;
}
//$sql_type = "SELECT p_class FROM Product group by p_class limit 40";
$sql_type = "SELECT type FROM csdn_test4 group by type limit 40";
$types = sqlQuery($sql_type,2);//print_r($types);
?>
<!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>contentByTypeTitle</title>
<style type="text/css">
div{
border:solid 1px;
width:800px;
height:120px;
margin:5px;
}
ul{
list-style:none;
margin:5px;
float:left;
width:200px;
}
li{
padding:5px;
text-decoration:underline;
}
</style>
</head>
<body>
<?php
foreach($types as $type){
foreach($type as $title){
//$sql_title = "SELECT * FROM Product where p_class='".$title."' limit 0,4";
$sql_title = "SELECT * FROM csdn_test4 where type='".$title."' limit 0,4";
$contents = sqlQuery($sql_title,2);
?>
<div>
<b><?=$title?></b><br>
<?php foreach($contents as $content){ ?>
<ul>
<?php foreach($content as $key=>$cont){ ?>
<li><span><?=$key?></span><?=$cont?></li>
<?php } ?>
</ul>
<?php } ?>
</div>
<?php
}
}
?>
</body>
</html>

$result = mysql_query("SELECT p_class FROM Product group by p_class limit 40");
mysql_data_seek($result, 0);
echo "<table border='1'>\n";
while ($row=mysql_fetch_row($result))
{
for ($i=0; $i<mysql_num_fields($result); $i++ )
{
echo "<tr>";
echo '<td>';
echo "$row[$i]";
echo '</td>';
echo "</tr>\n";
}
}
echo "</table>\n";
mysql_free_result($result);
<table border='1'>
<tr><td>111</td></tr>
<tr><td>2222</td></tr>
<tr><td>3333</td></tr>
</table>
mysql> select * from csdn_test4;
+----+--------+----------------------+
| id | type | content |
+----+--------+----------------------+
| 1 | 天文 | 天文 天文 天文 |
| 2 | 天文 | 天 天 天文 |
| 3 | 天文 | 文 文 天文 |
| 4 | 地理 | 地理 地理 地理 |
| 5 | 地理 | 地 地 地 |
| 6 | 地理 | 理 理 理 |
| 7 | 科技 | 科技 科技 科技 |
| 8 | 科技 | 科 科 科 |
| 9 | 科技 | 技 技 技 |
+----+--------+----------------------+
9 rows in set (0.00 sec)
<?php
//连接数据库
function connect() {
$connect = mysql_connect("localhost:3306", "root", "111111");
if (!$connect) {
die("服务器连接失败:" . mysql_error());
}
$conn = mysql_select_db("test", $connect) or die("数据库连接失败!<br/>");
mysql_query("set names utf8");
return $connect;
}
connect();
function sqlQuery($sql,$count){
$res = mysql_query($sql);
if(!$res)
die("SQL:{$sql}<br>Error:".mysql_error());
if(mysql_affected_rows() > 0){
$arr = array();
switch($count){
case 1:
$arr = mysql_fetch_array($res,MYSQL_ASSOC);
break;
case 2:
while($rows = mysql_fetch_array($res,MYSQL_ASSOC)){
array_push($arr,$rows);
}
break;
}
}else{
echo "查询失败<br>Error:".mysql_error();
}
return $arr;
}
//$sql_type = "SELECT p_class FROM Product group by p_class limit 40";
$sql_type = "SELECT type FROM csdn_test4 group by type limit 40";
$types = sqlQuery($sql_type,2);//print_r($types);
?>
<?php
foreach($types as $type){
foreach($type as $title){
//$sql_title = "SELECT * FROM Product where p_class='".$title."' limit 0,4";
$sql_title = "SELECT * FROM csdn_test4 where type='".$title."' limit 0,4";
$contents = sqlQuery($sql_title,2);//print_r($contents);
?>
<table border="" cellpadding="0" cellspacing="0">
<tr>
<td colspan=3><?=$title?></td>
</tr>
<tr>
<?php foreach(array_keys($contents[0]) as $tit){ ?>
<td><?=$tit?></td>
<?php } ?>
</tr>
<?php foreach($contents as $content){ ?>
<tr>
<?php foreach($content as $key=>$cont){ ?>
<td><?=$cont?></td>
<?php } ?>
</tr>
<?php } ?>
</table><br>
<?php
}
}
?>
