简体   繁体   中英

Inserting query results into a php string during foreach loop

I have a php foreach loop listing news stories pulled from MySQL, as part of the loop the story is printed out then underneath a query runs to pull, then display any images attached to the story. This is the a simplifid version of it:

foreach ($res AS $row) { 
    printf('<p>%s</p>' . PHP_EOL, nl2br($row['story']));


    $qry3 = "select * from tpf_images where news_id=".$row['news_id'];
    $res3 = $pdo->query($qry3);


    foreach ($res3 AS $row3) { 

        printf('<a href="/images/%s%s.jpg" rel="lightbox[%s]" title="%s. Photo Credit - %s" >
        <img src="/images/%s%s-thumb.jpg" style="max-height: 250px; max-width: 250px" alt="%s"/></a>' . PHP_EOL, $row3['url'], $row3['alt'], $row['headline'], $str ,$row3['credit'], $row3['url'], $row3['alt'], $row3['alt'] );
    }
}

It works fine but means all images for a news story are under that story. What I'm trying to achieve instead is that the images get printed out as part of the story text, at the start of each paragraph. The story is printed out with nl2br($row['story']) , using nl2br a paragraph is achieved with <br /><br /> . So is there a way for php to scan nl2br($row['story']) and if it finds <br /><br /> inserts the text I am using for the image foreach:

printf('<a href="/images/%s%s.jpg" rel="lightbox[%s]" title="%s. Photo Credit - %s" >
<img src="/images/%s%s-thumb.jpg" style="max-height: 250px; max-width: 250px" alt="%s"/></a>' . PHP_EOL, $row3['url'], $row3['alt'], $row['headline'], $str ,$row3['credit'], $row3['url'], $row3['alt'], $row3['alt'] );

That way the images will display in the body of the text rather thn under it. If there are more images in the result than <br /><br /> the rest of the images print out after the story like before.

Thanks for any help with this as I am completely stumped (also, was unsure what the title should be so that can be edited to better suit if needed)

EDIT: to try and make question clearer, nl2br($row['story']) get searched for <br /><br /> . If found it is replaced with <br /><image><br /> with image being the code I use to display images which is:

<a href="/images/%s%s.jpg" rel="lightbox[%s]" title="%s. Photo Credit - %s" >
        <img src="/images/%s%s-thumb.jpg" style="max-height: 250px; max-width: 250px" alt="%s"/></a>' . PHP_EOL, $row3['url'], $row3['alt'], $row['headline'], $str ,$row3['credit'], $row3['url'], $row3['alt'], $row3['alt']

Why not add the
directly into the printf line?

printf('<a href="/images/%s%s.jpg" rel="lightbox[%s]" title="%s. Photo Credit - %s" >
<img src="/images/%s%s-thumb.jpg" style="max-height: 250px; max-width: 250px" alt="%s"/></a><br>' . PHP_EOL, $row3['url'], $row3['alt'], $row['headline'], $str ,$row3['credit'], $row3['url'], $row3['alt'], $row3['alt'] );

Or even better, use css to style it, for example :

echo '<div class="images">';
foreach ($res3 AS $row3) { 

    printf('<a href="/images/%s%s.jpg" rel="lightbox[%s]" title="%s. Photo Credit - %s" >
    <img src="/images/%s%s-thumb.jpg" style="max-height: 250px; max-width: 250px" alt="%s"/></a>' . PHP_EOL, $row3['url'], $row3['alt'], $row['headline'], $str ,$row3['credit'], $row3['url'], $row3['alt'], $row3['alt'] );

}
echo '</div>';

CSS :

div.images a { display: block; }

Unless I completely misunderstood the question.

Edit::

Saw your new edit, you need to use sprintf instead :

foreach ($res AS $row) { 
    $story = '<p>' . preg_replace("/\n|\n\r|\n\r/", '<br>', $row['story']) . '</p>' . PHP_EOL;


    $qry3 = "select * from tpf_images where news_id=".$row['news_id'];
    $res3 = $pdo->query($qry3);

    $images = '';
    foreach ($res3 AS $row3) { 
        $images .= sprintf('<a href="/images/%s%s.jpg" rel="lightbox[%s]" title="%s. Photo Credit - %s" >
        <img src="/images/%s%s-thumb.jpg" style="max-height: 250px; max-width: 250px" alt="%s"/></a>' . PHP_EOL, $row3['url'], $row3['alt'], $row['headline'], $str ,$row3['credit'], $row3['url'], $row3['alt'], $row3['alt'] );
    }
    $story = preg_replace('/<br><br>/', '<br>' . $images . '</br>', $story);
}

user2574794. What you are requesting is not exactly "clear".

BTW, why don't you use a simple str_replace()? Something like:

str_replace('<br /><br />', '<br /><br />blabla', nl2br($row['story']));

use the str_replace() function in order to remove the <br/> 's

to remove the break rows:

str_replace(array('<br>','<br/>','',strtolower($row['story'])))

place the story inside a <span> and text-align the <span> s and <img> s left.

I would recommend you use <li> s since it is very easy to style it(vertical and horizontal text aligns)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM