0

I have this function:

public function findFavoriteMerchant($userId) {
  // I grab all the merchant_id from favoriteMerchant where user_id = $userId
  $q = $this->_db->prepare("SELECT * FROM favoriteMerchant WHERE user_id = ?");
  $q->bindParam(1, $userId, PDO::PARAM_INT);


        if ($q->execute()){

        $result = $q->fetchAll(PDO::FETCH_ASSOC);
        $i = -1;

              foreach ($result as $row) {

              $merchant = $this->_db->prepare("SELECT * FROM merchant WHERE merchant_id = ?");
              $merchant->bindParam(1, $row["merchant_id"], PDO::PARAM_INT);

                   if ($merchant->execute()){

                       $i++;

                       $merchantArray[$i] = $merchant->fetchAll(PDO::FETCH_ASSOC);
                    }
               }        
               return $merchantArray;
         }
} 

that return this :

[
  [ 
    {
        "merchant_id": "17",
        "business_name": "Nails Spa",
        "merchant_first_name": "Jonh ",
        "merchant_last_name": "Lampard",
        "merchant_email": "[email protected]",
        "merchant_address": "123 lampard street",
        "merchant_country": "United States",
        "merchant_city": "San Francisco ",
        "merchant_state": "Dubai",
        "merchant_postalCode": "92",
        "merchant_telephone": "043251653",
        "merchant_industry": "Beauty",
        "merchant_type": "Spa",
        "merchant_service": "other",
        "merchant_lat": "37.804512",
        "merchant_long": "-122.432335"
    }
],
[
    {
        "merchant_id": "19",
        "business_name": "Massage Spa",
        "merchant_first_name": "carl",
        "merchant_last_name": "smith",
        "merchant_email": "[email protected]",
        "merchant_address": "",
        "merchant_country": "United States",
        "merchant_city": "San Francisco ",
        "merchant_state": "Dubai",
        "merchant_postalCode": "92",
        "merchant_telephone": "043278439",
        "merchant_industry": "Beauty",
        "merchant_type": "Spa",
        "merchant_service": "other",
        "merchant_lat": "25.037189",
        "merchant_long": "55.251812"
    }
  ]
]

but I would like to get this:

[
    {
        "merchant_id": "17",
        "business_name": "Nails Spa",
        "merchant_first_name": "Jonh ",
        "merchant_last_name": "Lampard",
        "merchant_email": "[email protected]", 
      // and so on....

****UPDATE****

    $merchant =  new Merchant();
    echo json_encode($merchant->findFavoriteMerchant($userId),JSON_PRETTY_PRINT);

print r returns:

    Array
(
    [0] => Array
        (
            [merchant_id] => 17
            [business_name] => Massage Spa
            [merchant_first_name] => Jonh 
            [merchant_last_name] => Lampard
            [merchant_email] => [email protected]
            [merchant_address] => 123 lampard street
            [merchant_country] => United States
            [merchant_city] => San Francisco 
            [merchant_state] => Dubai
            [merchant_postalCode] => 92
            [merchant_telephone] => 043251653
            [merchant_industry] => Beauty
            [merchant_type] => Spa
            [merchant_service] => other
            [merchant_lat] => 37.804512
            [merchant_long] => -122.432335
            [merchant_userName] => Aviation 
            [password] => cfc10a708f01fe27750e3be246ca1daa
        )

)
Array
(
    [0] => Array
        (
            [merchant_id] => 19
            [business_name] => Angsana Spa
            [merchant_first_name] => carl
            [merchant_last_name] => smith
            [merchant_email] => [email protected]
            [merchant_address] => 
            [merchant_country] => United States
            [merchant_city] => San Francisco 
            [merchant_state] => Dubai
            [merchant_postalCode] => 92
            [merchant_telephone] => 043278439
            [merchant_industry] => Beauty
            [merchant_type] => Spa
            [merchant_service] => other
            [merchant_lat] => 25.037189
            [merchant_long] => 55.251812

        )

)

I tried several approaches but I don't get it

4
  • update the question to include the part where you build the final array you do json_encode on it Commented Jan 16, 2016 at 13:04
  • also, if you could add a print_r($merchantArray[$i]); inside the loop and update the question with the output that would be great Commented Jan 16, 2016 at 13:07
  • do you want only one result? maybe it is data problem. is it sure that the user has one favorite merchant ? Commented Jan 16, 2016 at 13:10
  • user in the database has 2 favourite merchant. I need both Commented Jan 16, 2016 at 13:13

3 Answers 3

1

You need to take the individual elements from the return of fetchAll since it returns an array.

Use a simple foreach to populate your final array.
Everything else stays the same, return $merchantArray, json_encode it then print it out.

$results = $merchant->fetchAll(PDO::FETCH_ASSOC);
foreach($results as $result){
    $merchantArray[$i] = $result;
}
Sign up to request clarification or add additional context in comments.

1 Comment

this also works! I think this is a more correct answer!
1

$merchant->fetchAll() returns an array. you added an array to result array, two times. take a first item or use foreach loop.

3 Comments

$data = $merchant->fetchAll(PDO::FETCH_ASSOC); $merchantArray[$i] = $data[0]; I did this and it worked! Thanks
Thanks @wafe, I selected the other answer because I think it's more complete and help other people to understand what to do. Thanks anyway +1
@mat I agree :) i am on moblie, so my answer is not pretty.
1

You could either extract the first row from the result of the second query

$merchantArray[$i] = $merchant->fetchAll(PDO::FETCH_ASSOC)[0];

Or you could use a JOIN to get all of the information at once

public function findFavoriteMerchant($userId) {
  // grab all merchant information from merchant where the merchant_id matches from favoriteMerchant where user_id = $userId
  $q = $this->_db->prepare("
    SELECT merch.*
    FROM favoriteMerchant AS fav
    INNER JOIN merchant merch ON fav.merchant_id = merch.merchant_id
    WHERE user_id = ?");
  $q->bindParam(1, $userId, PDO::PARAM_INT);

    if ($q->execute()){
      $merchantArray = $q->fetchAll(PDO::FETCH_ASSOC);
      return $merchantArray;
    }
} 

Comments

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.