Search code examples
phpcycle

From a string to a cycle in PHP


I have ta string, pulled from the db, where each value is separeted by '-', something like this: a-b-c. In my db I also have a field telling me how many values I have, in this case 3. I would need to create a textfiled for each value of the string, so I was thinking of doing something like this:

$numberOfValues=db=>n;
$string=bb=>string;
$singleValue = explode("-", $string);
for($i=0;$i<$numberOfValues;$i++): {
echo ('input type="text" value="'.$singleValue[$i].'">');
}

My question is basically: is this an efficient way of doing what I'm looking for? Or perhaps there's a "better" way? Thank's!


Solution

  • $values = explode('-', $string);
    
    foreach ($values as $value) {
        printf('<input type="text" value="%s">', htmlspecialchars($value));
    }