TechnoDiver Posted September 14, 2021 Share Posted September 14, 2021 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? Quote Link to comment https://forums.phpfreaks.com/topic/313735-disfunctional-while-loop/ Share on other sites More sharing options...
kicken Posted September 14, 2021 Share Posted September 14, 2021 (edited) 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 September 14, 2021 by kicken 1 Quote Link to comment https://forums.phpfreaks.com/topic/313735-disfunctional-while-loop/#findComment-1589897 Share on other sites More sharing options...
Solution maxxd Posted September 14, 2021 Solution Share Posted September 14, 2021 A more efficient way is to only select the 8 rows you're looking for instead of selecting the entire table. 2 Quote Link to comment https://forums.phpfreaks.com/topic/313735-disfunctional-while-loop/#findComment-1589905 Share on other sites More sharing options...
TechnoDiver Posted September 14, 2021 Author Share Posted September 14, 2021 3 hours ago, maxxd said: A more efficient way is to only select the 8 rows you're looking for instead of selecting the entire table. Do you mean only pulling 8 items from the DB? If that's what you mean could you provide a sample snippet please Quote Link to comment https://forums.phpfreaks.com/topic/313735-disfunctional-while-loop/#findComment-1589907 Share on other sites More sharing options...
ginerjm Posted September 14, 2021 Share Posted September 14, 2021 Add "limit 8" to your query. 1 Quote Link to comment https://forums.phpfreaks.com/topic/313735-disfunctional-while-loop/#findComment-1589908 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.