0

I'm trying to change specific words in a text.

Just "gallery" bbcode's will echo the gallery codes.

Everthing is ok, but foreach loop returns just one row.

here is my function

function bbcode_gallery($str){
global $vt,$siteurl;

$thumbheight = "150";
$thumbwidth = "150";

$patterns = "/\[gallery\](.+?)\[\/gallery\]/i";        
$replacements = "$1";          

$bb_str = preg_replace($patterns, $replacements, $str);  
$gal_id = strip_tags($bb_str);
$gal_id = settype($bb_str, "integer");

$images = $vt->tablo("SELECT * FROM gallery_uploads WHERE gal_id = '$gal_id'");

foreach ($images as $image) {

    $replace = '<img src="'.$siteurl.'/'.$image->url.'" width="'.$thumbwidth.'" height="'.$thumbheight.'">';

}
    //var_dump($images); I specified here is below.
$str = preg_replace($patterns, @$replace, $str); 
echo  $str;
}

var_dump(images) output;

Array(
[0] => stdClass Object
    (
        [guid] => 1
        [gal_id] => 1
        [url] => uploads/images/gallery/2bc8e542.jpg
        [type] => gallery
    )

[1] => stdClass Object
    (
        [guid] => 3
        [gal_id] => 1
        [url] => uploads/images/gallery/41ee461a.jpg
        [type] => gallery
    )

[2] => stdClass Object
    (
        [guid] => 4
        [gal_id] => 1
        [url] => uploads/images/gallery/b3768424.jpg
        [type] => gallery
    )

[3] => stdClass Object
    (
        [guid] => 5
        [gal_id] => 1
        [url] => uploads/images/gallery/edb9a830.jpg
        [type] => gallery
    ))

output;

<p>here is text</p>
<img src="http://site.com/uploads/images/gallery/edb9a830.jpg" width="150" height="150">
<p>here is text</p>

why foreach loop shows one row?

1 Answer 1

1

you must append instead of replace of var $replace - so it should be

foreach ($images as $image) {
    $replace .= ''; // dot is require to append to string
}
Sign up to request clarification or add additional context in comments.

Comments

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.