-4

This is my example of my code i need to get work...

If i use " echo '...'; " everything is ok but if i use " return '...'; " i get only one record. The problem i dont want to use echo is i get all results on the top of my page. i need to use return because i call this function somewhere else on my page.

Thanks!

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

    }
}
7h3ev1l
  • 1
  • 1
  • Yes, because `return` ends the execution of the function. Instead in your loop, set the string to an array item and after the loop, return the array. You can then later echo out. – Jonnix May 04 '16 at 16:21
  • I edited my comment to suggest storing each in an array, another option is to continuously concat the strings and just return the final string. – Jonnix May 04 '16 at 16:23
  • Thanks for suggestions @JonStirling! :) – 7h3ev1l May 04 '16 at 16:26

1 Answers1

0

Store all the data you want to return in a string, and later return the string:

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
}

An alternative (since you already have an array) would be mapping the array to your string values and returing the imploded the array like so:

public function something() {
    return implode('', array_map(function($val) {
        return "something";
    }, $array));
}
CherryDT
  • 25,571
  • 5
  • 49
  • 74
  • what about this: http://paste.md-5.net/roxirenudu.coffee , i get the wrong forums now... – 7h3ev1l May 04 '16 at 16:51
  • You would need to set `$display_forums = "";` *inside* the second foreach, but actually you don't need two variables at all! You can just keep concatenating to your first one. – CherryDT May 04 '16 at 17:06