0

i am trying to remove the old value of an array i searched over the internet i just found the method unset() using this solution but my case it seems different some how here is my full code

<?php



header('Content-type: application/json; charset=utf-8');
$link = mysqli_connect("localhost","root","");
mysqli_query($link,"SET NAMES UTF8");
$db= mysqli_select_db($link,"Mirsa") or die("Cannot select Database.");



 if (isset($_GET['Product_ID']) ){
   $productId;
    $Menu_ID =$_GET['Product_ID'];



$query = "SELECT 
   mirsaProductId,mirsaProductCode,mirsaProductDescription,
   mirsaProductPcsInCartoon,mirsaProductWeight,mirsaProductVolume,
    mirsaProductCodeBType,mirsaProductCodeCType,FK_mirsaCategoryId 
   from mirsaProduct WHERE FK_mirsaCategoryId='$Menu_ID'";



    $result = mysqli_query($link,$query) or die(mysql_error($link));

                $response["Products"] = array();
                $products = array();
         while ($row = mysqli_fetch_array($result)) {
                $image[]=array()         
                $products["mirsaProductId"] =          $row["mirsaProductId"];
                $products["mirsaProductCode"]         = $row["mirsaProductCode"];
                $products["mirsaProductDescription"]  = $row["mirsaProductDescription"];
                $products["mirsaProductPcsInCartoon"] = $row["mirsaProductPcsInCartoon"];
                $products["mirsaProductWeight"] =      $row["mirsaProductWeight"];
                $products["mirsaProductVolume"] =       $row["mirsaProductVolume"];
                $products["mirsaProductCodeBType"] =   $row["mirsaProductCodeBType"];
                $products["mirsaProductCodeCType"] =   $row["mirsaProductCodeCType"];
                $productId = $row["mirsaProductId"];




$query2 = "SELECT mirsaProductUrlImage FROM mirsaProduct,mirsaProductImages
WHERE FK_mirsaProductId = '$productId' GROUP BY mirsaProductUrlImage";     

    $result2=mysqli_query($link, $query2);
    while ($row2 = mysqli_fetch_array($result2))
      {
             $image [] = $row2["mirsaProductUrlImage"];
             $products["mirsaProductUrlImage"]=$image;
      }
     array_push($response["Products"], $products);





        }
                  die(json_encode($response));


  }

// echoing JSON response

 else {
// no products found
$response["success"] = 0;
$response["message"] = "No Products";
   die(json_encode($response));
 }
    ?>

i am getting a response of this way

  {
 ProductId: "1",
 ProductCode: "118.023",
 ProductDescription: "NEM OVAL (Transparent)",
 ProductPcsInCartoon: "10",
 ProductWeight: "7.55 - 8.05(KG)",
 ProductVolume: "0.104(m3)",
 ProductCodeBType: "NA",
 ProductCodeCType: "NA",
 ProductUrlImage: [
  "1.png",
  "2.png"
 ]
 },


{
    ProductId: "2",
    ProductCode: "118.024",
    ProductDescription: "NEM OVAL (Opac)",
    ProductPcsInCartoon: "10",
    ProductWeight: "7.55 - 8.05(KG)",
    ProductVolume: "7.55 - 8.05(KG)",
    ProductCodeBType: "NA",
    ProductCodeCType: "NA",
    ProductUrlImage: [
            "1.png",
            "2.png"
          ]

the second JSON object is not containing these 2 url of image it takes the old value of the $image[] so i need to reinitialised or empty his value in a way i need your help guys you are always our support

4
  • 1
    Your code is vulnerable to SQL injection attacks. You should use mysqli or PDO prepared statements with bound parameters as described in this post. Apr 26, 2017 at 17:59
  • i know its just an api getting json value for my iOS app no transaction no secret info...lets focus on how to solve my problem then i will update my code Apr 26, 2017 at 18:01
  • If I understood the question right, you don't need an $image array there. So instead this -> $image [] = $row2["mirsaProductUrlImage"]; $products["mirsaProductUrlImage"]=$image; Use this -> $products["mirsaProductUrlImage"]=$row2["mirsaProductUrlImage"];
    – Оzgur
    Apr 26, 2017 at 18:10
  • no mate i need the array[] but i need to empty the array on each time he end the loop Apr 26, 2017 at 18:11

1 Answer 1

0

I am going to take a stab. Since you never really initialize $image to be an array. Do you actually intend for:

$image[] = array()

to be:

$image = array();

That would cause the $image array to be reset each time through the first while loop.

2

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.