John Davidson

javascript - Display JSON datas in column with PHP then return selection in the same format

0 comments
Message:


I am currently working on a ticket reservation script (With date, time of reservation then quantity available).


I am trying to communicate with an API which is supposed to send me the data from the db in JSON format so that I can interpret it and display in PHP in a column in which I should display the day in the header of the column, the hour in each cells and a default quantity ( In my example we start from 60/60 which will be decremented by 1 when selecting the user.


For the moment, I'm just trying to manage to automatically create a column for each date, with the values ​​to select and the value of the remaining quantity (Knowing that a selection decrements by 1) then return the result in JSON format to the API to update the database and save the selection by assigning the user's IP to it.


I started a small script which retrieves the elements of the database in JSON format for the example, I think I have to create a foreach loop to create my columns but I'm stuck at the moment. Thank you for the leads or any help you think that you could bring me.


here's a picture of what i am trying to do:


enter image description here


<?php
try{
$pdo=new PDO(
'mysql:host=localhost;dbname=date_booking',
'root','',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
} catch(PDOException $e){
echo $e->getMessage();
}

$statement=$pdo->prepare("SELECT * FROM tbl_booking");
$statement->execute();
$datas = array();
while($res=$statement->fetch(PDO::FETCH_ASSOC)) {
$datas[]=$res;
}
$someArray = $datas; // Replace ... with your PHP Array
foreach ($someArray as $key => $value) {
echo '<pre>';
echo $value["date_booking"]. ' | ' . $value["hour_booking"]. ' | ' . $value["nb_booking"];
echo '</pre>';
}


My Answer:
To achieve this, you can follow these steps:

1. Retrieve the JSON data from the API using PHP.
2. Parse the JSON data and loop through each date to create columns.
3. Display the day in the header of each column, the hour in each cell, and the default quantity.
4. Allow the user to select a quantity, decrement it by 1, and update the display.
5. Return the updated data in JSON format to the API to update the database.

Here's an example code snippet to get you started:

php

// Retrieve JSON data from the API
$json_data = file_get_contents('http://api.example.com/data');
$data = json_decode($json_data, true);

// Loop through each date to create columns
$columns = [];
foreach ($data as $date => $hours) {
$column = [];
$column['day'] = $date;
$column['hours'] = [];

foreach ($hours as $hour => $quantity) {
$column['hours'][] = [
'hour' => $hour,
'quantity' => $quantity
];
}

$columns[] = $column;
}

// Display the columns in JSON format
echo json_encode($columns);

?>


This code snippet will help you get started with displaying the JSON data in columns with PHP. You can then add functionality to allow the user to select a quantity, decrement it by 1, and update the display accordingly. Finally, you can return the updated data in JSON format to the API to update the database.

Rate this post

5 of 5 based on 3296 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us