Bogdanel 123
Bogdanel 123

Reputation: 43

$result_array only returning one row

The query below should return multiple ingredients, but it only returns one ingredient. What am I doing wrong?

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $result_array['id'] = $row['id'];
        $result_array['name'] = $row['product_name'];
        $result_array['phone'] = $row['categories_name'];
        $result_array['image'] = $row['url'];

        $sqlI = "SELECT * FROM `ingredients` inner join products_ingredients on ingredients.id=products_ingredients.idIngredient WHERE idProduct=".$row['id']."";
        $resultI = $conn->query($sqlI);

        if($resultI->num_rows > 0) {
            while($rowI = $resultI->fetch_array()) {
                $result_array['ingredients'] = $rowI['ingredient_name'];

            }
        } 
    array_push($products, $result_array);   
    }
}

And when I use echo it displays all the rows

 $sqlI = "SELECT * FROM `ingredients` inner join products_ingredients on ingredients.id=products_ingredients.idIngredient WHERE idProduct=".$row['id']." ORDER BY ingredient_name ASC";
    $resultI = $mysqli->query($sqlI);

    if($resultI->num_rows > 0) {
        while($rowI = $resultI->fetch_assoc()) {
            echo $rowI['ingredient_name'].' <a href="removeProdIngred.php?id='.$rowI['id'].'">X</a><br />';
        }
    }

The output

[1] => Array
        (
            [id] => 2
            [name] => Supa de rosii (350 ml)
            [phone] => Supe
            [image] => https://restaurant.onlinebusiness.university/img/products/2/2.jpg
            [ingredients] => ceapa
        )

Upvotes: 0

Views: 59

Answers (1)

sohan verma
sohan verma

Reputation: 86

You need to add [] after $result_array['ingredients'] array, then it will store all ingredient_name name into array.

$result_array['ingredients'][] = $rowI['ingredient_name'];

Upvotes: 1

Related Questions