2016-05-04 29 views
-4

これは私のコードの例です。私は仕事をする必要があります...foreachでリターンを使用すると、medooでエコーされない

"echo '...'を使用すると、 return '...'; "私はレコードを1つだけ取得します。エコーを使用したくないという問題は、私のページの上にすべての結果を得ることです。私は私のページのどこかにこの関数を呼び出すので、私はリターンを使用する必要があります。

ありがとうございます!

public function showForum() 
{ 

    $cats = $this->db->query("SELECT * FROM forum_cats ORDER BY cat_order ASC")->fetchAll(); 

    foreach ($cats as $cat) { 
     return '<table class="table forum table-striped"> 
      <thead> 
      <tr> 
       <th class="cell-stat" 
        style="background-image: url(\'\'); background-size: 50px; background-repeat: no-repeat; background-position: center;"></th> 
       <th> 
        <h3>' . $cat['cat_name'] . '</h3> 
       </th> 
       <th class="cell-stat text-center hidden-xs hidden-sm">Topics</th> 
       <th class="cell-stat text-center hidden-xs hidden-sm">Posts</th> 
       <th class="cell-stat-2x hidden-xs hidden-sm">Last Post</th> 
      </tr> 
      </thead> 
      <tbody> 
      <tr> 
       <td class="text-center"><i class="fa fa-question fa-2x text-primary"></i></td> 
       <td> 
        <h4><a href="#">Frequently Asked Questions</a><br> 
         <small>Some description</small> 
        </h4> 
       </td> 
       <td class="text-center hidden-xs hidden-sm"><a href="#">9 542</a></td> 
       <td class="text-center hidden-xs hidden-sm"><a href="#">89 897</a></td> 
       <td class="hidden-xs hidden-sm">by <a href="#">John Doe</a><br> 
        <small><i class="fa fa-clock-o"></i> 3 months ago</small> 
       </td> 
      </tr> 
      </tbody> 
     </table>'; 

    } 
} 
+0

はい、「return」は関数の実行を終了するためです。ループの代わりに、文字列を配列項目に設定し、ループの後に配列を返します。後でエコーアウトすることができます。 –

+0

それぞれを配列に格納するようにコメントを編集しました。別のオプションは、文字列を連続して連結し、最後の文字列を返すことです。 –

+0

提案ありがとうございました@JonStirling! :) – 7h3ev1l

答えて

0

ストアあなたは、文字列で返し、以降の文字列を返すようにしたいすべてのデータ:

public function something() { 
    $result = ""; // Create empty string 

    foreach($array as $val) { 
     $result .= "something"; // Add something to the string in the loop 
    } 

    return $result; // Return the full string 
} 

代替が(すでに配列を持っているので)あなたの文字列に配列をマッピングすることになります値を返して、配列を以下のように入れ子にします:

public function something() { 
    return implode('', array_map(function($val) { 
     return "something"; 
    }, $array)); 
} 
+0

恐ろしい!ありがとう!!! <3その作業! – 7h3ev1l

+0

どうすればいいですか:http://paste.md-5.net/roxirenudu.coffee、私は間違ったフォーラムを今すぐ取得します... – 7h3ev1l

+0

'$ display_forums =" ";' * 'inside *の2番目のforeachを設定する必要があります実際には2つの変数はまったく必要ありません!あなたはちょうどあなたの最初のものに連結し続けることができます。 – CherryDT

関連する問題