[SOLVED] PHP – Make PHP function output multiple instances

Issue

I have $tutorial->difficulty which sometimes has multiple outputs, and for each output that goes into the breakCommas function, I need to output a link for each one. Currently with how I have it, when $tutorial->difficulty outputs multiple items, it basically clumps them all into a single link. From what I’ve read I realize you can’t have multiple outputs per function. Any ideas how I can separate it to where it outputs multiple links?

PHP

function breakCommas($str) {
  return ('<a href="'.str_replace(' ','-',strtolower($str)).'-tutorial">'.$str.'</a>');
}

HTML

<div>
  <?=breakCommas($tutorial->difficulty)?>
</div> 

Solution

try this function:

function breakCommas($str) {
    $s = explode (",", $str);
    $a = array();
    foreach ($s as $st) {
        $a[] = "<a href=\"".str_replace(" ","-",strtolower($st))."-tutorial\">{$st}</a>";
    }
    return implode("", $a);
}

Answered By – diavolic

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published.