Skip to main content
Inspiring
September 23, 2012
Answered

Master Page w/Images from each Post

  • September 23, 2012
  • 1 reply
  • 594 views

So my problem is hopfully simple. My master page which lists all the "Newest Posts" includes the following: Title, Description (1st sentence), Link to details page, and an Image. The problem is that I have multiple images linked with every "Post", so it posts it more than once. For example one I have 3 images of and it posts all 3 different images but the same title, description and link.

How can I overcome this to have it only grab the 1st image associated with the post? And how can I have the text and description "float" over the image. As everything is in one <p> and just loops.

In my functions.php

function getAllPosts($read) {

$sql = 'SELECT *

          FROM posts

          INNER JOIN post2photo USING (post_id)

          INNER JOIN photos USING (photo_id)

          ORDER BY created DESC';

return $read->fetchAll($sql);

}

In my Master.php

$feature = getAllPosts($dbRead);

<?php foreach ($feature as $post) { ?>

<p>

     <?php echo $post['name']; ?>:

     <?php echo['description'] ?>

     <span id="places"><img src=".../image_upload/<?php echo $post['filename']; ?>" width="400" /></span>

     <a href="detail.php?place=<?php eco $post['link_name']; ?>">More</a>

</p>

<?php } ?>

This topic has been closed for replies.
Correct answer TunedInMag

Think I have it figured out...

function getAllPosts($read) {

$sql = 'SELECT *

          FROM posts

          INNER JOIN post2photo USING (post_id)

          INNER JOIN photos USING (photo_id)

          GROUP BY post_id

          ORDER BY created DESC';

return $read->fetchAll($sql);

}

Doing this it only grabs one instance of each post's picture. I am testing to ensure its the correct solution.

1 reply

TunedInMagAuthorCorrect answer
Inspiring
September 24, 2012

Think I have it figured out...

function getAllPosts($read) {

$sql = 'SELECT *

          FROM posts

          INNER JOIN post2photo USING (post_id)

          INNER JOIN photos USING (photo_id)

          GROUP BY post_id

          ORDER BY created DESC';

return $read->fetchAll($sql);

}

Doing this it only grabs one instance of each post's picture. I am testing to ensure its the correct solution.