0

At this point, I can see all listed cities. I would like to see only those in which there is an order. How to do it? Thank you in advance for any help! The script is written in cakephp, unfortunately I'm just learning and mastering this framework from scratch. I hope it is not complicated, or if I can do it on this code, do you need to rebuild the controller?

 <?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
    Seems you would to loop on `orders` then, not a list of `cities`. What is this `$list_city`? A list of all cities? Could do a cosmetic fix but ideally you should iterate on what you want, not what you have available. – ficuscr Jun 04 '19 at 19:32
  • i.e. why the need to hide empty fields and how does that scale with "a big number" of cities. – ficuscr Jun 04 '19 at 19:35
  • $list_city - $list_city = $this->Orders->Customers->Cities->find('all', ['order'=>['name'=>'asc']]); – Wojtek Ka Jun 04 '19 at 19:39
  • At the moment I have a list of 20 cities and if I have an order in only one, I can see a list of all 20, and under this one is an order. – Wojtek Ka Jun 04 '19 at 19:40
  • Me, I would get rid of the `list_city` and just have `$orders` returned with a sort on city. One loop. Or, simple cosmetic fix could be as simple as some CSS like [Hide empty cells in table](https://stackoverflow.com/questions/12023771/hide-empty-cells-in-table) – ficuscr Jun 04 '19 at 19:47
  • I know that I am asking you a lot, but would you please write it in the easiest way? I am not a programmer, but unfortunately my present, has a great project and there is no time to improve it :( Thank you in advance! – Wojtek Ka Jun 04 '19 at 19:51

1 Answers1

0

I would do something like I show below. Ideally you would have the database return something closer to this structure to start with.

Forget the list of cities, we don't care about cities we don't have orders for is what you are saying. So, loop on orders, key them by city.

<?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
        }
    }

ficuscr
  • 6,975
  • 2
  • 32
  • 52
  • So I am going to dump everything, the whole table and it will list me orders like that? And here ... // echo details for each order I have to insert fields from the table what's in the order yes? – Wojtek Ka Jun 04 '19 at 20:36
  • Whatever the query that returns `$orders` now is should be suitable, it's not returning the whole table is it? I assumed each record in `$orders` was an order you need to display. And yes, you should be able to just copy and paste most of that as the scope is still `$order`... So this still works for example: `= h($order->description) ?>` – ficuscr Jun 04 '19 at 20:38
  • Trying to keep it readable. Everything you have in the if block for `if($order->customer->city->name == $city->name):` could be moved to where I have the placeholder `//echo details for each order`. Don't just want to do it for you, ideally have you learn something here too! This making sense? – ficuscr Jun 04 '19 at 20:41
  • I can not paste the whole code, because it's too long, I understand what you write, only I do not know php and its syntax, so that's what you put in, but it does not work :( – Wojtek Ka Jun 04 '19 at 21:05