代码:
<?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 怎么回事  别打击我 不会写!求安慰!

解决方案 »

  1.   

    $items=mysql_fetch_array($resitems,MYSQL_BOTH);
    得到的一维数组$prices['th_hprice3'] += $n['th_hprice'] * $items[$n['th_areas']]['nights'];
    计算时需要的是一个二维数组
      

  2.   

    稍微看了一下代码$prices ['th_hprice3'] = $n ['th_hprice'] * $items [$n ['th_areas']] ['nights'];这是你的计算公式,如果最后等于0, 那么其中一项必定等于零...哈哈哈所以分别输出 $n ['th_hprice'] 和 $items [$n ['th_areas']] ['nights'] 你就知道问题在什么地方了。由于无法看到你的原始数据所以这个只能让你自己劳动啦
      

  3.   

    谢谢老大 还是有点不明白 就这个计算搞几天了 结合这个帖子 求大虾们帮忙给代码改改 谢谢了!
    帖子地址:http://topic.csdn.net/u/20120106/11/603810d2-e0af-4b23-b14b-a8467f0839d4.html
      

  4.   

    $resitems=mysql_query($sqlcitynights);这里的 $sqlcitynights 是不是 $sqlitems? 如果不是的话 $sqlcitynights是什么内容?
      

  5.   


    那么是不是 $sqlitems? 如果是那么SQL里你只获取了 to_city,nights 
    但是我看你最后要的大约是th_areas和nights吧?( $items[$n['th_areas']]['nights']);
    那么 th_areas 呢?另外在sql 执行后 print_r($items);将$items 输出的结果贴出来看看
      

  6.   

    是的 我已经改过来了 th_areas 是上一个查询数组的值 它和to_city是一样的 打印结果 Array ( [0] => 北京 [to_city] => 北京 [1] => 3 [nights] => 3 ) 
      

  7.   

    这个是酒店查询的数组打印出的内容 Array ( [0] => 北京金地来酒店 [th_name] => 北京金地来酒店 [1] => 北京 [th_areas] => 北京 [2] => 4 [th_start] => 4 [3] => 400 [th_hprice] => 400 [4] => 500 [th_sprice] => 500 [5] => 600 [th_lprice] => 600 )
      

  8.   

    代码重贴一下 我认为的错误重新改了下
     $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 求安慰
      

  9.   

    我没看太懂你上面的关系 不过不要紧,你的SQL修正正确就好了。现在回到问题的关键$prices ['th_hprice3'] = $n ['th_hprice'] * $items [$n ['th_areas']] ['nights'];$items [$n ['th_areas']] ['nights']; 这个是啥东西呢,把这个修正就可以了.
    看起来是不是 价格*地区*住了几个晚上?这个 nights 的值从何而来?你的两条SQL我都未能看到这个字段.
      

  10.   

    还是 $items=mysql_fetch_array($resitems,MYSQL_BOTH);?
    $items 需要完整取出while($item=mysql_fetch_array($resitems,MYSQL_BOTH) )
      $items[] = $item;$n['th_areas'] 对应 tour_price 表中什么字段?
      

  11.   

    是价格是根据地区的来的 每个地区对应住几个晚上
    nights 第二个sql语句有!
      

  12.   

    第一个sql语句查询的内容是所有地区的酒店信息(每个地区只有三家酒店,分别对应3,4,5星的酒店)
    th_name酒店名字 th_areas酒店地区 th_start酒店星级 th_hprice价格1 th_sprice价格2 th_lprice价格3
    第二个sql语句查询的内容是 $to_id 线路对应在每个城市住宿的夜数 to_city城市名 nights 夜数
    逻辑关系是一条线路经过多个地区 住酒店的时候 比如说我在第一个城市住的三星 后面的城市都必须住三星
    住四星的话 后面也必须都四星 ,5星 后面的城市都住5星 
      

  13.   

    $items=mysql_fetch_array($resitems,MYSQL_BOTH);这里修改成 
    $items=array();
    while($result=mysql_fetch_assoc($resitems,MYSQL_BOTH)){
    $items[]=$result;
    }
    print_r($items);输出结果 瞧瞧,我承认我已经陷入昏迷状态...哈哈哈
      

  14.   

    我都不好意思了 到了这一步了 脸是浮云了 结果array() 俩个查询都循环进行打印了 都是array()
      

  15.   

    我发消息给你,直接跟我msn吧.这样问来答去太蛋痛了。
      

  16.   

    装一个嘛申请一下很快的...我还给了个gtalk给你,看消息,
    我在linux 不想用那个webQQ.