仅在更改时显示列

时间:2015-04-08 07:46:06

标签: php mysql laravel

我有这张桌子:

some_id | page_id | column1 | column2 | column3 | column4 |
   1    |   1     |    1    |    1    |    1    |    1    | 
   1    |   2     |    1    |    0    |    1    |    1    | 
   1    |   3     |    1    |    0    |    1    |    1    | 
   2    |   2     |    1    |    0    |    0    |    1    | 
   2    |   3     |    1    |    0    |    1    |    0    |
   3    |   6     |    0    |    0    |    0    |    0    |  

在此表中, some_id page_id 是外键。

现在我在php中显示(在表格中)如此:

some_id | page_id | column1 | column2 | column3 | column4 |
   some1|   page1 |    yes  |    yes  |    yes  |    yes  | 
   some1|   page2 |    yes  |    no   |    yes  |    yes  | 
   some1|   page3 |    yes  |    no   |    yes  |    yes  | 
   some2|   page2 |    yes  |    no   |    no   |    yes  | 
   some2|   page3 |    yes  |    no   |    yes  |    no   |
   some3|   page6 |    no   |    no   |    no   |    no   |

但我希望这样:

some_id | page_id | column1 | column2 | column3 | column4 |
   some1|   page1 |    yes  |    yes  |    yes  |    yes  | 
        |   page2 |    yes  |    no   |    yes  |    yes  | 
        |   page3 |    yes  |    no   |    yes  |    yes  | 
   some2|   page2 |    yes  |    no   |    no   |    yes  | 
        |   page3 |    yes  |    no   |    yes  |    no   |
   some3|   page6 |    no   |    no   |    no   |    no   |

所以基本上每当有新的 some_id 时,它才会显示结果。如果没有,则应为空td。我不知道该怎么做。

这是包含查询结果的数组:

array(10) {
  [0]=>
  object(stdClass)#351 (6) {
    ["some_id"]=>
    int(2)
    ["page_id"]=>
    int(1)
    ["column1"]=>
    int(1)
    ["column2"]=>
    int(0)
    ["column3"]=>
    int(1)
    ["column4"]=>
    int(0)
  }
  [1]=>
  object(stdClass)#352 (6) {
    ["some_id"]=>
    int(2)
    ["page_id"]=>
    int(6)
    ["column1"]=>
    int(1)
    ["column2"]=>
    int(1)
    ["column3"]=>
    int(0)
    ["column4"]=>
    int(1)
  }
  [2]=>
  object(stdClass)#353 (6) {
    ["some_id"]=>
    int(2)
    ["page_id"]=>
    int(7)
    ["column1"]=>
    int(0)
    ["column2"]=>
    int(0)
    ["column3"]=>
    int(0)
    ["column4"]=>
    int(0)
  }
  [3]=>
  object(stdClass)#354 (6) {
    ["some_id"]=>
    int(4)
    ["page_id"]=>
    int(1)
    ["column1"]=>
    int(1)
    ["column2"]=>
    int(1)
    ["column3"]=>
    int(1)
    ["column4"]=>
    int(1)
  }
  [4]=>
  object(stdClass)#355 (6) {
    ["some_id"]=>
    int(4)
    ["page_id"]=>
    int(2)
    ["column1"]=>
    int(1)
    ["column2"]=>
    int(1)
    ["column3"]=>
    int(1)
    ["column4"]=>
    int(1)
  }
  [5]=>
  object(stdClass)#356 (6) {
    ["some_id"]=>
    int(4)
    ["page_id"]=>
    int(3)
    ["column1"]=>
    int(1)
    ["column2"]=>
    int(1)
    ["column3"]=>
    int(1)
    ["column4"]=>
    int(1)
  }
  [6]=>
  object(stdClass)#357 (6) {
    ["some_id"]=>
    int(4)
    ["page_id"]=>
    int(4)
    ["column1"]=>
    int(1)
    ["column2"]=>
    int(1)
    ["column3"]=>
    int(1)
    ["column4"]=>
    int(1)
  }
  [7]=>
  object(stdClass)#358 (6) {
    ["some_id"]=>
    int(4)
    ["page_id"]=>
    int(5)
    ["column1"]=>
    int(1)
    ["column2"]=>
    int(1)
    ["column3"]=>
    int(1)
    ["column4"]=>
    int(1)
  }
  [8]=>
  object(stdClass)#359 (6) {
    ["some_id"]=>
    int(4)
    ["page_id"]=>
    int(6)
    ["column1"]=>
    int(1)
    ["column2"]=>
    int(1)
    ["column3"]=>
    int(1)
    ["column4"]=>
    int(1)
  }
  [9]=>
  object(stdClass)#360 (6) {
    ["some_id"]=>
    int(4)
    ["page_id"]=>
    int(7)
    ["column1"]=>
    int(1)
    ["column2"]=>
    int(1)
    ["column3"]=>
    int(1)
    ["column4"]=>
    int(1)
  }
}

这就是我展示它的方式:

@foreach ($array as $something)
<tr>
<td>{{$something->some_id}}</td>
<td>{{$something->page_id}}</td>
<td>{{$something->column1}}</td>
<td>{{$something->column2}}</td>
<td>{{$something->column3}}</td>
<td>{{$something->column4}}</td>

</tr>
@endforeach

如何将其作为我上面描述和显示的理想结果?每个 some_id 只显示一次,并且在第一次出现的行中

2 个答案:

答案 0 :(得分:0)

只需使用PHP,您就不需要SQL来执行此操作。

在循环初始化变量之前跟踪some_id的值。

我不熟悉您使用的符号,所以我按照我的方式设置它。

<?php
    $currId = '';
    @foreach ($array as $something) {
?>
      <tr>
        <?php if ($something->some_id != $currId) {
          $currId = $something->some_id;
        ?>

          <td><?php echo $something->some_id; ?></td>

        <?php } else { ?>

          <td>&nbsp;</td>

        <?php } ?>

        <td><?php echo $something->page_id]; ?></td>
        <td><?php echo $something->column1; ?></td>
        <td><?php echo $something->column2; ?></td>
        <td><?php echo $something->column3; ?></td>
        <td><?php echo $something->column4; ?></td>

      </tr>
    <?php } ?>

答案 1 :(得分:0)

如果您想让模板干净,可以这样做:

$previous = '';

$callback = function(&$item, $key, &$previous) {
    if ($item->some_id == $previous) {
        $item->some_id = '&nbsp;';
    } else {
        $previous = $item->some_id;
    }
};

array_walk($array, $callback, $previous);