如何隐藏表格中的空字段?

时间:2019-06-04 19:25:59

标签: php cakephp cakephp-3.0

这时,我可以看到所有列出的城市。我只想查看有订单的订单。怎么做?预先感谢您的任何帮助!该脚本是用cakephp编写的,不幸的是,我只是从头开始学习和掌握这个框架。我希望它并不复杂,或者如果我可以在此代码上完成操作,是否需要重建控制器?

 <?php foreach ($list_city as $city): ?>
        <span class="cities"><?= h($city->name) ?></span>
      <?php $licz=1; foreach ($orders as $order): ?>
        <?= '<table>' ?>

        <?php if($order->customer->city->name == $city->name): ?>
          <td style="width: 10%; text-align: center;">
                <span>
              <?= $this->Form->postLink('<span class="status-icon icon-circle-empty"</span>', ['action' =>'fajrant', $order->id],
                                ['title'=>__('Dostarczono?'), 'escape'=>false,
                                'confirm' => __('Czy dostarczono zamówienie: {0}?', $order->customer->address)]) ?>
            </span>
          </td>
          <td>
            <span>
                 <?= h($order->customer->address) ?>
            </span>

            <?php if($order->product_1>0) {
             echo ('<span class="produkt_butt">');
             echo $this->Number->format($order->product_1) . ' szt' . '</span>'; }

             if($order->product_2>0) {
               echo ('<span class="produkt_butt1">');
               echo $this->Number->format($order->product_2) . ' szt' . '</span>'; }
             ?>

            <span><?= h($order->description) ?></span>
          </td>
          <td>
            <span><?= $this->Number->currency($order->price,'',['precision' => 0]) ?></span>
          </td>
          <td>
              <a href="tel:<?= h($order->customer->phone); ?>" target="_blank"></a>
          </td>
          <td>
            <span><?= $this->Html->link(__('') . '', ['action' =>'view', $order->id], ['title'=>'Zobacz zamówienie', 'class'=>'chev-icon icon-chev-right']) ?></span>

          </td>
        <?php endif ?>
        <?= '</table>' ?>

1 个答案:

答案 0 :(得分:0)

我会做类似下面的事情。理想情况下,您应该让数据库返回更接近于此结构的内容。

忘记城市列表,我们不在乎您要说的没有订单的城市。因此,按订单循环,按城市键。

<?php

    $ordersByCity = [];
    foreach ($orders as $order) {
        $ordersByCity[$order->customer->city->name][] = $order;
    }

    //var_dump($ordersByCity);

    foreach ($ordersByCity as $customerCityName => $orders) {
        //loop on each city. We could know how many to expect with a simple `count($orders)`
        echo 'span class="cities">' . h($customerCityName) . '</span>';
        foreach ($orders as $order) {
            ...  //echo details for each order
        }
    }