duan19911992 2013-04-15 08:23
浏览 13

循环访问数据库并返回相关数据

In my controller I have this Code to loop through database and return the data

$faultgroup = $this->booking_model->Get_Fault_Group_Display($grouptype); 

$data['Get_Fault_Group_Display'] = $faultgroup; $getresults = array(); 
$data['get_fault_group_data'] = array(); 

foreach ($faultgroup as $key ) { 
$show = $key->Showgroup; 
$getresults = $this->booking_model->get_fault_group_data($grouptype,$show);
$data['get_fault_group_data'] = $getresults ; 

}


In my View i have this Code to loop through each record with the specific grouptype and display record (to_do_item) from database that match that grouptype

<?php if ( ! is_null($Get_Fault_Group_Display)): ?>
<?php if (count($Get_Fault_Group_Display)): ?>
<?php foreach ($Get_Fault_Group_Display as $result): ?>

<?php echo $result->Showgroup; ?>                                                                   
<?php foreach ($get_fault_group_data as $key) :?>

<?php echo $key->to_do_item; ?>


<?php endforeach ?>
<?php endforeach ?>



 <?php else: ?>


<?php endif ?>

My problem is only the last row is shown on all the grouptypes because the loop keeps overiding $data['get_fault_group_data'] with the new $getresults

  • 写回答

1条回答 默认 最新

  • duancuisan2503 2013-04-15 08:42
    关注

    Shouldn't you use the $data['get_fault_group_data'] as an array?

    Controler:

    $data['get_fault_group_data'][$key] = $getresults ; 
    

    View:

        <?php if ( ! is_null($Get_Fault_Group_Display)): ?>
    <?php if (count($Get_Fault_Group_Display)): ?>
    <?php foreach ($Get_Fault_Group_Display as $i => $result): ?>
    
    <?php echo $result->Showgroup; ?>                                                                   
    <?php foreach ($get_fault_group_data[$i] as $key) :?>
    
    <?php echo $key->to_do_item; ?>
    
    
    <?php endforeach ?>
    <?php endforeach ?>
    
    
    
     <?php else: ?>
    
    
    <?php endif ?>
    
    评论

报告相同问题?