I have a cooperative(user) that can post many newsfeeds, so a one is to many relationship. My goal is to show all the newsfeeds of the cooperative in a list and use that list to link each newsfeeds to an update and delete page. However my code is only showing the first newsfeed and since I put it inside a loop, the first newsfeed is being loop six times. Why is that?
Im using the $loggerUser['coopID'] to get the coopID that is supplied to the where clause of the viewCoopNewsfeed function.
this is my sql command
function viewCoopNewsfeed($coopID)
{
try {
$connection = connect();
$statement = $connection->prepare("SELECT * FROM newsfeed WHERE
coopID = :coopID");
$statement->bindParam(":coopID", $coopID);
$statement->execute();
$statement->setFetchMode(PDO::FETCH_ASSOC);
if ($coopNewsfeed = $statement->fetch()) {
return $coopNewsfeed;
} else {
return NULL;
}
} catch (PDOException $exception) {
die("Error: " . $exception->getMessage());
}
}
and this is the loop in the view
<?php
if(!empty($coopNewsfeeds))
{
foreach($coopNewsfeeds as $coopNewsfeed)
{
?>
<form method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Photo</label>
<img class="profile-pic" src="<?=$coopNewsfeeds['photo']?>"/>
</div>
</div>
<br><br><br><br><br><br><br><br><br><br>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control" placeholder="Title" name="title" value="<?=$coopNewsfeeds['title']?>" required>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Body</label>
<textarea class="form-control" name="Body" value="<?=$coopNewsfeeds['Body']?>" placeholder="Body" id="" cols="30" rows="2"></textarea>
</div>
</div>
</form>
<?php
}
?>
<?php
}
?>