A
A
arsenaljek2020-05-28 06:51:08
PHP
arsenaljek, 2020-05-28 06:51:08

Forming json with nested loops?

I'm trying to generate json.
The problem is that the second loop with pictures doesn't output images that are specifically related to a particular product, but all these images "increase" with each product.
The array looks like this

Array
(
    [0] => Array
        (
            [link_tov] => link
            [name_tov] => название товара_1
            [rub_inf] => описание рубрики
            [price_tov] => цена розница
            [price_tov_zavod] => цена опт
            [name] => название категории
            [link] => урл категории
            [images] => https://изображение_1.png;https://изображение_2.png;https://изображение_3.png;
        )

    [1] => Array
        (
            [link_tov] => link
            [name_tov] => название товара_2
            [rub_inf] => описание рубрики
            [price_tov] => цена розница
            [price_tov_zavod] => цена опт
            [name] => название категории
            [link] => урл категории
            [images] => Опять идёт изображение_1 из прошлого товара;Опять идёт изображение_2 из прошлого товара;Опять идёт изображение_3 из прошлого товара; А вот тут добавляется уже нормальное изображение относяшиеся к данному товару
        )

)

The array formation code
<?php
while($row = mysqli_fetch_assoc($resultProduct)) {
    $sql1 = "SELECT * FROM `images` WHERE `tovar_id` = '".$row['id_tov']."' AND type = 'standart' LIMIT 10 ";
    $result1 = mysqli_query($link,$sql1);
    while($img = mysqli_fetch_assoc($result1)) {
        $images .= $img['url'];
    }
    $use = strip_tags($row['use']);
    $price[]=array("link_tov"=>$row['link_tov'],"name_tov"=>$row['name_tov'],"rub_inf"=>$row['rub_inf'],"use"=>$use,"price_tov"=>$row['price_tov'],"price_tov_zavod"=>$row['price_tov_zavod'],"name"=>$row['status'],"link"=>$row['link'],"images"=>$images);
}
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Q
qwermus, 2020-05-28
@qwermus

Images need to be reset. In general, any variable used must first be defined before being used. Those. immediately after the first while you put And now, with each new product, the images will be clean.
$images = '';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question