Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

creating a dynamic response php. filter business name and add to specific array key

I have a query in PHP that I wrote that has 3 LEFT JOINs. I get information from a business, client, and a service the business offers. I am looking for a way to create the response to the front end that inserts the information into a dedicated array. This is all dynamic though.

To keep things easier to understand, lets say my data is like so…

{
  "success": "true",
  "appts": [
    {
      "businessName": "abc1",
      "clientId": "aHoAcu0ZGzt9nxpwXr1MBqkDb",
      "serviceId": "aHoAcu0ZGzt9nxpwXr1MBqkDb"
    },
    {
      "businessName": "abc2",
      "clientId": "aHoAcu0ZGzt9nxpwXr1MBqkDb",
      "serviceId": "aHoAcu0ZGzt9nxpwXr1MBqkDb"
    },
    {
      "businessName": "abc3",
      "clientId": "aHoAcu0ZGzt9nxpwXr1MBqkDb",
      "serviceId": "aHoAcu0ZGzt9nxpwXr1MBqkDb"
    },
    {
      "businessName": "abc2",
      "clientId": "aHoAcu0ZGzt9nxpwXr1MBqkDb",
      "serviceId": "aHoAcu0ZGzt9nxpwXr1MBqkDb"
    },
    {
      "businessName": "abc1",
      "clientId": "aHoAcu0ZGzt9nxpwXr1MBqkDb",
      "serviceId": "aHoAcu0ZGzt9nxpwXr1MBqkDb"
    },
    {
      "businessName": "abc1",
      "clientId": "aHoAcu0ZGzt9nxpwXr1MBqkDb",
      "serviceId": "aHoAcu0ZGzt9nxpwXr1MBqkDb"
    },
    {
      "businessName": "abc4",
      "clientId": "aHoAcu0ZGzt9nxpwXr1MBqkDb",
      "serviceId": "aHoAcu0ZGzt9nxpwXr1MBqkDb"
    }
  ]
}

What I am looking to do is something more custom, like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  "success": "true",
  "appts": [
    {
      "abc1": [
        {
          "businessName": "abc1",
          "clientId": "aHoAcu0ZGzt9nxpwXr1MBqkDb",
          "serviceId": "aHoAcu0ZGzt9nxpwXr1MBqkDb"
        },
        {
          "businessName": "abc1",
          "clientId": "aHoAcu0ZGzt9nxpwXr1MBqkDb",
          "serviceId": "aHoAcu0ZGzt9nxpwXr1MBqkDb"
        },
        {
          "businessName": "abc1",
          "clientId": "aHoAcu0ZGzt9nxpwXr1MBqkDb",
          "serviceId": "aHoAcu0ZGzt9nxpwXr1MBqkDb"
        }
      ]
    },
    {
      "abc2": [
        {
          "businessName": "abc2",
          "clientId": "aHoAcu0ZGzt9nxpwXr1MBqkDb",
          "serviceId": "aHoAcu0ZGzt9nxpwXr1MBqkDb"
        },
        {
          "businessName": "abc2",
          "clientId": "aHoAcu0ZGzt9nxpwXr1MBqkDb",
          "serviceId": "aHoAcu0ZGzt9nxpwXr1MBqkDb"
        }
      ]
    }
  ]
}

Here is my query

$query = "SELECT a.StartTime, a.ClientId, a.ServiceId, a.id, a.WhoEdit, a.EndTime, a.Confirmed, a.WhoBooked, a.AppointmentNote, c.ClientFirstName, 
            c.ClientLastName, c.ClientPhoneNumber, c.ClientAddress, c.ClientAddress2, b.BusinessName, b.BusinessAddress, b.BusinessAddress2, b.BusinessCity,
            b.BusinessState, b.BusinessZip, b.BusinessOwnerName, b.BusinessOwnerEmail, b.BusinessOwnerPhoneNumber, c.ClientCity, c.ClientState, c.ClientZip, s.ServiceName, s.ServicePrice, 
            s.ServiceLength, s.ServiceNote FROM appointments a 
        LEFT JOIN clients c ON a.ClientId = c.ClientId
        LEFT JOIN services s ON a.ServiceId = s.ServiceId
        LEFT JOIN business b ON a.BusinessId = b.BusinessId
        WHERE a.Confirmed='false' OR a.confirmed='' ORDER BY b.BusinessName asc, a.StartTime desc";  

Is there an easy way to handle this before I echo it out to the client?

>Solution :

I hope I am reading your comment correctly. if i am this would give you the result you are looking to achieve please adapt the $result variable to whatever is the result set from your query

<?php
    
    $organizedData = array();
    
    foreach ($result as $row) {
        $businessName = $row['BusinessName'];
        
        // Create a new entry for the business if it doesn't exist
        if (!isset($organizedData[$businessName])) {
            $organizedData[$businessName] = array();
        }
    
        // Add the appointment details to the business entry
        $appointmentDetails = array(
            "businessName" => $row['BusinessName'],
            "clientId" => $row['ClientId'],
            "serviceId" => $row['ServiceId']
            // Add other fields as needed
        );
    
        $organizedData[$businessName][] = $appointmentDetails;
    }
    
    // Create the final response array
    $response = array(
        "success" => "true",
        "appts" => array_map(function ($businessName, $appointments) {
            return array($businessName => $appointments);
        }, array_keys($organizedData), $organizedData)
    );
    
    // Convert the response to JSON and return it
    echo json_encode($response);

I hope this helps its very hard to give a more accurate response without further information

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading