【问题标题】:Codeigniter MySQL Group Results into MultiDimensional ArraysCodeigniter MySQL 组结果到多维数组中
【发布时间】:2014-08-05 11:44:52
【问题描述】:

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

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

$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 中获取数据,逐行遍历它并将其添加到每个 exer* 的子数组中。

所以我应该得到一个类似这样的数组:

array
(
  exer1
  (
    array(25,6), array(25,5), array(25,5)
  )
  exer2
  (
    array(80,7), array(80,6), array(80,6)
  )
)

天哪,希望这是有道理的。

提前致谢!

乔恩

【问题讨论】:

    标签: php mysql arrays codeigniter multidimensional-array


    【解决方案1】:

    有不同的方法来解决这个问题。如果你想要一个纯 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; 
    ?>
    

    【讨论】:

    • 非常好,非常感谢。这比像我认为我将不得不处理我的 MySQL 语句那样过于复杂化要好得多。
    猜你喜欢
    • 1970-01-01
    • 2019-10-15
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    相关资源
    最近更新 更多