<?php
header('Content-type:text/html; charset=utf-8');
/*
思路:
连接数据库,
查询商品表,得到商品名称/库存/价格
得到数组后
循环打印tr
*/
$conn = mysql_connect('localhost','root','xiaosatufu');
$sql = 'use test';
mysql_query($sql,$conn);
$sql = 'set names utf8';
mysql_query($sql,$conn);
/*
第三个改进版,用左连接来查询.
[goods left join category on goods.cat_id = category.cat_id] 当成C表看
select goods_id,goods_name,goods_number,shop_price,cat_name from C;
select goods_id,goods_name,goods_number,shop_price,cat_name from goods left join category on goods.cat_id = category.cat_id;
这不行,继续改进
*/
$sql = 'select goods_id,goods_name,goods_number,shop_price,cat_name from goods left join category on goods.cat_id = category.cat_id;';
$rs = mysql_query($sql,$conn);
$list = array();
while($row = mysql_fetch_assoc($rs)) {
$list[] = $row;
}
//print_r($list);exit;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<title>新建网页</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<script type="text/javascript">
</script>
<style type="text/css">
</style>
</head>
<body>
<h1>我也会做报价单</h1>
<table border="1">
<tr>
<td>商品名称</td>
<td>栏目名称</td>
<td>商品库存</td>
<td>商品价格</td>
</tr>
<?php foreach ($list as $v) { ?>
<tr>
<td><?php echo $v['goods_name']; ?></td>
<td><?php echo $v['cat_name']; ?></td>
<td><?php echo $v['goods_number']; ?></td>
<td><?php echo $v['shop_price']; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>