I have class called posts in a separate file :
<?php
class POSTS {
//Start of class properties:
private $db_connection;
public $post_id;
public $section_id;
public $user_id;
public $post_title;
public $post_details;
public $post_date;
public $post_category;
public $post_display;
public $num_of_rows;
public function getRelatedPosts($section_name, $category, $display) {
$stm = $this->db_connection->prepare("SELECT * FROM posts WHERE section_name!=:Section_name AND category=:Category AND display=:Display ORDER BY id DESC");
$stm->bindParam(":Section_name", $section_name);
$stm->bindParam(":Category", $category);
$stm->bindParam(":Display", $display);
$stm->execute();
$this->num_of_rows = $stm->rowCount();
if ($this->num_of_rows >= 1) {
$post_data = $stm->fetch(PDO::FETCH_OBJ);
$this->post_id = $post_data->id;
$this->section_id = $post_data->section_id;
$this->user_id = $post_data->user_id;
$this->post_title = $post_data->title;
$this->post_details = $post_data->details;
$this->post_date = $post_data->date;
$this->post_category = $post_data->category;
$this->post_display = $post_data->display;
}
}
}
?>
Then I want to loop through the results in my Index file:
$section_name = 'PHP';
$display = 'yes';
$POSTS->getRelatedPosts($section_name, $category $display);
$num_of_rows = $POSTS->num_of_rows;
if ($num_of_rows >= 1) {
for ($m=1; $m<=$num_of_rows; $m++) {
$post_id = $POSTS->post_id;
$section_id = $POSTS->section_id;
$user_id = $POSTS->user_id;
$post_title = $POSTS->post_title;
$post_details = $POSTS->post_details;
$post_date = $POSTS->post_date;
?>
<div id="related_post">
<h4><a href=""><?php echo $post_title;?></a></h4>
<p><?php echo $post_details;?></p>
</div>
<?php
}
} else {
echo 'Sorry no related posts now!';
}
Unfortunately The results are only one record repeated as many as the $num_of_rows variable equal. I tried some different ways with fetch methods like: fetchAll() and others styles but always the result is an error or only one record repeated.
Someone help me with my code please.
If you want to loop, try looping in your method:
public function getRelatedPosts($section_name, $category, $display)
{
$stm = $this->db_connection->prepare("SELECT * FROM posts WHERE section_name!=:Section_name AND category=:Category AND display=:Display ORDER BY id DESC");
$stm->bindParam(":Section_name", $section_name);
$stm->bindParam(":Category", $category);
$stm->bindParam(":Display", $display);
$stm->execute();
$this->num_of_rows = $stm->rowCount();
if ($this->num_of_rows >= 1) {
while($post_data = $stm->fetch(PDO::FETCH_OBJ)) {
$this->post_id[] = $post_data->id;
$this->section_id[] = $post_data->section_id;
$this->user_id[] = $post_data->user_id;
$this->post_title[] = $post_data->title;
$this->post_details[] = $post_data->details;
$this->post_date[] = $post_data->date;
$this->post_category[] = $post_data->category;
$this->post_display[] = $post_data->display;
}
}
}
Your loop would likely be something like:
for ($m=1; $m<=$num_of_rows; $m++) {
$post_id = $POSTS->post_id[$m];
$section_id = $POSTS->section_id[$m];
$user_id = $POSTS->user_id[$m];
$post_title = $POSTS->post_title[$m];
$post_details = $POSTS->post_details[$m];
$post_date = $POSTS->post_date[$m];
?>
<div id="related_post">
<h4><a href=""><?php echo $post_title;?></a></h4>
<p><?php echo $post_details;?></p>
</div>
<?php
}
In my class I should use the following instead of the current one:
$results = $stm->fetchAll(PDO::FETCH_OBJ);
I will not get any advantages of my current class properties. In my index file I will loop through the $results
using foreach
loop as the following:
foreach($results as $post){
$post_title = $post->title;
$post_details = $post->details;
}
and so on...
I'm experimenting with PDO and I had the same issue with PDO::FETCH_OBJ
I'm using PHP 5.6 in xampp 5.6.30
needless to say that - the DB name is animals - it has three columns only : animal_id, animal_type, animal_name
the following test code works fine and outputs all the records (that in my test DB are only eleven)
$stmt = $dbh->prepare("SELECT * FROM animals");
$stmt->execute();
if($stmt->rowCount() > 0) {
while($obj = $stmt->fetch(PDO::FETCH_OBJ)) {
echo $obj->animal_type . " " . $obj->animal_name . "<br>";
}
}
perhaps you may insert the counters inside the loop or, even better, make the query to properly limit the range.
Anyway, the code above, as expected outputs what follow
kookaburra bruce
emu bruce
goanna bruce
dingo bruce
kangaroo bruce
wallaby bruce
wombat bruce
koala bruce
kiwi cambiato
pippo zollo
pippz zoll2