ARRAY MULTIPLE OBJETO JSON API 2 TABLAS O MAS - VER EN TABLA HTML
<?php
$dbh = new PDO("mysql:host=localhost;dbname=DBNAME", $username, $password);
$sql = "SELECT `id`, `title`, `time`, `start`, `backgroundColor`
FROM my_table";
$result = $dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC);
//To output as-is json data result
//header('Content-type: application/json');
//echo json_encode($result);
//Or if you need to edit/manipulate the result before output
$return = [];
foreach ($result as $row) {
$return[] = [
'id' => $row['id'],
'title' => $row['title'],
'start' => $row['start'].' '.$row['time'],
'backgroundColor' => $row['backgroundColor']
];
}
$dbh = null;
header('Content-type: application/json');
echo json_encode($return);
?>
///////////////////////
$array = array();
while($row = mysqli_fetch_array($result))
{
array_push($array,$row);
}
$json_array = json_encode($array);
////////////////////
$data = array(); // result variable
$i=0
$query = "SELECT id,title,start,backgroundColor FROM my_table"; // query with SELECT
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){ // iterate over results
$data['item'][$i]['id'] = $row['id']; // rest similarly
...
...
$i++;
}
header('Content-type: application/json'); // display result JSON format
echo json_encode(array(
'success' => true,
'data' => $data // this is your data variable
));
/////////////////////////////////
<?php
include_once './db_connect.php';
function getProducts(){
$db = new DB_CONNECT();
// array for json response
$response = array();
$response["products"] = array();
// Mysql select query
// check for post data
if (isset($_GET["ProductGroup"])) {
$selected_group = $_GET['ProductGroup'];
}
$result = mysql_query("SELECT * FROM Product WHERE ProductType= $selected_group")or die(mysql_error());
while($row = mysql_fetch_array($result)){
// temporary array to create single category
$tmp = array();
$tmp["id"] = $row["ProductID"];
$tmp["name"] = $row["ProductName"];
$tmp["type"] = $row["ProductType"];
$tmp["image"] = $row["ProductImage"];
$tmp["des"] = $row["ProductDescription"];
// push category to final json array
array_push($response["products"], $tmp);
}
// keeping response header to json
header('Content-Type: application/json');
// echoing json result
echo json_encode($response);
}
getProducts();
?>
////////////////////////////////
<?php
include_once './db_connect.php';
function getProducts(){
$db = new DB_CONNECT();
// array for json response
$response = array();
$response["products"] = array();
// Mysql select query
// check for post data
if (isset($_GET["ProductGroup"])) {
$selected_group = $_GET['ProductGroup'];
}
$result = mysql_query("SELECT * FROM Product WHERE ProductType= '".mysql_real_escape_string($selected_group)."'")or die(mysql_error());
while($row = mysql_fetch_array($result)){
// temporary array to create single category
$tmp = array();
$tmp["id"] = $row["ProductID"];
$tmp["name"] = $row["ProductName"];
$tmp["type"] = $row["ProductType"];
$tmp["image"] = $row["ProductImage"];
$tmp["des"] = $row["ProductDescription"];
// push category to final json array
array_push($response["products"], $tmp);
}
// keeping response header to json
header('Content-Type: application/json');
// echoing json result
echo json_encode($response);
}
getProducts();
?>
/////////////////////
$sql = 'select
employee_id,
first_name,
last_name
from employees
';
$stmt = $pdo->prepare($sql);
$stmt->execute();
$data = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$data[] = $row;
}
$response = [];
$response['data'] = $data;
echo json_encode($response, JSON_PRETTY_PRINT);
} catch (PDOException $e) {
echo 'Database error. ' . $e->getMessage();
}
https://www.alibabacloud.com/blog/how-to-return-mysql-data-in-json-format-with-php-on-ubuntu-20-04-server_598024
////////////////////////////
while ($row = $users_stmt->fetch(PDO::FETCH_ASSOC)){
$dataarray['data'][] = array(
$row['username'], $row['fullname'],
$row['email'], $row['siteright'],
2011/04/25, $320,800
);
}
$json_data = json_encode($newData,JSON_PRETTY_PRINT);
echo $json_data;
//////////////////////////////////////
<?php
include "config.php";
$return_arr = array();
$query = "SELECT * FROM users ORDER BY NAME";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)){
$id = $row['id'];
$username = $row['username'];
$name = $row['name'];
$email = $row['email'];
$return_arr[] = array("id" => $id,
"username" => $username,
"name" => $name,
"email" => $email);
}
// Encoding array in JSON format
echo json_encode($return_arr);
$(document).ready(function(){
$.ajax({
url: 'ajaxfile.php',
type: 'get',
dataType: 'JSON',
success: function(response){
var len = response.length;
for(var i=0; i<len; i++){
var id = response[i].id;
var username = response[i].username;
var name = response[i].name;
var email = response[i].email;
var tr_str = "<tr>" +
"<td align='center'>" + (i+1) + "</td>" +
"<td align='center'>" + username + "</td>" +
"<td align='center'>" + name + "</td>" +
"<td align='center'>" + email + "</td>" +
"</tr>";
$("#userTable tbody").append(tr_str);
}
}
});
});
<div class="container">
<table id="userTable" border="1" >
<thead>
<tr>
<th width="5%">S.no</th>
<th width="20%">Username</th>
<th width="20%">Name</th>
<th width="30%">Email</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
Comentarios
Publicar un comentario