0

The view must be like this:

{
  "app": [
    {
      "system": {
        "base_url": "https://youtube.com",
        "email_address": "[email protected]",
        "privacy_policy_url": "https://googles.com",
        "terms_of_use_url": "https://fasfg.com",
        "splash_screen_timout": 1000,
        "loading_spinner": "Wave"
      }
    },
    {
      "settings": {
        "dark_mode": true,
        "screen_sleep": true,
        "fullscreen_mode": true,
        "notification_sound_option": true,
        "notification_vibration_option": true,
        "download_via_wifi": true,
        "device_information": true,
        "other_links": true,
        "danger_zone": true
      }
    }

But when I try to make it like this it overwrites the old function and prints the last object like this:

{
  "app": [
    {
      "settings": {
        "dark_mode": true,
        "screen_sleep": true,
        "fullscreen_mode": true,
        "notification_sound_option": true,
        "notification_vibration_option": true,
        "download_via_wifi": true,
        "device_information": true,
        "other_links": true,
        "danger_zone": true
      }
    }

this is my PHP code i know the problem but I can't fix it, I think the answer is appending data but I don't know how and I searched for answer but nothing clear for me:

$fetch_system = $db->prepare("SELECT base_url, email_address, 
                                    privacy_policy_url, terms_of_use_url, 
                                    splash_screen_timout, loading_spinner 
                               FROM configuration 
                               WHERE secret_api_key=?");

$fetch_system->bind_param("s", $_SESSION['secret_api_key']);
$fetch_system->execute();

$rows = array();

$result = $fetch_system->get_result();

while($rows1 = $result->fetch_assoc()) {
    $rows['app'] = $rows1;
}


$fetch_settings = $db->prepare("SELECT dark_mode, screen_sleep, full_screen, 
                                        notification_sound, notification_vibration, 
                                        download_via_wifi, device_information, 
                                        other_links, danger_zone 
                                FROM configuration  
                                WHERE secret_api_key=?");

$fetch_settings->bind_param("s", $_SESSION['secret_api_key']);
$fetch_settings->execute();

$rows['app'] = array();

$result = $fetch_settings->get_result();
while($rows2 = $result->fetch_assoc()) {
    $rows['settings'] = $rows2;
}
echo json_encode($rows);
3
  • The {} do not balance in your JSONs. And you have an unclose [.in each json. Please fix this so it is easier to understand. Commented Feb 3, 2020 at 22:02
  • 2
    Try removing the second $rows['app'] = array();, you are clearing the values saved before. Commented Feb 3, 2020 at 22:03
  • Thanks, it works now. you saved my day :) Commented Feb 3, 2020 at 22:08

1 Answer 1

2

Because DB communication may be expensive, so one query and building resulting array as you need may be more efficient solution.

#get all configuration by secret_api_key
$fetch_system = $db->prepare("SELECT * FROM configuration WHERE secret_api_key=?");

$fetch_system->bind_param("s", $_SESSION['secret_api_key']);
$fetch_system->execute();
$result = $fetch_system->get_result();
$configuration = $result->fetch_assoc();

#build result from data
$rows = array(
    'app'=>array(
        'system' => array (
            'base_url' => $configuration['base_url'],
            'email_address' => $configuration['email_address'],
            'privacy_policy_url' => $configuration['privacy_policy_url'],
            'terms_of_use_url' => $configuration['terms_of_use_url'],
            'splash_screen_timout' => $configuration['splash_screen_timout'],
            'loading_spinner' => $configuration['loading_spinner']
        ),
        'settings' => array (
            'dark_mode' => $configuration['dark_mode'],
            'screen_sleep' => $configuration['screen_sleep'],
            'fullscreen_mode' => $configuration['fullscreen_mode'],
            'notification_sound_option' => $configuration['notification_sound_option'],
            'notification_vibration_option' => $configuration['notification_vibration_option'],
            'download_via_wifi' => $configuration['download_via_wifi'],
            'device_information' => $configuration['device_information'],
            'other_links' => $configuration['other_links'],
            'danger_zone' => $configuration['danger_zone']
        )
    )
);

echo json_encode($rows);
Sign up to request clarification or add additional context in comments.

1 Comment

that's the best answer I saw on stack overflow, I'm an android developer who wants to configure his app online so, I'm new on PHP but this answer solves it all. thank you :)

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.