Vecta
Vecta

Reputation: 2350

PHP Foreach String Concatenation

I'm attempting to modify some PHP to suit my needs and I'm having a bit of trouble.

I've created the variable $mutitag to hold the names of some variables from which I need to fetch some values.

I made a foreach loop to set $tv as each value of $multitag, fetch the values from the database, and return them into $get_tags. However, when I run the code below it only returns the values from the second value of $multitag array. How can I correctly concatenate $get_tags with all of the returned values? Thanks for your help!

$multitag = explode(",", $tv);
if ($tv == '' && !isset($value)) {
    return "No template variable for tags was declared.";
}

foreach ($multitag as $tv) {
    $get_tags = implode(
        $delimiter,
        $modx->getTemplateVarOutput($idname=array($tv), $page_id, $published="1")
    );
}

Upvotes: 1

Views: 6947

Answers (2)

Mr. Smith
Mr. Smith

Reputation: 5558

$get_tags = '';

foreach($multitag as $tv){
   $get_tags .= implode($delimiter,$modx->getTemplateVarOutput($idname=array($tv), $page_id, $published="1"));
}

Upvotes: 1

John Giotta
John Giotta

Reputation: 16954

Concat the variable get_tags with .=

$get_tags = '';
//...
$get_tags .= implode(...);

Upvotes: 3

Related Questions