0

I have a website that is made in php and mysql.

It is a podcast site which I have created.

The home page has a list of podcasts and once one is clicked it then brings up the episode.php?id= followed by the ID that is listed in mysql for that podcast.

at the bottom of the episodes page I have added a comment box.

and I have it to display the comments saved in mysql using:

<?php class feedback {
public function fetch_all(){
    global $pdo;
      $query = $pdo->prepare("SELECT * FROM comments");
      $query->execute();
return $query->fetchAll();
              } }
$feedback = new feedback;
$articles = $feedback->fetch_all();
?>

<html>
<body>

    <?php foreach ($articles as $feedback) { ?>

<div class="comment" align="center">Name: <font size="3" color="grey"><?php echo $feedback['name']; ?></font> &nbsp;&nbsp; Email: <font size="3" color="grey">Hidden</font>
<br />
<font size="5" color="red"><div align="left"><?php echo $feedback['post']; ?></font></div></div>
<br><div class="divider2"> </div><br>
<?php } ?>
</html>
</body>

This displays all the comments that are listed in the comments field in mysql. each comment has a "cast" tab which displays the id of the podcast.

How can I get this to reflect the page being viewed?

for example.

if I'm viewing episode.php?id=1 then I want the comments with the "cast" tab of "1" to be displayed and not the "cast" tab of "2". Also the same goes for episode.php?id=2. and so on!

Please can someone guide me on how to do this?

thank you.

Kev

Kev Hopwood
  • 149
  • 1
  • 3
  • 15

2 Answers2

0

so I had a little play around with this after trying Timo Dörsching's suggestion which did not work.

I hit undo to get it back to my original post and changed this:

<?php foreach ($articles as $feedback) { ?>

<div class="comment" align="center">Name: <font size="3" color="grey"><?php echo $feedback['name']; ?></font> &nbsp;&nbsp; Email: <font size="3" color="grey">Hidden</font>
<br />
<font size="5" color="red"><div align="left"><?php echo $feedback['post']; ?></font></div></div>
<br><div class="divider2"> </div><br>
<?php } ?>

to this:

    <?php foreach ($articles as $feedback) {
if ($feedback['cast'] === $_GET['id']) { ?>

<div class="comment" align="center">Name: <font size="3" color="grey"><?php echo $feedback['name']; ?></font> &nbsp;&nbsp; Email: <font size="3" color="grey">Hidden</font>
<br />
<font size="5" color="red"><div align="left"><?php echo $feedback['post']; ?></font></div></div>
<br><div class="divider2"> </div><br>
<?php } } ?>

this does the job perfectly.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Kev Hopwood
  • 149
  • 1
  • 3
  • 15
-1

perhaps you have to filter the $_GET['id'] & $cast.... but this easy to google how to do this... Have a look at: How can I prevent SQL injection in PHP?

$cast = $_GET['id'];
public function fetch_all($cast){
    global $pdo;
      $query = $pdo->prepare("SELECT * FROM comments WHERE cast/id/... = ".$cast."");
      $query->execute();
return $query->fetchAll();
              } }
$feedback = new feedback;
$articles = $feedback->fetch_all();
Community
  • 1
  • 1
timod
  • 585
  • 3
  • 13