0

I am new to php webservices. I wish to get sql data in following format: enter image description here

SQL query that am using is:

    $result = mysql_query("select r.id,r.Name, o.`Title`, o.id, o.`Description`, o.`OfferLocation`
from `restaurants` r left join `offers` o on r.id = o.`restaurant_id`");

With this query I get redundant restaurant names for each unique offer which is fine. But how do I format this data as given in the image as a multidimensional array?

Following is the code I have tried so far:

if(mysql_num_rows($result) > 0){
        $response["restaurant"] = array();$cnt=0;
        $item_rest=array();
            while($row=mysql_fetch_array($result)){
                $item_rest["RestaurantID"]=$row["id"];
            }
            $response_offer["offers"]=array();
            while($row=$result->fetch_array($result)){

                $item = array();

                $item["Offer"]= $row["Title"];
                $item["OfferId"]= $row["id"];
                $item["Description"]= $row["Description"];
                $item["OfferLocation"]= $row["OfferLocation"];

                array_push($response_offer["offers"],$item);
            }
            array_push($response["restaurant"],$response_offer);
            $response["success"]=1;
        }

How do I do this?

5
  • 1
    That's json. Look at json_encode. Commented Oct 3, 2014 at 9:18
  • Ya that's right. It's json. But how do I format it? Can you give me head start for the same? Commented Oct 3, 2014 at 9:21
  • Can you provide a var_export($response);, please Commented Oct 3, 2014 at 9:25
  • please paste data format in pastebin.com image is blurred Commented Oct 3, 2014 at 9:51
  • you are constructing the array $response wrong. Commented Oct 3, 2014 at 9:55

1 Answer 1

0

I think you are trying to use two APIs at the same time, try changing $result->fetch_array($result) (mysqli) into mysql_fetch_array($result) (mysql).

Consider to stop using mysql_ functions as they're deprecated, more here

0

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.