2012-11-16 98 views
-1

我有一个关于If语句的问题,它真的让我发疯。 看来我没有找到合适的解决方案。请让我解释一下情况。 还有4种可能的情况一个PHP脚本中的IF语句

  1. 无需出菲林
  2. 没有图片
  3. 无需出菲林,但与图片
  4. 没有画面,但与电影

多一点点的信息,但也许不是必要。 字段电影和图片是数据库中的字段。用PHP脚本,我正在检查一个字段是否填充了某些东西。有了这些信息,我将建立我的页面。在某些情况下,不会有照片或电影的地方。我希望你明白我在说什么。

这是我的代码

<?php 
    class Artikel{ 
public function printWholeArticle() 
     { 
      include ('connection.php'); 
      $sSql = "SELECT UNIX_TIMESTAMP(datum) as unixDatum, titel, artikel, id, image, video FROM tblArtikels WHERE id = '" . $this->m_sKey."'"; 
      $res = $mysqli -> query($sSql); 
      return ($res); 
     } 
} 


$key = $_GET['id']; // I get the key from the url 
$oNieuwsArtikel = new Artikel(); 
$oNieuwsArtikel -> Key = $key; 
$vAllNieuwsArtikel = $oNieuwsArtikel -> printWholeArticle(); 

$artikel = $vAllNieuwsArtikel -> fetch_assoc(); 


// just a part of my if statement, to let you guys know how I print the data to the screen 
if ($artikel['image'] == "") 
      { 
       echo "<div class='datum'>" . "<div class='day'>" . date('d', $artikel['unixDatum']) . "</div>" . "<div class='month'>" . "/" . date('m',       $artikel['unixDatum']) . "</div>" . "<div class='year'>" . date('Y', $artikel['unixDatum']) . "</div>" . "</div>"; 
      echo "<p>" . "<div class='content_wrap_page'>" . "<div class='artikel_page'>" 
      . "<h1>" . $artikel['titel'] ."</h1>" . $artikel['artikel'] . "</div>" . "</div>" . "</p>"; 
      } 

感谢

+0

你能分享一下你的代码吗? – PeeHaa

+0

@PeeHaa请参阅上面的文章。 – Niels

回答

1
if (empty($artikel['video'])) 
{ 
    // no film code 

    if (!empty($artikel['image'])) 
    { 
     // no film but with picture code 
    } 
} 
else if (empty($artikel['image'])) 
{ 
    // no picture code 

    if (!empty($artikel['video'])) 
    { 
     // no picture but with film code 
    } 
} 

其中$薄膜和$图片设置为从数据库检索值。

+0

我无法创建变量,因为这些值处于while循环中。 (请参阅我的代码) – Niels

+0

上述方法可行,您只需将变量指向想要检查的数组值即可。 –