Foreach not looping through query results - Duplicating 1 row

Apologies if i have not posted this correctly…first timer

So I have a web app that communicates with 4 tables(user table, truck table, trailer table, load table). A user can post an item and depending on the category he chooses the item will be posted in the respective table. The users dashboard should print out all the items a user has posted.

I wrote this class:

[php]

class Poster{

 public function displayUser($poster_id){

 	global $pdo;
			$query = $pdo->prepare('SELECT * FROM `user` WHERE id=?');
 	$query->bindValue(1,$poster_id);
	$query->execute();

 	return $query->fetch();


 }

public function displayMyPost($poster_id){

	global $pdo;
	$query = $pdo->prepare("SELECT idtruck as id, title as title, `date`as `date` FROM truck WHERE poster_id = ?
				UNION ALL
				select idtrailer as id, title as title, `date`as `date` FROM trailer WHERE poster_id =?
				UNION ALL
				select idload as id, title as title, `date`as `date` FROM `load` WHERE poster_id = ?");
	$query->bindValue(1,$poster_id);
	$query->bindValue(2,$poster_id);
	$query->bindValue(3,$poster_id);
	$query->execute();

	return $query->fetch();
}

}[/php]

On the users dashboard page i have this code to print out the results in to table:

<table class="table table-bordered table-hover table-striped">
                                        <thead>
                                            <tr>
                                                <th>Post ID</th>
                                                <th>Title</th>
                                                <th>Date posted</th>
                                            </tr>
                                        </thead>
                                        <?php

                                        $poster_id = $user_id;
                                        $myPost = new Poster;
                                        $post = $myPost->displayMyPost($poster_id);

                                        ?>
                                        <tbody>
                                            <?php foreach($post as $myPostTest){ ?>
                                            <tr>
                                                <td><?php echo $post['id']; ?></td>
                                                <td><?php echo $post['title']; ?></td>
                                                <td><?php echo $post['date']; ?></td>
                                            </tr>
                                            <?php } ?>
                                        </tbody>
                                    </table>

The problem i am facing is that the table is printing on the first row. It is duplicating the same row the number of times the person has posted an item.

Replace:

                           [php]             <td><?php echo $post['id']; ?></td>
                                             <td><?php echo $post['title']; ?></td>
                                             <td><?php echo $post['date']; ?></td>[/php]

With:
[php]

<?php echo $myPostTest['id']; ?>
<?php echo $myPostTest['title']; ?>
<?php echo $myPostTest['date']; ?>[/php]

Also you should use “Union” not “Union All” - Union all allows duplicates (not that you have any)…

I tried that and i get the following error:

Warning: Illegal string offset ‘id’ in C:\wamp\www\truckLoader\scripts\connect.php on line 41

on all the result items.

I have pasted the table below to illustrate the problem Im facing:

Post ID Title Date posted
16 Scania 2015-04-14 01:25:34
16 Scania 2015-04-14 01:25:34
16 Scania 2015-04-14 01:25:34
16 Scania 2015-04-14 01:25:34
16 Scania 2015-04-14 01:25:34
16 Scania 2015-04-14 01:25:34

Bun when i run the same query in Mysql it works.

I keep looking at it and I think what I suggested was perfect, I know you get an error. I must be missing something…

Maybe [member=46186]Kevin Rubio[/member] or [member=71845]JimL[/member] or [member=72272]astonecipher[/member] or [member=57087]Strider64[/member] will figure out what we are missing.

Thanks [member=69011]Topcoder[/member]

What does the following give you? (put it on line 14):

print_r($post);

Hi Kevin.

print_r($post) gives me this:

Array ( [id] => 16 [0] => 16 [title] => Scania [1] => Scania [date] => 2015-04-14 01:25:34 [2] => 2015-04-14 01:25:34 )

Funny thing is…the results are duplicated 6 times, that specific user has posted 6 times. Does this mean my method is incorrect?

Because when i run the query in MySQL it works…I’ve attached an image of what the results look like in MySQL


Untitled.png

I got it…

i changed my displayMyPost method. I had return $query->fetchAll(); i changed it to return $query->fetchAll();

then i had to make some changes to my foreach and it worked ;D

Thanks for all your help guys Im stoked.

Why did you use union instead of join?

No particular reason, is that a bad thing?

It appears you may be using more tables then necessary. It would help to see an SQL dump of your db.

Sponsor our Newsletter | Privacy Policy | Terms of Service