php - 如何更改代码,使其不仅适用于静态值 "general"和 "plus"?

标签 php laravel

session 中的注册有 1 个或多个关联的参与者,并且与注册关联的每个参与者都与注册类型相关联。

我有一个查询来获取与特定注册参与者关联的每个注册类型的信息(名称和价格),查询结果是:

Registration {#258 ▼
  ...
  #relations: array:1 [▼
    "participants" => Collection {#262 ▼
      #items: array:2 [▼
        0 => Participant {#269 ▼
          #fillable: array:4 [▶]
          ...
          #relations: array:1 [▼
            "registration_type" => RegistrationType {#274 ▶}
          ]
          ... // then RegistrationType has attributes: name, price, etc
        }
        1 => Participant {#271 ▼
          #fillable: array:4 [▶]
          ...
          #relations: array:1 [▼
            "registration_type" => RegistrationType {#274 ▼
              ... // then RegistrationType has attributes: name, price, etc
            }
          ]
        }
      ]
    }
  ]
 ...
}

现在我想显示注册摘要,例如:

Registration type        Quantity            Price           Subtotal
general                1                  0.00             0.00
plus                   2                  2.00             4.00

我有下面的代码来获取此摘要,但它使用静态注册类型名称(一般“和加号”)。但是注册类型可以有任何名称,创建 session 的用户可以使用任何名称创建注册类型. 您知道如何使用任何注册类型名称而不只是静态值“general”和“plus”来实现相同的摘要吗?

<ul class="list-group">
    <li>
        <span>Registration Type</span>
        <span>Quantity</span>
        <span>Price</span>
        <span>Subtotal</span>
    </li>

    <?php
    $general_type_count = 0;
    $plus_type_count = 0;
    foreach($registrationTypeDetails->participants as $p){
        if($p->registration_type['name'] === 'general')
            $general_type_count ++;
        if($p->registration_type['name'] === 'plus')
            $plus_type_count ++;
    }
    $plus = 0; $general = 0;
    ?>
    @foreach($registrationTypeDetails->participants as $participant)
        <li>
            <span> {{$participant->registration_type['name']}} </span>
            <span>
                @if($participant->registration_type['name'] === 'general')
                    {{$general_type_count}}
                @endif
                @if($participant->registration_type['name'] === 'plus')
                    {{$plus_type_count}}
                @endif
             </span>
            <?php
            if ($participant->registration_type['name'] === 'general')
                $general++;
            if ($participant->registration_type['name'] === 'plus')
                $plus++;
            ?>
            <span class="font-size-sm">{{$participant->registration_type['price']}} </span>
            <span class="font-size-sm">{{$participant->registration_type['price'] * $general_type_count}} </span>
        </li>
    @endforeach
</ul>

返回“registrationTypeDetails”的 Controller :

class PController extends Controller
{
    public function showSummary($id = "", $slug = "", $regID)
    {
      $registrationDetails = Registration::with('participants:id,ticket_type_id,registration_id')->find($registrationID);
        return view('conferences.showSummary', compact(registrationTypeDetails'));
    }
}

最佳答案

您可以使用数组代替变量:

<ul class="list-group">
    <li>
        <span>Registration Type</span>
        <span>Quantity</span>
        <span>Price</span>
        <span>Subtotal</span>
    </li>

    <?php
    $type_counts = [];
    foreach($registrationTypeDetails->participants as $p){
        if (!isset($type_counts[$p->registration_type['name']])) {
            $type_counts[$p->registration_type['name']] = 0;
        }
        $type_counts[$p->registration_type['name']]++;
    }
    ?>
    @foreach($registrationTypeDetails->participants as $participant)
        <li>
            <span> {{$participant->registration_type['name']}} </span>
            <span>
                {{$type_counts[$participant->registration_type['name']]}}
             </span>
            <span class="font-size-sm">{{$participant->registration_type['price']}} </span>
            <span class="font-size-sm">{{$participant->registration_type['price'] * $type_counts[$participant->registration_type['name']]}} </span>
        </li>
    @endforeach
</ul>

我删除了未使用的$generalplus。如果需要,您可以为他们做同样的事情。

$type_counts[$participant->registration_type['name']] 使用一个变量也是一个好主意,它经常被使用。这样可以避免很多重复:

<ul class="list-group">
    <li>
        <span>Registration Type</span>
        <span>Quantity</span>
        <span>Price</span>
        <span>Subtotal</span>
    </li>

    <?php
    $type_counts = [];
    foreach($registrationTypeDetails->participants as $p){
        $name = $p->registration_type['name'];
        if (!isset($type_counts[$name])) {
            $type_counts[$name] = 0;
        }
        $type_counts[$name]++;
    }
    ?>
    @foreach($registrationTypeDetails->participants as $participant)
        @php ($name = $participant->registration_type['name'])
        <li>
            <span> {{$name]}} </span>
            <span>
                {{$type_counts[$name]}}
             </span>
            <span class="font-size-sm">{{$participant->registration_type['price']}} </span>
            <span class="font-size-sm">{{$participant->registration_type['price'] * $type_counts[$name]}} </span>
        </li>
    @endforeach
</ul>

关于php - 如何更改代码,使其不仅适用于静态值 "general"和 "plus"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50779708/

相关文章:

javascript - PHP/JS : Auto click button in iframe when its opened

PHP获取数组数组中键的最小值

php - Laravel IronMq 队列的意外并行执行

php - 性能明智 : which is better in Laravel Database

php - 如何用bb代码改变背景颜色

php - 清除表中的所有数据,但保留列/结构

php - 从 MySQL 到 PHP 页面选择数据

java - 上传到服务器后图像文件为空

php - 如何在 laravel 4.2 中使用带有自定义预过滤器的 AJAX 上传 CSV 文件

类不存在