Jump to content

disfunctional while loop


TechnoDiver
Go to solution Solved by maxxd,

Recommended Posts

I've got a lot of questions today, it seems. I can't see where the problem is in this method, it seemed very simple when I started

<?php

public function recent() {
            $rule = "ORDER BY RAND()";
            $field = "type";
            $type = "recent";
            $query = $this->_db->get(self::$_table, array($field, "=", $type), $rule);
            $this->_data = $query->all();

            $counter = 0;
            while($counter <= 7) {
                foreach($this->data() as $obj) {
                    if(strlen($obj->title) > 25) {
                        $obj->title = substr($obj->title, 0, 25) . "...";
                    } 
                    $data = array("id" => $obj->id, "title" => $obj->title, "content" => $obj->content, "author" => $obj->author, "add_by" => $obj->add_by, "category" => $obj->post_category, "image" => $obj->post_image, "num_likes" => $obj->num_likes, "num_comments" => $obj->num_comments, "num_views" => $obj->num_views, "post_cat_id" => $obj->post_cat_id, "date_added" => $obj->date_added );

                    echo $this->html($type, $data);

                    $counter ++;
                }
                // $counter ++;
            }
            
        }

It's pretty self explanatory. retrieve all items within the criteria and display the first 8. Problem is it displays all of the items that fit the criteria. It works fine other than that and I can't see the problem. What am I overlooking?

Link to comment
Share on other sites

Your foreach loop will run to completion (ie, print every row) before your while loop condition gets re-evaluated.  In pseudo-code, what you wrote is this:

while $counter < 8
    for each result $row
        print $row
        increment $counter
    end
end

Where as what you want to write, is

while $counter < 8
    fetch next result $row
    if $row exists
        print $row
    end
    increment $counter
end
                  

There are multiple ways to write that.  One way is to fetch a row at a time rather than everything.  Another is to fetch it all like you are now, but break; after the 8th row.

Edited by kicken
  • Thanks 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.