代码:
<?php $hotels=array();
$hotelsql="SELECT th_name,th_areas,th_start,th_hprice,th_sprice,th_lprice FROM tour_hotel";
$res=mysql_query($hotelsql);
$hotels=mysql_fetch_array($res,MYSQL_BOTH); $items=array();
$sqlitems="SELECT to_city,nights FROM tour_price WHERE to_id=".$to_id;
$resitems=mysql_query($sqlcitynights);
$items=mysql_fetch_array($resitems,MYSQL_BOTH);
//初始化价格
$prices = array(
'th_hprice3'=>0,//3星淡季价
'th_hprice4'=>0,//4星淡季价
'th_hprice5'=>0,//5星淡季价
'th_sprice3'=>0,//3星平季价
'th_sprice4'=>0,//4星平级价
'th_sprice5'=>0,//5星平级价
'th_lprice3'=>0,//3星旺季价
'th_lprice4'=>0,//4星旺季价
'th_lprice5'=>0 //5星旺季价
);
foreach($hotels as $n) {
switch($n['th_start']) {
case 3: $prices['th_hprice3'] += $n['th_hprice'] * $items[$n['th_areas']]['nights'];
$prices['th_sprice3'] += $n['th_sprice'] * $items[$n['th_areas']]['nights'];
$prices['th_lprice3'] += $n['th_lprice'] * $items[$n['th_areas']]['nights'];
break;
case 4: $prices['th_hprice4'] += $n['th_hprice'] * $items[$n['th_areas']]['nights'];
$prices['th_lprice4'] += $n['th_sprice'] * $items[$n['th_areas']]['nights'];
$prices['th_lprice4'] += $n['th_lprice'] * $items[$n['th_areas']]['nights'];
break;
case 5: $prices['th_hprice5'] += $n['th_hprice'] * $items[$n['th_areas']]['nights'];
$prices['th_sprice5'] += $n['th_sprice'] * $items[$n['th_areas']]['nights'];
$prices['th_lprice5'] += $n['th_lprice'] * $items[$n['th_areas']]['nights'];
break;
}
} $prices['th_hprice3']= $n['th_hprice'] * $items[$n['th_areas']]['nights'];
?>
<tr>
<td width="6%" rowspan="3" align="center" bgcolor="#f1f1f1">3星</td>
<td width="6%" align="center" bgcolor="#f1f1f1">淡季</td>
<td align="center"><?php echo $prices['th_hprice3'] ?></td>
echo $prices['th_hprice3']这个最后计算的等于0 怎么回事 别打击我 不会写!求安慰!
<?php $hotels=array();
$hotelsql="SELECT th_name,th_areas,th_start,th_hprice,th_sprice,th_lprice FROM tour_hotel";
$res=mysql_query($hotelsql);
$hotels=mysql_fetch_array($res,MYSQL_BOTH); $items=array();
$sqlitems="SELECT to_city,nights FROM tour_price WHERE to_id=".$to_id;
$resitems=mysql_query($sqlcitynights);
$items=mysql_fetch_array($resitems,MYSQL_BOTH);
//初始化价格
$prices = array(
'th_hprice3'=>0,//3星淡季价
'th_hprice4'=>0,//4星淡季价
'th_hprice5'=>0,//5星淡季价
'th_sprice3'=>0,//3星平季价
'th_sprice4'=>0,//4星平级价
'th_sprice5'=>0,//5星平级价
'th_lprice3'=>0,//3星旺季价
'th_lprice4'=>0,//4星旺季价
'th_lprice5'=>0 //5星旺季价
);
foreach($hotels as $n) {
switch($n['th_start']) {
case 3: $prices['th_hprice3'] += $n['th_hprice'] * $items[$n['th_areas']]['nights'];
$prices['th_sprice3'] += $n['th_sprice'] * $items[$n['th_areas']]['nights'];
$prices['th_lprice3'] += $n['th_lprice'] * $items[$n['th_areas']]['nights'];
break;
case 4: $prices['th_hprice4'] += $n['th_hprice'] * $items[$n['th_areas']]['nights'];
$prices['th_lprice4'] += $n['th_sprice'] * $items[$n['th_areas']]['nights'];
$prices['th_lprice4'] += $n['th_lprice'] * $items[$n['th_areas']]['nights'];
break;
case 5: $prices['th_hprice5'] += $n['th_hprice'] * $items[$n['th_areas']]['nights'];
$prices['th_sprice5'] += $n['th_sprice'] * $items[$n['th_areas']]['nights'];
$prices['th_lprice5'] += $n['th_lprice'] * $items[$n['th_areas']]['nights'];
break;
}
} $prices['th_hprice3']= $n['th_hprice'] * $items[$n['th_areas']]['nights'];
?>
<tr>
<td width="6%" rowspan="3" align="center" bgcolor="#f1f1f1">3星</td>
<td width="6%" align="center" bgcolor="#f1f1f1">淡季</td>
<td align="center"><?php echo $prices['th_hprice3'] ?></td>
echo $prices['th_hprice3']这个最后计算的等于0 怎么回事 别打击我 不会写!求安慰!
得到的一维数组$prices['th_hprice3'] += $n['th_hprice'] * $items[$n['th_areas']]['nights'];
计算时需要的是一个二维数组
帖子地址:http://topic.csdn.net/u/20120106/11/603810d2-e0af-4b23-b14b-a8467f0839d4.html
那么是不是 $sqlitems? 如果是那么SQL里你只获取了 to_city,nights
但是我看你最后要的大约是th_areas和nights吧?( $items[$n['th_areas']]['nights']);
那么 th_areas 呢?另外在sql 执行后 print_r($items);将$items 输出的结果贴出来看看
$hotels=array();
$hotelsql="SELECT th_name,th_areas,th_start,th_hprice,th_sprice,th_lprice FROM tour_hotel";
$res=mysql_query($hotelsql);
$hotels=mysql_fetch_array($res,MYSQL_BOTH);
//print_r($hotels);return;
$items=array();
$sqlcitynights="SELECT to_city,nights FROM tour_price WHERE to_id=".$to_id;
//echo $sqlcitynights;return;
$resitems=mysql_query($sqlcitynights);
$items=mysql_fetch_array($resitems,MYSQL_BOTH);
//print_r($items);return;
//初始化价格
$prices = array(
'th_hprice3'=>0,//3星淡季价
'th_hprice4'=>0,//4星淡季价
'th_hprice5'=>0,//5星淡季价
'th_sprice3'=>0,//3星平季价
'th_sprice4'=>0,//4星平级价
'th_sprice5'=>0,//5星平级价
'th_lprice3'=>0,//3星旺季价
'th_lprice4'=>0,//4星旺季价
'th_lprice5'=>0 //5星旺季价
);
foreach($hotels as $n) {
switch($n['th_start']) {
case 3: $prices['th_hprice3'] += $n['th_hprice'] * $items[$n['th_areas']]['nights'];
$prices['th_sprice3'] += $n['th_sprice'] * $items[$n['th_areas']]['nights'];
$prices['th_lprice3'] += $n['th_lprice'] * $items[$n['th_areas']]['nights'];
break;
case 4: $prices['th_hprice4'] += $n['th_hprice'] * $items[$n['th_areas']]['nights'];
$prices['th_lprice4'] += $n['th_sprice'] * $items[$n['th_areas']]['nights'];
$prices['th_lprice4'] += $n['th_lprice'] * $items[$n['th_areas']]['nights'];
break;
case 5: $prices['th_hprice5'] += $n['th_hprice'] * $items[$n['th_areas']]['nights'];
$prices['th_sprice5'] += $n['th_sprice'] * $items[$n['th_areas']]['nights'];
$prices['th_lprice5'] += $n['th_lprice'] * $items[$n['th_areas']]['nights'];
break;
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>线路价格详细</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="style/index.css" type="text/css" rel="stylesheet" />
<style type="text/css">
<!--
.STYLE1 {color: #FF0000}
-->
</style>
</head>
<script src="../js/jquery.min.js" type="text/javascript" language="JavaScript"></script>
<script src="../js/my.js" type="text/javascript" language="JavaScript"></script>
<script type="text/javascript" language="JavaScript">
</script>
<body>
<table width="1016" class="add_table">
<tr>
<td colspan="13" align="center" bgcolor="#f1f1f1">线路名称</td>
</tr>
<tr>
<td colspan="2" align="center" bgcolor="#f1f1f1">人数:</td>
<td width="8%" align="center">1</td>
<td width="8%" align="center">2</td>
<td width="8%" align="center">3</td>
<td width="8%" align="center">4</td>
<td width="8%" align="center">5</td>
<td width="8%" align="center">6</td>
<td width="8%" align="center">7</td>
<td width="8%" align="center">8</td>
<td width="8%" align="center">9</td>
<td width="8%" align="center">10</td>
<td width="8%" align="center">单间差</td>
</tr>
<tr>
<td width="6%" rowspan="3" align="center" bgcolor="#f1f1f1">3星</td>
<td width="6%" align="center" bgcolor="#f1f1f1">淡季</td>
<td align="center"><?php echo $prices['th_hprice3'] ?></td>//这里的打印输出的是0 求安慰
看起来是不是 价格*地区*住了几个晚上?这个 nights 的值从何而来?你的两条SQL我都未能看到这个字段.
$items 需要完整取出while($item=mysql_fetch_array($resitems,MYSQL_BOTH) )
$items[] = $item;$n['th_areas'] 对应 tour_price 表中什么字段?
nights 第二个sql语句有!
th_name酒店名字 th_areas酒店地区 th_start酒店星级 th_hprice价格1 th_sprice价格2 th_lprice价格3
第二个sql语句查询的内容是 $to_id 线路对应在每个城市住宿的夜数 to_city城市名 nights 夜数
逻辑关系是一条线路经过多个地区 住酒店的时候 比如说我在第一个城市住的三星 后面的城市都必须住三星
住四星的话 后面也必须都四星 ,5星 后面的城市都住5星
$items=array();
while($result=mysql_fetch_assoc($resitems,MYSQL_BOTH)){
$items[]=$result;
}
print_r($items);输出结果 瞧瞧,我承认我已经陷入昏迷状态...哈哈哈
我在linux 不想用那个webQQ.