Home > Backend Development > PHP Tutorial > laravel5 - 关于判断循环 Laravel 5 php

laravel5 - 关于判断循环 Laravel 5 php

WBOY
Release: 2016-06-06 20:33:56
Original
1243 people have browsed it

<code> @foreach($pages as $page)
                @if (count($page->nav_id) === 2)
                    <li><a href="/pages/%7B%7B%20%24page->id%20%7D%7D">{{ $page->title }}</a></li>
                @endif
@endforeach
</code>
Copy after login
Copy after login

数据库的 内容 是

laravel5 - 关于判断循环 Laravel 5 php

分 2中导航

现在是在视图里面做判断

当 nav_id = 2 的时候循环出来

这样做好像循环不出来 哪里写错了吗

回复内容:

<code> @foreach($pages as $page)
                @if (count($page->nav_id) === 2)
                    <li><a href="/pages/%7B%7B%20%24page->id%20%7D%7D">{{ $page->title }}</a></li>
                @endif
@endforeach
</code>
Copy after login
Copy after login

数据库的 内容 是

laravel5 - 关于判断循环 Laravel 5 php

分 2中导航

现在是在视图里面做判断

当 nav_id = 2 的时候循环出来

这样做好像循环不出来 哪里写错了吗

int count ( mixed $var [, int $mode = COUNT_NORMAL ] )
count — 计算数组中的单元数目或对象中的属性个数

而你$page->nav_id本身就是一个值了,所以count($page->nav_id) 永远等于1,所以需要把count()去掉
直接$page->nav_id === 2 就可以判断了~~

把条件改为

<code>($page->nav_id) === 2
</code>
Copy after login

楼上正解,count 是计算个数,而你的count($page->nav_id) 无关是如何计算,数值都是1, 1不等于2,所以循环不出

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template