-3

我正在使用 CMS ProscessWire。

我有一个工作 foreach 循环,显示孩子的标题和 5 个链接。链接显示为图像。这完美无瑕。但后来我想要一个空链接一个黑白图像来显示没有链接。所以我做了一个if语句。但是,当我将 if 语句放入循环中时,显示的唯一子项是带有空链接的子项。

有谁知道如何解决这个问题?我认为这是一件简单的事情,但无法弄清楚。

PHP代码:

    $rdio='rdio.png';
    $itunes='itunes.png';
    $xbox='xbox.png';
    $googleplay='googleplay.png';
    $spotify='spotify.png';
    $deezer='deezer.png';

    foreach($page->children as $child)

    if(!$child->Rdio) {
$rdio='rdiog.png';
     }
    else
     {
    $rdio='rdio.png';
     }

       echo "<ul style='background-color:#CCC;'>"."<li><h1>{$child->title}</h1></li>".<li><a class='rdio' href='{$child->Rdio}'><img src='../site/templates/img/$rdio' alt='rdio'/></a></li>"."<li><a class='itunes' href='{$child->iTunes}'><img src='../site/templates/img/$itunes' alt='itunes'/></a></li>"."<li><a class='xbox' href='{$child->Xbox}'><img src='../site/templates/img/$xbox' alt='xbox'/></a></li>"."<li><a class='googleplay' href='{$child->GooglePlay}'><img src='../site/templates/img/$googleplay' alt='googleplay'/></li>"."<li><a class='spotify' href='{$child->Spotify}'><img src='../site/templates/img/$spotify' alt='spotify'/></a></li>"."<li><a class='deezer' href='{$child->Deezer}'><img src='../site/templates/img/$deezer' alt='deezer'/></a></li>"."</ul><br/>";            
4

2 回答 2

0

您缺少一些括号,foreach这些括号仅执行以下 IF 语句并继续在页面上打印出单个元素。要纠正这个问题,只需在 foreach 循环周围添加括号,如下所示:

    $rdio='rdio.png';
    $itunes='itunes.png';
    $xbox='xbox.png';
    $googleplay='googleplay.png';
    $spotify='spotify.png';
    $deezer='deezer.png';

    foreach($page->children as $child) {

    if(!$child->Rdio) {
$rdio='rdiog.png';
     }
    else
     {
    $rdio='rdio.png';
     }

       echo "<ul style='background-color:#CCC;'>"."<li><h1>{$child->title}</h1></li>".<li><a class='rdio' href='{$child->Rdio}'><img src='../site/templates/img/$rdio' alt='rdio'/></a></li>"."<li><a class='itunes' href='{$child->iTunes}'><img src='../site/templates/img/$itunes' alt='itunes'/></a></li>"."<li><a class='xbox' href='{$child->Xbox}'><img src='../site/templates/img/$xbox' alt='xbox'/></a></li>"."<li><a class='googleplay' href='{$child->GooglePlay}'><img src='../site/templates/img/$googleplay' alt='googleplay'/></li>"."<li><a class='spotify' href='{$child->Spotify}'><img src='../site/templates/img/$spotify' alt='spotify'/></a></li>"."<li><a class='deezer' href='{$child->Deezer}'><img src='../site/templates/img/$deezer' alt='deezer'/></a></li>"."</ul><br/>";      
于 2013-10-25T09:32:37.423 回答
0

尝试使用empty方法:

$rdio='rdio.png';
$itunes='itunes.png';
$xbox='xbox.png';
$googleplay='googleplay.png';
$spotify='spotify.png';
$deezer='deezer.png';

foreach($page->children as $child) {

if(empty($child->Rdio)) {
$rdio='rdiog.png';
 }
else
 {
$rdio='rdio.png';
 }

   echo "<ul style='background-color:#CCC;'>"."<li><h1>{$child->title}</h1></li>".<li><a class='rdio' href='{$child->Rdio}'><img src='../site/templates/img/$rdio' alt='rdio'/></a></li>"."<li><a class='itunes' href='{$child->iTunes}'><img src='../site/templates/img/$itunes' alt='itunes'/></a></li>"."<li><a class='xbox' href='{$child->Xbox}'><img src='../site/templates/img/$xbox' alt='xbox'/></a></li>"."<li><a class='googleplay' href='{$child->GooglePlay}'><img src='../site/templates/img/$googleplay' alt='googleplay'/></li>"."<li><a class='spotify' href='{$child->Spotify}'><img src='../site/templates/img/$spotify' alt='spotify'/></a></li>"."<li><a class='deezer' href='{$child->Deezer}'><img src='../site/templates/img/$deezer' alt='deezer'/></a></li>"."</ul><br/>";
于 2013-10-25T09:46:54.910 回答