2013-02-18 47 views
2

如何根據相關ID打印數據?

Product  Plan   ProductPlan 
id |name  id |name  id | product_id | plan_id  
1 aplha  1 a   1  1   2    
2 bravo  2 b   2  4   c 
3 charlie 4 c 
4 delta 

我想打印計劃名稱對相關產品,像

查看,我想

alpha | delta | 
    a   c 
    b 

,但它顯示我像,

alpha | bravo | charlie |delta 
a 
b  
c 

這是我的控制器代碼

$p_plan = $this->ProductPlan->find('all',array(
            'conditions'=>array(
               'ProductPlan.product_id'=>'Product.product_id' 
               )      
             ) 
            ); 
    $this->set('p_plan', $p_plan); 

這是我的看法代碼,

<table> 
<thead> 
    <tr> 
     <?php foreach ($p as $ps){?> 
     <th> 
       <?php echo __l($ps['Product']['name']);?> 

     </th> 
     <?php }?> 
    </tr> 
</thead>  
    <tbody> 
     <?php foreach ($p as $p1){ 
        foreach($p1['Plan'] as $plan){ 
         debug($plan); 
     ?> 
     <tr> 
      <td> 
       <?php echo __l($plan['name']);?>  
      </td> 
     </tr>   
     <?php } 
        }?> 

     </tbody> 

什麼是錯的,我的看法?如果一些身體幫我,我會很thaksful他。提前致謝。

+3

請標記您正在使用的cakephp的唯一版本。 – Rikesh 2013-02-18 13:33:23

回答

1

顯然我不能評論,但我會盡力回答。既然你沒有在你的視圖中提及p_plan,並且你不顯示$ p是否被創建,那麼很難分辨實際正在發生的事情。

的工作流程: - 獲取3個表 - 按他們進入plan_id的數據類型 PRODUCT_ID =>的數組的數組 - 跟蹤product_ids到列號 - 該數組傳遞給視圖 - 視圖打印數組作爲行=列

控制器:

//get just the productplan table 
$p_plan = $this->ProductPlan->find('all',array(
    'conditions'=>array(
      'ProductPlan.product_id'=>'Product.product_id' 
    ),'recursive' => -1 
)); 

//get just the products table 
$products = $this->Product->find('list',array(
    'fields' => array('id','name') 
)); 

//get just the plans table 
$plans = $this->Plan->find('list',array(
    'fields' => array('id','name') 
)); 

//holder is a table to store counting variables 
$holder = array(null); 
//final_p_plan will be passed to the view. Array of arrays sorted as product_id => plan_ids 
$final_p_plan = array(null); 
//p_plan_ids will be passed to the view. Stores an array of used product id 
$p_plan_ids = array(null); 
//max_length is keeping track of how long the longest one is. will be passed to view. needed for the loop. 
$max_length = 0; 

//want to end up with an array we can just loop through and print off 
foreach($p_plan as $p) 
{ 
    //if it's empty set row 0 as product name, then row 1 as the active plan name. else set the next row to the next plan name 
    if(empty($holder[[$p['product_id']]])) 
    { 
     $holder[[$p['product_id']]]['i'] = 2; 
     $final_p_plan[$p['product_id']][0] = $products[$p['product_id']]; 
     $final_p_plan[$p['product_id']][1] = $plans[$p['plan_id']]; 

     array_push($p_plan_ids,$p['product_id']); 
    } else { 
     $final_p_plan[$p['product_id']][$holder[[$p['product_id']]]['i']] = $plans[$p['plan_id']]; 
     $holder[[$p['product_id']]]['i'] = $holder[[$p['product_id']]]['i'] + 1; 
     if($holder[[$p['product_id']]]['i'] > $max_length) 
     { 
      $max_length = $holder[[$p['product_id']]]['i']; 
     } 
    } 
} 

$this->set('final_p_plan', $final_p_plan); 
$this->set('p_plan_ids', $p_plan_ids); 
$this->set('max_length', $max_length); 

檢視:

<table> 
    <?php $i = 0; while($i < max_length): ?> 
     <?php if($i == 0): ?> 
      <thead> 
       <tr> 
        <?php foreach($p_plan_ids as $id): ?> 
         <th> 
          <?php echo $final_p_plan[$id][$i]; ?> 
         </th> 
        <?php endforeach; ?> 
       </tr> 
      </thead> 
      <tbody> 
     <?php else: ?> 
      <tr> 
       <?php foreach($p_plan_ids as $id): ?> 
        <th> 
         <?php echo $final_p_plan[$id][$i]; ?> 
        </th> 
       <?php endforeach; ?> 
      </tr> 
     <?php endif; $i++; ?> 
    <?php endwhile; ?> 
    </tbody> 
</table> 
+0

nope沒有工作,沒有必要加入hasAndbelognstoMany關係。 – usii 2013-02-19 05:48:24

+0

對不起,寫這個假設問題中給出的是所有的東西。你能告訴我什麼對此不起作用嗎? – iexiak 2013-02-19 12:44:11