Hello. Such a problem, I upload ads from the server, including the paths to the images. I will explain the essence, I send a request to the server, it forms a list of ads in the current category and I try to create an array already in the current array img_name[] . Before adding rows to an array, I create a query in the database ( если 'id_ads равен $row['id'], то добавить строку . Rows are added, but also those that do not fit my condition are added. Ie, for example, I I pull out the image paths for the ad with id=233 , in the base of which there are 2 photos (paths), and in the ad with id=234 , 1 photo (path), then in the response from the server I get the paths to the pictures I don't need for each ad. :

enter image description here

 "id": "234", "user_id": "1", "img_name": [ "1_img.jpg" --- id_ads == 234 ] "id": "233", "user_id": "1", "img_name": [ "1_img.jpg", --- id_ads == 234 !!!! - откуда? "2_img.jpg", --- id_ads == 233 "3_img.jpg" --- id_ads == 233 ] 

In general, I will attach the screenshots more, explained as I could. Help me please. Here is the server function

 function adsLoading($cat) { $sql = mysql_query('SELECT * FROM `ads` WHERE `new_ads` = "0" AND `cat` = '.$cat.' ORDER BY `id` DESC'); if(mysql_num_rows($sql) > 0) { while($row = mysql_fetch_array($sql)) { $id = $row['id']; $data['id'] = id; $user_id = $row['user_id']; $data['user_id'] = $user_id; $picAds = mysql_query('SELECT * FROM `upload_img_ads` WHERE `user_id` = 1 AND `temp` = 0 AND `id_ads` = '.$id.''); if(mysql_num_rows($picAds) > 0) { while($img = mysql_fetch_array($picAds)) { $image = $img['img_name']; $id_ads = $img['id_ads']; $data['img_name'][] = $image; } } $vrl['data'][] = $data; } } else { $data['id'] = 0; $vrl['data'][] = ""; } exit(json_encode(array("data" => $vrl['data']))); } 

enter image description here

The screenshot shows that in the array with id = 232, somehow miraculously adds data that does not belong to this id, despite the fact that in the sample condition this is indicated.

  • You need to initialize, young man - vp_arth
  • one
    $data = array(); in the right place will solve all ( no ) your problems. - vp_arth
  • @vp_arth and what does this initialization change? With it on the contrary, empty lines are added to the array: "", "img_name": image.jpg - sergei1094
  • You just have to understand that “some kind of miracle” is you. Because you reuse already filled array. The extra lines remain there from previous iterations of the loop. - vp_arth
  • Initialization of the array did not change anything - sergei1094

1 answer 1

You just have to understand that you are re-using an already filled array.
The extra lines remain there from previous iterations of the loop.

 function adsLoading($cat) { $sql = mysql_query('SELECT * FROM `ads` WHERE `new_ads` = "0" AND `cat` = '.$cat.' ORDER BY `id` DESC'); if(mysql_num_rows($sql) > 0) { while($row = mysql_fetch_array($sql)) { $id = $row['id']; /* * Инициализация! */ $data = array(); $data['id'] = id; $user_id = $row['user_id']; $data['user_id'] = $user_id; $picAds = mysql_query('SELECT * FROM `upload_img_ads` WHERE `user_id` = 1 AND `temp` = 0 AND `id_ads` = '.$id.''); if(mysql_num_rows($picAds) > 0) { while($img = mysql_fetch_array($picAds)) { $image = $img['img_name']; $id_ads = $img['id_ads']; $data['img_name'][] = $image; } } $vrl['data'][] = $data; } } else { $data['id'] = 0; $vrl['data'][] = ""; } exit(json_encode(array("data" => $vrl['data']))); } 
  • Thank you so much!) I already realized that I was re-using it, only I initialized it after function adsLoading($cat) { .. Now everything works like a clock :) Thanks again - sergei1094
  • In addition, I would recommend that you familiarize yourself with the red plate here . Well, read about the protection against sql-injection. - vp_arth
  • respected. Thanks for the care of course) Protection will be necessary, for now, for now, only the framework and the architecture. - sergei1094
  • Especially, if the project is still at the creation stage, be sure to change the database access API from mysql_ to mysqli_ or PDO - vp_arth
  • On mysqli_ and it is most convenient for me to work, in this example, this is just "I repeat it again" - a skeleton :) Thank you once again. - sergei1094