2012-11-16 100 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

上述方法可行,您只需將變量指向想要檢查的數組值即可。 –