Codeigniter MySQL组结果成多维数组


Codeigniter MySQL Group Results into MultiDimensional Arrays

不确定这个问题的最佳措辞(因此无法搜索我想要达到的目标),但这是我想要达到的目标:

我可以像这样从表中提取一组结果:

$this->db->select('sets.id,
                   sets.wo_id,
                   sets.weight,
                   sets.reps,
                   exercise_list.title');
$this->db->from('sets');
$this->db->join('exercise_list','sets.ex_id= exercise_list.id');
$this->db->where('sets.wo_id',$wo_id);
//return $query->result_array();
$q = $this->db->get();
$query = $q->result_array();
return $query;
不用太担心它在做什么,它返回一个数组(只显示我使用的位),像这样:
******************************
* title  *  weight  *  reps  *
******************************
* exer1  *  25      *   6    *
* exer1  *  25      *   5    *
* exer1  *  25      *   5    *
* exer3  *  80      *   7    *
* exer3  *  80      *   7    *
* exer3  *  80      *   6    *
******************************

我现在所做的只是通过这个表的foreach循环来显示它们:

<?php foreach($sets as $set): ?>
  <tr>
    <td><?php echo $set['title']; ?></td>
    <td><?php echo $set['weight']; ?></td>
    <td><?php echo $set['reps']; ?></td>
  </tr>
<?php endforeach; ?>

与上面的mysql数组非常相似。

但我想实现的是更像这样的东西(仅限示例):

<div (for example)> 
    <tr>
      <td colspan="3"><?php echo $title; ?></td>
    <tr>
    <tr>
      <th>Set No.</th><th>Weight</th><th>Reps</th>
    <tr>
    <tr>
      <th> 1 </th><th> 25 </th><th> 6 </th>
    <tr>
    <tr>
      <th> 1 </th><th> 25 </th><th> 5 </th>
    <tr>
    <tr>
      <th> 1 </th><th> 25 </th><th> 5 </th>
    <tr>
</div>
<div (for example)> 
    <tr>
      <td colspan="3"><?php echo $NextTitle; ?></td>
    <tr>
    <tr>
      <th>Set No.</th><th>Weight</th><th>Reps</th>
    <tr>
    <tr>
      <th> 1 </th><th> 80 </th><th> 7 </th>
    <tr>
    <tr>
      <th> 1 </th><th> 80 </th><th> 7 </th>
    <tr>
    <tr>
      <th> 1 </th><th> 80 </th><th> 6 </th>
    <tr>
</div>

所以它会呈现这样的内容:

***************************
*         Exer1           *
***************************
* Set No. * Weight * Reps *
***************************
*    1    *   25   *  6   *
*    2    *   25   *  5   *
*    3    *   25   *  5   *
***************************
***************************
*         Exer2           *
***************************
* Set No. * Weight * Reps *
***************************
*    1    *   80   *  7   *
*    2    *   80   *  7   *
*    3    *   80   *  6   *
***************************

对不起,这是一个很长的帖子,希望它清楚我在努力做什么。我认为这是沿着我从mysql中获取数据,逐行迭代并将其添加到子数组的行*。

所以我应该得到一个像这样的数组
array
(
  exer1
  (
    array(25,6), array(25,5), array(25,5)
  )
  exer2
  (
    array(80,7), array(80,6), array(80,6)
  )
)

天哪,希望这能说得通。

提前感谢!

Jon

解决这个问题有不同的方法。如果你想用纯php的方式解决这个问题,而不改变你的MySQL代码,你可以:

$title = "";
$i = 1;
<?php foreach($sets as $set):
    if($title != $set['title']){
         $i = 1;
         if($title != "") { echo "</div>";}?>
         <div> 
         <tr>
             <td colspan="3"><?php echo $set['title']; ?></td>
         <tr>
         <tr>
             <th>Set No.</th><th>Weight</th><th>Reps</th>
         <tr>
         <tr>
    <?php
    }
    ?>
    <tr>
        <td><?php echo $i; $i++; ?></td>
        <td><?php echo $set['weight']; ?></td>
        <td><?php echo $set['reps']; ?></td>
    </tr>
    <?php
    $title = $set['title'];
endforeach; 
?>