So…
class postQuery {
public function __construct($args = NULL){
$post_type = $args['post_type'];
$limit = $args['limit'];
$category = $args['cat'];
global $sql;
$sql = $sql->query('SELECT * FROM posts');
$sql = $sql->fetchAll(PDO::FETCH_ASSOC);
}
public function havePosts() {
global $sql;
global $rowNum;
$rowNum = 0;
$rowMax = count($sql);
while($rowNum <= $rowMax) {
return $rowNum;
$rowNum++;
}
}
}
the havePosts() function should run while $rowNum < $rowMax… everything ok this far…
but now, i want to create a while statement with this function, like this:
$con = new postQuery();
while($con->havePosts()){
global $sql;
global $rowNum;
return $sql[$rowNum]['title'];
}
How can i return the data given by my WHILE inside the Function one by one?
Your
postQueryclass may look like this:And then you can loop through posts:
Note that my example is not best practice at all. You should avoid using global variables and lot of data you can store to class member variables (e.g.
count($sql)). You should also fix my arithmetic to avoid using magic operations like$this->currentRow + 1.