0

I need to generate the following JSON from a PHP loop and SQL DB but I'm having trouble:

    [
      {
        "ItemName": "Websites1",
        "Websites1": [
          {
            "0": "1",
            "ID": "1",
            "1": "Essential",
            "WebsiteName": "Essential 1",
            "2": "EI",
            "WebsiteCode": "EI"
          },
          {
            "0": "2",
            "ID": "2",
            "1": "Icon Ibiza",
            "WebsiteName": "Icon 1",
            "2": "IC",
            "WebsiteCode": "IC"
          },
          {
            "0": "3",
            "ID": "3",
            "1": "So Ibiza",
            "WebsiteName": "So 1",
            "2": "SO",
            "WebsiteCode": "SO"
          }
        ]
      },
      {
        "ItemName": "Websites2",
        "Websites2": [
          {
            "0": "1",
            "ID": "1",
            "1": "Essential Ibiza",
            "WebsiteName": "Essential 2",
            "2": "EI",
            "WebsiteCode": "EI"
          },
          {
            "0": "2",
            "ID": "2",
            "1": "Icon Ibiza",
            "WebsiteName": "Icon 2",
            "2": "IC",
            "WebsiteCode": "IC"
          },
          {
            "0": "3",
            "ID": "3",
            "1": "So Ibiza",
            "WebsiteName": "So 2",
            "2": "SO",
            "WebsiteCode": "SO"
          }
        ]
      }
    ]

I have the relevant data being returned from the DB into PHP fine but I can't work out how to generate the relevant key value pairs and nesting using PHP loops. The code I have so far is:

            $this->db->sql =    "SELECT ID AS ID, WebsiteName AS SelectText, WebsiteCode AS SelectValue FROM Banners_Websites ORDER BY WebsiteName";

            $rs = $this->db->query($this->db->sql);

            if ($this->db->row_count != 0) {

                $response = array();
                $json = array();

                while ($row = $this->db->row_read($rs)) {

                    $json["ID"] = $row["ID"];
                    $json["SelectText"] = $row["SelectText"];
                    $json["SelectValue"] = $row["SelectValue"];

                    //$json[] = $row;                               
                }

                $response["Websites1"] = $json;


            }


            $rs = $this->db->query($this->db->sql);

            if ($this->db->row_count != 0) {

                $json2 = array();

                while ($row = $this->db->row_read($rs)) {

                    $json2["ID"] = $row["ID"];
                    $json2["SelectText"] = $row["SelectText"];
                    $json2["SelectValue"] = $row["SelectValue"];

                    //$json[] = $row;                               
                }

                $response["Websites2"] = $json2;

            }



            return '[' . json_encode($response) . ']';

I'm obviously doing something very wrong as I only end up with one row for each query. The key value pairs are being overwritten with each loop. Also the nesting isn't correct. Sorry for such a dumb question but I've been trying to figure this out from info online and am stuck.

Thanks,

Noon.

EDIT - Managed to fix this using the annswer provided by Anushil Nandan below but the PHP needed a few extra tweaks to get the required format as folows:

            // -----------------------------------------------------------------------------------------------
            // BANNERMANGEMENTFORM
            function bannerManagementJson($JsonItem) {

                $this->db->connect();

                $response = array();

                //Generate Websites drop-down
                $SqlStr = "SELECT WebsiteName AS SelectText, WebsiteCode AS SelectValue FROM Banners_Websites ORDER BY WebsiteName";
                $response[] = $this->retrieveFormElementJson($SqlStr,'Websites',1);

                //Generate Clients drop-down
                $SqlStr = "SELECT ClientName AS SelectText, ID AS SelectValue FROM Banners_BannerClients ORDER BY ClientName";
                $response[] = $this->retrieveFormElementJson($SqlStr,'Clients',2);

                return json_encode($response);

            }   

            // -----------------------------------------------------------------------------------------------
            // RETRIEVEFORMELEMENTJSON
            function retrieveFormElementJson($SqlStr,$ElementName,$SelectListNo) {

                $this->db->sql = $SqlStr;

                $rs = $this->db->query($this->db->sql);

                if ($this->db->row_count != 0) {

                    $json = array();

                    while ($row = $this->db->row_read($rs)) {
                        $json[] = $row;
                    }

                    $arrayResponse = array("ItemName"=>$ElementName, "SelectList" . $SelectListNo=>$json);

                }

                return $arrayResponse;

            }   
1
  • this commented line is you need $json[] = $row; Commented Apr 22, 2017 at 11:30

1 Answer 1

1

your array is being overridden every time it's entering while loop

try: $json1[] = array(); $json2[] = array();

        $this->db->sql =    "SELECT ID AS ID, WebsiteName AS SelectText, WebsiteCode AS SelectValue FROM Banners_Websites ORDER BY WebsiteName";

        $rs = $this->db->query($this->db->sql);

        if ($this->db->row_count != 0) {

            $response = array();
            $json[] = array();

            while ($row = $this->db->row_read($rs)) {

                $json[]["ID"] = $row["ID"];
                $json[]["SelectText"] = $row["SelectText"];
                $json[]["SelectValue"] = $row["SelectValue"];

                //$json[] = $row;                               
            }

            $response["Websites1"] = $json;


        }


        $rs = $this->db->query($this->db->sql);

        if ($this->db->row_count != 0) {

            $json2[] = array();

            while ($row = $this->db->row_read($rs)) {

                $json2[]["ID"] = $row["ID"];
                $json2[]["SelectText"] = $row["SelectText"];
                $json2[]["SelectValue"] = $row["SelectValue"];

                //$json[] = $row;                               
            }

            $response["Websites2"] = $json2;

        }



        return '[' . json_encode($response) . ']';
Sign up to request clarification or add additional context in comments.

3 Comments

still your code looks overwriting each time $json["ID"] = $row["ID"];
Needed to make a few extra tweaks but this got me there so thanks! I've added the extra tweaks to my original question
Don't use string concatenation to add the [] characters at the end. Use return json_encode([$response]);

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.