0

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
                }

                ?>
Cid
  • 14,968
  • 4
  • 30
  • 45

1 Answers1

0

$coopNewsfeed = $statement->fetch() this fetches the next result (the first one in your case, because you executed fetch() only once). Since you are returning $coopNewsfeed immediatly after this statement, you are getting only 1 row.

Use fetchAll() instead.

if ($coopNewsfeed = $statement->fetchAll()) {
    return $coopNewsfeed; // this now returns all results in a 2d array
} else {
    return NULL;
}

And then, in your view :

foreach($coopNewsfeeds as $coopNewsfeed)
{
    // some html
    // notice the variable name -----v-----------v
    <img class="profile-pic" src="<?=$coopNewsfeed['photo']?>"/>
    // more html and stuff
}
Cid
  • 14,968
  • 4
  • 30
  • 45
  • Thank you @Cid I use "fetchAll()" the rows now match the database but the data are gone the picture is not shown and the title became "Undefined Index". – John Macasinag Oct 30 '19 at 09:56
  • That's because you are not using the variable `$coopNewsfeed` from the foreach loop, but the 2d array `$coopNewsfeeds` – Cid Oct 30 '19 at 10:03
  • Thank you and I am so sorry for my ignorance I did not notice it. Thank you for paying attention to my humble dilemma. And again sorry. – John Macasinag Oct 30 '19 at 10:12