2014-04-24 63 views
0

我有個PHP對象輸出爲:分組的PHP對象

Array ( 
    [0] => stdClass Object ( 
     [id] => 25 
     [itemid] => 13 
     [studentid] => 9 
     [score] => 22 
     [imagealt] => 
     [timemodified] => 0 
     [data] => 
     [firstname] => studentfirst9 
     [lastname] => studentlast9 
    ) 
    [1] => stdClass Object ( 
     [id] => 26 
     [itemid] => 13 
     [studentid] => 10 
     [score] => 23 
     [imagealt] => 
     [timemodified] => 0 
     [data] => 
     [firstname] => studentfirst10 
     [lastname] => studentlast10 
    ) 
    [2] => stdClass Object ( 
     [id] => 1 
     [itemid] => 1 
     [studentid] => 1 
     [score] => 2 
     [imagealt] => 
     [timemodified] => 0 
     [data] => 
     [firstname] => studentfirst1 
     [lastname] => studentlast1 
    ) 
    [3] => stdClass Object ( 
     [id] => 2 
     [itemid] => 1 
     [studentid] => 2 
     [score] => 3 
     [imagealt] => 
     [timemodified] => 0 
     [data] => 
     [firstname] => studentfirst2 
     [lastname] => studentlast2 
    ) 
    [4] => stdClass Object ( 
     [id] => 9 
     [itemid] => 5 
     [studentid] => 1 
     [score] => 6 
     [imagealt] => 
     [timemodified] => 0 
     [data] => 
     [firstname] => studentfirst1 
     [lastname] => studentlast1 
    ) 
    [5] => stdClass Object ( 
     [id] => 10 
     [itemid] => 5 
     [studentid] => 2 
     [score] => 7 
     [imagealt] => 
     [timemodified] => 0 
     [data] => 
     [firstname] => studentfirst2 
     [lastname] => studentlast2 
    ) 
    [6] => stdClass Object ( 
     [id] => 17 
     [itemid] => 9 
     [studentid] => 1 
     [score] => 10 
     [imagealt] => 
     [timemodified] => 0 
     [data] => 
     [firstname] => studentfirst1 
     [lastname] => studentlast1 
    ) 
    [7] => stdClass Object ( 
     [id] => 18 
     [itemid] => 9 
     [studentid] => 2 
     [score] => 11 
     [imagealt] => 
     [timemodified] => 0 
     [data] => 
     [firstname] => studentfirst2 
     [lastname] => studentlast2 
    ) 
    [8] => stdClass Object ( 
     [id] => 3 
     [itemid] => 2 
     [studentid] => 1 
     [score] => 3 
     [imagealt] => 
     [timemodified] => 0 
     [data] => 
     [firstname] => studentfirst1 
     [lastname] => studentlast1 
    ) 
    [9] => stdClass Object ( 
     [id] => 4 
     [itemid] => 2 
     [studentid] => 2 
     [score] => 4 
     [imagealt] => 
     [timemodified] => 0 
     [data] => 
     [firstname] => studentfirst2 
     [lastname] => studentlast2 
    ) 
    [10] => stdClass Object ( 
     [id] => 11 
     [itemid] => 6 
     [studentid] => 1 
     [score] => 7 
     [imagealt] => 
     [timemodified] => 0 
     [data] => 
     [firstname] => studentfirst1 
     [lastname] => studentlast1 
    ) 
    [11] => stdClass Object ( 
     [id] => 12 
     [itemid] => 6 
     [studentid] => 2 
     [score] => 8 
     [imagealt] => 
     [timemodified] => 0 
     [data] => 
     [firstname] => studentfirst2 
     [lastname] => studentlast2 
    ) 
    [12] => stdClass Object ( 
     [id] => 19 
     [itemid] => 10 
     [studentid] => 1 
     [score] => 11 
     [imagealt] => 
     [timemodified] => 0 
     [data] => 
     [firstname] => studentfirst1 
     [lastname] => studentlast1 
    ) 
    [13] => stdClass Object ( 
     [id] => 20 
     [itemid] => 10 
     [studentid] => 2 
     [score] => 12 
     [imagealt] => 
     [timemodified] => 0 
     [data] => 
     [firstname] => studentfirst2 
     [lastname] => studentlast2 
    ) 
    [14] => stdClass Object ( 
     [id] => 7 
     [itemid] => 4 
     [studentid] => 1 
     [score] => 5 
     [imagealt] => 
     [timemodified] => 0 
     [data] => 
     [firstname] => studentfirst1 
     [lastname] => studentlast1 
    ) 
    [15] => stdClass Object ( 
     [id] => 8 
     [itemid] => 4 
     [studentid] => 2 
     [score] => 6 
     [imagealt] => 
     [timemodified] => 0 
     [data] => 
     [firstname] => studentfirst2 
     [lastname] => studentlast2 
    ) 
) 

如,可以看到,僅存在兩個studentid具有不同itemid。我想將它們分組,並打印作爲表所示:

    itemidid1 itemidid2 itemidid3 
studentid1   x1   x2    x3 
studentid2   y1   y2    y3 

我想下面的代碼,但它顯示的所有值下的[其他。

<?php foreach ($hits as $hit): ?> 
      <?php if ($hit->studentid == $current_id): ?> 
       <td> <?php echo $hit->score; ?> </td> 
      <?php else: ?> 
       <?php if ($current_id != -1): ?> 
        </tr> 
       <?php endif; ?> 
       <?php $current_id = $hit->studentid; ?> 
       <tr> 
        <td><a href="#" id="<?php echo $hit->studentid; ?>" class="student-grade-data"> <?php echo $hit->firstname.' '.$hit->lastname; ?> </a></td> 
        <td> <?php echo $hit->score; ?> 
       </td> 
      <?php endif; ?> 
     <?php endforeach; ?> 
+0

「正如人們可以看到」...該數組輸出是巨大的。你能減少示例輸入嗎? –

+0

首先遍歷所有對象並將它們存儲在由ID分組的緩衝區數組中,然後循環遍歷該緩衝區並輸出HTML –

+0

顯示您的查詢,'select * ....' – SagarPPanchal

回答

0

你要實現查詢,對所有對象添加group by條款,如group by field name

0

環和兩個數組喜歡收集的對象:

$byStudentId[$hit->studentid] = $hit; 
$byItemId[$hit->itemId] = $hit; 

那麼你可以得到的列通過array_keys($byItemId)和finaly建立表循環超過$ byStudenId