dongxin9759 2014-07-18 18:31
浏览 40
已采纳

多维阵列打印

I'm trying very hard to display this multi dimentional array for past 3 to 4 days to no avail.
All I need to do is to display like below:

carName:

carImage:
days:
amount:  // this is where I'm facing the problem to display all the days and amount related to specific car..One car will have few days and amount quoted for each day.

//pls look at my code and help... Thanks

<?php
    mysql_select_db($database);

    $query_showall = "SELECT rental.*,
                             car_name.*,
                             gallery.*,
                             car_make.* 
                        FROM rental,
                             car_name,
                             gallery,
                             car_make 
                       WHERE car_name.carName_id = gallery.carName_id 
                         AND car_name.carMake_id = car_make.carMake_id
                         AND rental.carName_id   = car_name.carName_id 
                    GROUP BY rental.carName_id";

    $result_showall = mysql_query($query_showall) or die(mysql_error());

    while($row_showall = mysql_fetch_array($result_showall)) {
        $carMake_all = $row_showall['carName'];

        $carmake_1[$row_showall['carName_id']][] = $row_showall;          
    }                                                                   

    foreach($carmake_1 as $make_1=>$name_1) {
        foreach($name_1 as $n_1) {
            echo $n_1['carName'].'<br/>'; 
            echo $n_1['gallery'].'<br/>';

            /* I need to loop through the rental table 
               to retrieve num of days and amount for 
               each car here.. */
            echo $n_1['rental_days'].'<br/>';
            echo $n_1['rental_amount'].'<br/>';     
        }                                                 
    }                                                  
?>                                              

edit without GROUP BY But how can I stop the carName and imageName not to repeat ?

    <?php
 mysql_select_db($database);


 $query_showall="SELECT rental.*,car_name.*,gallery.*,car_make.* FROM rental,car_name,gallery,car_make WHERE car_name.carName_id=gallery.carName_id AND  
     car_name.carMake_id=car_make.carMake_id   AND rental.carName_id=car_name.carName_id ORDER BY rental_days ASC";



$result_showall=mysql_query($query_showall)or die(mysql_error());
  while($row_showall=mysql_fetch_array($result_showall))
  {
      $carMake_all=$row_showall['carName'];
      $carmake_1[$row_showall['carName_id']][]=$row_showall;

  }                                                                 


            foreach($carmake_1 as $make_1=>$name_1)

                   {

            foreach($name_1 as $n_1)

                     {

                          echo $n_1['carName'].'<br/>'; 
                          echo $n_1['gallery'].'<br/>';
                          echo $n_1['rental_days'].'<br/>';
                          echo $n_1['rental_amount'].'<br/>';


                 }

                  }   

?>
  • 写回答

1条回答 默认 最新

  • duanqiongchong0354 2014-07-18 19:19
    关注

    Okay, I'll take a shot at this. What I would do is rebuild the array before you try to dump it out so that all your data is organized the way you want it to display, that way at each step of the foreach you can only display what you want.

    This code would use your SQL statement without the group by, which as you stated is including all the data.

    // Start with an empty array...
    $cars = array();
    
    // Loop over all your results
    while($row_showall = mysql_fetch_array($result_showall)) {
        // Set name, if the ID comes up again, it will be reset but won't hurt anything
        $cars[$row_showall['carName_id']]['name']    = $row_showall['carName'];
        // Set gallery, if the ID comes up again, it will be reset but won't hurt anything
        $cars[$row_showall['carName_id']]['gallery'] = $row_showall['gallery'];
    
        // Now, we add a new property called 'details' and put a new array in...
        $cars[$row_showall['carName_id']]['details'][] = array(
            // Storing a unique day
            'days'   => ['rental_days'],
            // And a unique amount
            'amount' => ['rental_amount']
        );
    }
    
    // Loop over each entry in our array...
    foreach($cars as $car) {
        // Print out the name, once
        echo $car['name'] . '<br />';
        // Print out the gallery, once
        echo $car['gallery'] . '<br />';
    
        // Loop over all the details...
        foreach($car['details'] as $detail) {
            // Print out each day
            echo $detail['days']  . '<br />';
            // Print out each amount
            echo $detail['amount']  . '<br />';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。