0

I have input like this:

$recipe['ingredients'] = '100ml milk, 350ml double cream, 150ml water';

I'm trying to split it up so it looks as follows:

<ul>
    <li>100ml milk</li>
    <li>350ml double cream</li>
    <li>150ml water</li>
</ul>

So far I have the following code:

$ingredientsParts = explode(',', $row_rs_recipes['ingredients']);
$ingredients = array($ingredientsParts);
while (! $ingredients) { 
   echo" <li>$ingredients</li>";
}

but nothing gets printed.

2
  • PHP's explode() returns an array: php.net/manual/en/function.explode.php Commented Feb 8, 2012 at 9:07
  • $ingredients = array($ingredientsParts); You don't need this. ingredientsParts is already an array after explosion. I would, personally, use for or foreach loop to traverse whole array but that's up to you. I'm not sure if it could be done by this while condition. Are you trying to reference to array internal pointer by !$ingredients? Commented Feb 8, 2012 at 9:07

6 Answers 6

3
$ingredientsParts = explode(',', $row_rs_recipes['ingredients']);
    $li = '<ul>';
    foreach($ingredientsParts as $key=>$value){
         $li.=" <li>$value</li>";
    }
    $li.= '</ul>';

echo $li;
Sign up to request clarification or add additional context in comments.

1 Comment

This answer is missing an educational explanation. $key is declared but never accessed. It seems strange to declare a variable as $li when it contains <ul> element data.
2

this should be enough:

$ingredientsParts = explode(', ', $row_rs_recipes['ingredients']);

foreach ($ingredientsParts as $ingredient)
{ 
    echo "<li>$ingredient</li>";
}

or you can explode it by ',' and use echo '<li>' . trim($ingredient) . '</li>'; to remove whitespace from beginning/end of that string

Comments

1
  1. When you explode() a string it is automatically converted into an array. You do not need to convert it to an array type as you did on the second line.
  2. You want to use a foreach() loop to iterate through an array, not a while loop.

       $ingredientsAry = explode(',', $row_rs_recipes['ingredients']);
       foreach($ingredientsAry as $ingredient){
           echo "<li>$ingredient</li>";
       }
    

In fact you can just do a foreach() loop on the explode() value

foreach(explode(',', $row_rs_recipes['ingredients']) as $ingredient){
    echo "<li>$ingredient</li>";
}

2 Comments

you forgot the space after a comma
@bzx It's saver to split only on the , as this is the separating char. Imaging the string foo, bar,foobar, if the space was included this string would not be split properly.
0

The explode method already return an array, so you don't have to transform your variable $ingredientsParts into an array.

Just do:

$ingredientsParts = explode(', ', $row_rs_recipes['ingredients']);
foreach ($ingredientsParts as $ingredient)
{ 
echo "<li>$ingredient</li>";
}

Comments

0
if (!empty($recipe['ingredients'])) {
    echo '<ul><li>' . implode('</li><li>', explode(', ', $row_rs_recipes['ingredients'])) . '</li></ul>';
}

Comments

0

You can do this:

$ingredients = explode(',', $row_rs_recipes['ingredients']);

$list = '<ul>';

foreach ($ingredients as $ingredient) 
{
   $list .= '<li>' . $ingredient . '</li>';
}
$list .= '</ul>';

echo $list;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.