views:

14

answers:

1

We have some bbcode and I'm trying to setup the ability for a certain code to call a function that loads some images from our photo gallery. So if someone puts in an album number, it pulls 5 of the most recent photos out. When we call this function regularly (on another page, just as a regular php statement), it works as expected. But when we call this function through the preg_replace system, it loads the function at the top of the page.

See here: http://www.greendayauthority.com/news/1961/ The pumpkin images at the top of the site should be within the news post, below "Thanks to Marcus M. for sending the news." - which is where I placed the bbcode [picvault]1[/picvault]

Here's how we change out the values for the bbcode

$newsPost  = preg_replace(array_keys($bbcode), array_values($bbcode), $newsPost);

Here's the portion of the $bbcode that calls our function

"/\[picvault\](.*?)\[\/picvault\]/e" => "relatedImages($1)",

And here's the relatedImage function

function relatedImages($albumID) {

    $queryImage = mysql_query("SELECT * FROM cpg140_pictures WHERE aid = $albumID ORDER BY pid DESC LIMIT 5");

    echo "<div class='relatedImages'>";
    while($images = mysql_fetch_array($queryImage)) {

            $relImgPath = $images['filepath'];
            $relImgName = $images['filename'];
            $relThumbImgUrl = "http://www.greendayauthority.com/Picture_Vault/albums/$relImgPath/thumb_$relImgName";
            $relFullImgUrl = "http://www.greendayauthority.com/Picture_Vault/albums/$relImgPath/$relImgName";

        echo "<div class='relImage'>
                        <a href='$relFullImgUrl' rel='lightbox-$albumID'><img src='$relThumbImgUrl'></a>
                    </div>";
    }

    echo " <div class='relImage'>
                        <a href='http://www.greendayauthority.com/Picture_Vault/thumbnails.php?album=$albumID' target='_top'><img src='http://www.greendayauthority.com/images/viewmorephotos.png'&gt;&lt;/a&gt;
                 </div>";
    echo "</div>";
}
+2  A: 

If you use that function as callback it will output its html snippets right away. This happens because it uses echo. And when your preg_replace thus invokes the echo before the real page output starts, then the image html will preceed the rest of the page.

Solution: make the relatedImages callback function use return instead of echo.

mario