简体   繁体   中英

How to use foreach() to display results from array

I have the following query:

$sth = $this->db->prepare("SELECT * FROM comment WHERE status = 1");
$sth->setFetchMode(PDO::FETCH_ASSOC);
$sth->execute();

$reviews = array();

while($row = $sth->fetch()){    
    $reviews[]['name'] = $row['name'];
    $reviews[]['comment'] = $row['comment'];
    $reviews[]['star'] = $row['star'];
}

return $reviews;
}

Which returns the following for var_dump($this->reviews) :

array(9) { 
[0]=> array(1) { ["name"]=> string(5) "name1" } 
[1]=> array(1) { ["comment"]=> string(8) "comment1" } 
[2]=> array(1) { ["star"]=> string(1) "4" } 
[3]=> array(1) { ["name"]=> string(5) "name2" } 
[4]=> array(1) { ["comment"]=> string(8) "comment2" } 
[5]=> array(1) { ["star"]=> string(1) "4" } 
[6]=> array(1) { ["name"]=> string(5) "name3" } 
[7]=> array(1) { ["comment"]=> string(8) "comment3" } 
[8]=> array(1) { ["star"]=> string(1) "4" } 
}

How can I use foreach to display name , comment , star by row?

Ie:

name1, email1@email.com, comment1
name2, email2@email.com, comment2
name3, email3@email.com, comment3

I know it should be something like:

foreach( $this->reviews as $key){
  echo "Name:". $this->reviews['name'] . "Email:" . $this->reviews['email'] . "Comment:" .  $this->reviews['comment'];
}

But this gives me Notice: Undefined index: name . How do I define the index?

You need to generate an array of arrays. Something like

while($row = $sth->fetch()){    
    $reviews[] = array('name' => $row['name'], 'comment' => $row['comment'], 'star'=> $row['star']);
}

Now your $reviews object will have just as many elements as there were rows in the query result, and you can print it any way you want.

As for printing (which I think is where your error message is coming from):

foreach($reviews as $thisValue) {
  echo "Name: ". $value['name'];
}

should work.

You are setting up the insert into the array incorrectly:

while($row = $sth->fetch()){    
    $reviews[]['name'] = $row['name'];
    $reviews[]['comment'] = $row['comment'];
    $reviews[]['star'] = $row['star'];
}

The shorthand [] inserts a new element each and every single time, you want to actually insert an array AS the element.

while($row = $sth->fetch()){    
    $revArr=array('name' => $row['name'], 'comment' => $row['comment'], 'star' => $row['star']);
    $reviews[]=$revArr;
}

Edit: Now change your second foreach to this:

foreach( $this->reviews as $key){
  echo "Name:". $key['name'] . "Email:" . $key['email'] . "Comment:" .  $key['comment'];
}

You should use your foreach loop like this:

foreach( $this->reviews as $value){
  echo "Name:". $value['name'] . "Email:" . $value['email'] . "Comment:" .  $value['comment'];
}

Notice how i use $value instead of $this->reviews inside the loop.

foreach( $this->reviews as $key){
  echo "Name:". $this->reviews['name'] . "Email:" . $this->reviews['email'] . "Comment:" .  $this->reviews['comment'];
}

this is not the right way, because now you are working on $key so write -

   foreach( $this->reviews as $key){
      echo "Name:". $key['name'] . "Email:" . $key['email'] . "Comment:" .  $key['comment'];
    }

you can do it as follow:

$reviews = array();
while($row = $sth->fetch()){    
   $reviews[] = array($row['name'], $row['comment'], $row['email'], $row['star']);
}

and then you can do a heredoc:

$str = '';
foreach( $this->reviews as $review ){
   $str .=<<<html
       name: {$review['name']}
       email: {$review['email']}
       comment: {$review['comment']}
       start: {$review['star']}
   html;
}

finally i echo everything:

echo $str;

I think this is much cleaner, and you can add any html.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM