2014-01-14 72 views
0

我需要一些幫助。 由於我構建的結構,我無法使用回聲。我沒有時間來解釋爲什麼。但我的問題是,有沒有辦法返回多個數據?可以這麼說,例如:如何返回多個值?

final public function getCamps() 
     { 
      $campaigns = mysql_query("SELECT * From cms_news WHERE campaign='1'"); 

      while($skriv = mysql_fetch_assoc($campaigns)) 
      { 
      return 
      ' 
      <hr/> 
      <div class="campaign_images"> 
      </div> 
      <div class="campaign_content"> 
      <b>'.$skriv['title'].'</b><br> 
      '.$skriv['shortstory'].' 
      </div> 
      <p class="gothere">Go there &raquo;</p> 
      <div style="clear:both;"></div> 
      <hr/> 

      '; 
      } 

     } 

但問題是返回將只給我1值,然後停止腳本。我該如何解決這個問題?我聽說我應該返回一個數組或其他東西,但我該怎麼做?謝謝。對不起,我的英語btw。

回答

0

就試試這個:

final public function getCamps() 
    { 
     $campaigns = mysql_query("SELECT * From cms_news WHERE campaign='1'"); 
     $data_return = ""; 
     while($skriv = mysql_fetch_assoc($campaigns)) 
     { 
     $data_return .= //this returns multiple values from your db 
     ' 
     <hr/> 
     <div class="campaign_images"> 
     </div> 
     <div class="campaign_content"> 
     <b>'.$skriv['title'].'</b><br> 
     '.$skriv['shortstory'].' 
     </div> 
     <p class="gothere">Go there &raquo;</p> 
     <div style="clear:both;"></div> 
     <hr/> 

     '; 
     } 
    return($data_return); 

    } 

希望這有助於。

+0

非常感謝!但是返回=「」;部分給了我一個錯誤。 – Eymen

+0

對不起,請參閱上面的更新代碼:) – Aljie

1

將數據添加到數組並返回。

$data = array(); 

while($skriv = mysql_fetch_assoc($campaigns)) 
{ 
    $data[] = 
    ' 
    <hr/> 
    <div class="campaign_images"> 
    </div> 
    <div class="campaign_content"> 
    <b>'.$skriv['title'].'</b><br> 
    '.$skriv['shortstory'].' 
    </div> 
    <p class="gothere">Go there &raquo;</p> 
    <div style="clear:both;"></div> 
    <hr/> 

    '; 
} 

return $data; 

如果您只需要將輸出內容則implodeecho

$data = $obj->getCamps(); 

echo implode($data); 

雖然我個人不會在環路中添加HTML。我只是行數據添加到陣列,並返回:

final public function getCamps() 
{ 

    $data = array(); 

    $campaigns = mysql_query("SELECT * From cms_news WHERE campaign='1'"); 

    while($skriv = mysql_fetch_assoc($campaigns)) 
    { 
    $data[] = $skriv; 
    } 

    return $data; 

} 

然後遍歷返回數組過,並添加HTML

<?php foreach ($obj->getCamps() as $camp) { ?> 

    <hr> 

    <div class="campaign_images"></div> 

    <div class="campaign_content"> 

    <b><?php echo $camp['title']; ?></b> 

    <br> 

    <?php echo $camp['shortstory']; ?> 

    </div> 

    <p class="gothere">Go there &raquo;</p> 

    <div style="clear:both;"></div> 

    <hr> 

<?php } ?> 
+0

我不能使用回聲。我想在不使用回顯的情況下打印數據 – Eymen

+0

您必須使用echo(或「print」)輸出數據。 – MichaelRushton

0

您可以輕鬆地與多個鍵返回數組/值對。你只需要知道如何在接收端使用它。

return [ 
    'html' => '<hr/><div class="campaing_images>....</div>', 
    'error' => false, 
] 

你也可以做一個正常的數組索引版本

return [ 
    '<hr/><div class="campaing_images>....</div>', 
    false 
] 
1

只要一個return語句執行,控制葉函數的範圍。您只能使用return一次。它看起來像要做的是建立一個字符串並返回整個事情。因此,而不是這樣的:

while(condition) { 
    return value; 
} 

這樣做:

result = ''; 
while(condition) { 
    result .= value; 
} 
return result; 

這是一個函數內彙總結果的標準模式。