-4

これは、仕事を得るために必要な私のコードの例です...

「 echo '...'; 」を使用するとすべて問題ありませんが、「 return '...'; 」を使用するとレコードが 1 つしか取得されません。エコーを使用したくない問題は、ページの上部にすべての結果が表示されることです。この関数をページの別の場所で呼び出すため、return を使用する必要があります。

ありがとう!

    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>';

    }
}
4

1 に答える 1

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));
}
于 2016-05-04T16:23:09.740 に答える