2

Alright, so I've been foraging through the web as well as stackoverflow for sometime for an answer to my question(below), however nearly every explanation and/or topic I've found is either too convoluted for me or doesn't cater to my specific needs...

In short I have a blog that outputs numerous POSTs. Each POST contains multiple IMAGES. The POST contains data like TITLE, DESCRIPTION, POST DATE.. These things are contained in ONE Table called tbl_blog. The IMAGES belong to their own table called tbl_blog_imgs.

Right now, my code "works", however it only displays one blog post with images that match that blog post's ID. (In tbl_blog_imgs, I have a row called blog_key that matches the ID of each entry in tbl_blog)

Problem is naturally that I have more than one blog post and only one seems to post... IN

My Code...

<?php

require('myownsecretfile.php');

$sql = "SELECT blog_id, blog_title, blog_desc, date_format(blog_post_date,'%d/%m/%Y') as blog_post_date ";
$sql .= "FROM tbl_blog ";

$result = mysql_query($sql);

while($row = mysql_fetch_assoc($result)) {
    $blog_id = $row['blog_id']; 
    $blog_title = $row['blog_title']; 
    $blog_desc = $row['blog_desc'];
    $blog_post_date = $row['blog_post_date'];       

    ?>

    <div class="blog-title"><a href="blog_more.php?blog_title=<?php echo $blog_title; ?>"><?php echo $blog_title; ?></a>
    <p class="blog-post-date-style">-&nbsp;<?php echo $blog_post_date; ?>&nbsp;-</p>
    <br>
    <div class="blog-desc">
    <p class="blog-description-style"><?php echo $blog_desc; ?></p>
    </div>
    </div>
    <br>

    <?php

    $sql = "SELECT * ";
    $sql .= "FROM tbl_blog_imgs ";
    $sql .= "WHERE blog_img_key='$blog_id' ";

    $result = mysql_query($sql);

    while($row = mysql_fetch_assoc($result)) {
        $blog_img_key = $row['blog_img_key'];   
        $blog_img_path = $row['blog_img_path']; 
        $blog_img_alt = $row['blog_img_alt'];
        $blog_img_title = $row['blog_img_title'];       

        ?>

        <img src="images/blog/<?php echo $blog_img_path; ?>" alt="<?php echo $blog_img_alt; ?>" title="<?php echo $blog_img_title; ?>"><br>


        <?php
    }
}   
?>

Must I perform an INNER JOIN? If not, what would be the smartest and most efficient way of acheiving a loop inside of a loop...

6
  • Try group_concat, and explode the array according to your need Commented Sep 18, 2013 at 11:22
  • It's hard to tell from the way you formatted your post. Is the second loop nested inside the first loop? If so, I think it should work. Commented Sep 18, 2013 at 11:23
  • With nested loops should you be using the same variable for inner and outer loops for the results of the query? Commented Sep 18, 2013 at 11:25
  • yea, it's all one continuous chunk of code... I just inserted the text to clarify what was going on (not that the most on here need that...) In that case, I'll edit it to be more simple. Commented Sep 18, 2013 at 11:26
  • I knew it was one continuous piece of code, but it's hard to tell whether they're nested because you didn't use proper indentation. Commented Sep 18, 2013 at 11:27

1 Answer 1

1

You're using the same variable $result for both queries. So when it finishes displaying all the images for the first blog, and returns to the beginning of the outer loop, it does

$row = mysql_fetch_assoc($result);

But now $result contains the result of the image query, not the blog query. And since that query has returned all its rows, this returns false, so the outer loop terminates.

Change them to $result_blog and $result_img and I think your problem should be solved.

It's also confusing to use the same variable $row for both loops. In this case it doesn't cause a problem, because you immediately set variables to the elements of those arrays. But it's a bad habit, so I suggest you rename them as well.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. As I'm still learning, I didn't even consider how $result is a variable and not an element... ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.