0

I am creating my own blog from scratch with a homepage that loads the latest posts in order of time published. I call the posts using a front controller and store the data on a MySQL database. The website itself is great and the posts all load perfectly with no issue. The issue is getting the homepage to work.

I created a few PHP functions for the homepage. They generally order the posts (database rows) by ID in descending order, since it's an autoincrement field, and call their data. And then to show the latest post as a sort of 'featured post' right at the top, by fetching the data from the very top row in the database, which is the latest post.

And that works fine - when I echo the result it shows the latest post just as I want it.

Below that I want two boxes, side by side, for the two posts before the first one. So I made this function to call them:

function fetch_rest_posts_1($conn) {

$stuff = $conn->query("SELECT * FROM table WHERE is_post = 1 ORDER BY id DESC LIMIT 1,2");

    while ($row = $stuff->fetch_array()) {
        $i=1;
        return '<div id="post_'.$i.'" style="width:308px;height:215px;padding:5px">
                <h2>'.$row['title'].'</h2>
                <p>'.date('d/m/Y',strtotime($row['published_date'])).' by '.$row['author'].' | <a href="http://www.domainname.com/'.$row['uri'].'#disqus_thread"></a></p>
                <p>'.$row['meta_description'].'</p>
                </div>';
                $i++;

    } // style="white-space:nowrap;width:100%;overflow:hidden;text-overflow:ellipsis"

}

And it actually does work great when I echo the result, shows everything I want, but it only shows one div, not two. When I take the SQL query and directly enter it into phpMyAdmin, it gives me two rows. Have I done something wrong?

(I put the auto-increasing $i in there so that I could isolate each box and alter the style later.)

3
  • 3
    return exits the function, it doesn't go to another loop. Commented Sep 18, 2014 at 18:54
  • you're declaring $i every time it loops, $i will never be able to increment if you declare it again.. Take out the $i =1; ... and what @TwiStar said... Commented Sep 18, 2014 at 18:59
  • There are several ways to deal with it. I would recommend separating output from DB logic, and not use echo or $str = ''; $str+= '...'. Just read something on templating, and separation of logic from presentation. This will do you a lot of improvement and spare you from a lot of hurt, trust me. Commented Sep 18, 2014 at 19:04

3 Answers 3

1

Your problem is caused by the return statement in the loop. You should add $return = '' at the top of your function, replace return by $result .=, and return $result at the end of your function.

In addition, the loop counter $i is reset in every iteration. Move the initial assignment out of the loop.

EDIT: The .= is intentional to append to $result instead of replacing it with another value constructed from the next dataset.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks andy, worked great. Btw should be $result = '' in your second sentence.
0

initiate $i outside the loop and use echo() instead of return() return() breaks the loop or use

$result .= '<div id="post_'.$i.'" style="width:308px;height:215px;padding:5px">
                <h2>'.$row['title'].'</h2>
                <p>'.date('d/m/Y',strtotime($row['published_date'])).' by '.$row['author'].' | <a href="http://www.domainname.com/'.$row['uri'].'#disqus_thread"></a></p>
                <p>'.$row['meta_description'].'</p>
                </div>';

and return $result; after the loop

Comments

0

That's because return will stop execution of the function try this approach:

function fetch_rest_posts_1($conn) {
    $stuff = $conn->query("SELECT * FROM table WHERE is_post = 1 ORDER BY id DESC LIMIT 1,2");
    $post = array();
    while ($row = $stuff->fetch_array()) {
        $post[] = $row;
    }
    return $post;
}

So the function purpose is to just get the data, so you can later print it:

$row = fetch_rest_posts_1($conn);
for($i = 0; count(row); $i++){
 echo '<div id="post_'.$i.'" style="width:308px;height:215px;padding:5px">
        <h2>'.$row[$i]['title'].'</h2>
        <p>'.date('d/m/Y',strtotime($row['published_date'])).' by '.$row[$i]['author'].' | <a href="http://www.domainname.com/'.$row[$i]['uri'].'#disqus_thread"></a></p>
        <p>'.$row[$i]['meta_description'].'</p>
        </div>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.