I have the following code:
include "config.php";
error_reporting(0);
$idSuccess = 0;
if(isset($_POST['userName']))
{
$usr = $_POST['userName'];
$pwd = $_POST['userPwd'];
$sqlQ = "SELECT * FROM dr_users where dr_user_name='$usr' AND dr_user_pwd='$pwd' LIMIT 1";
$q = mysql_query($sqlQ);
if(mysql_num_rows($q) > 0)
{
$userO = mysql_fetch_assoc($q);
$userData = array('userID' => $userO['dr_user_id'], 'userName' => $userO['dr_user_name'], 'userPwd' => $userO['dr_user_pwd'], 'userEml' => $userO['dr_user_email'], 'privLvl' => $userO['dr_user_priv_level']);
$idSuccess = 1;
$uID = $userO['dr_user_id'];
$charQ = mysql_query("SELECT * FROM dr_chars WHERE dr_user_id='$uID'");
while($char = mysql_fetch_array($charQ))
{
$chars[] = $char;
$charID = $char['dr_char_id'];
$itemQ = mysql_query("SELECT * FROM dr_items WHERE dr_char_id='$charID'");
while($item = mysql_fetch_array($itemQ))
{
$itemData[] = array('itemID' => $item['dr_item_id'], 'itemName' => $item['dr_item_name'], 'itemDesc' => $item['dr_item_desc'], 'itemType' => $item['dr_item_type'], 'itemCost' => $item['dr_item_cost'], 'itemCurType' => $item['dr_item_cur_type'], 'itemAtk' => $item['dr_item_stat_atk'], 'itemDef' => $item['dr_item_stat_def'], 'itemEnd' => $item['dr_item_stat_end'], 'itemLuck' => $item['dr_item_stat_luck'], 'itemFileURL' => $item['dr_item_file_url'], 'itemStaff' => $item['dr_item_staff'], 'itemEquipped' => $item['dr_char_eqp'], 'spX' => $item['dr_static_pos_x'], 'spY' => $item['dr_static_pos_y']);
}
$charData[] = array('charID' => $char['dr_char_id'], 'charName' => $char['dr_char_name'], 'charRace' => $char['dr_char_race'], 'charLvl' => $char['dr_char_lvl'], 'charItems' => $itemData);
}
$resData = array('idSuccess' => $idSuccess, 'user' => $userData, 'chars' => $charData);
} else {
$resData = array('idSuccess' => $idSuccess);
}
echo json_encode($resData);
}
?>
I have successfully loaded character data from the table 'dr_chars' in my database. I'm trying to load the corresponding item data from the retrieved character ID and push it into the $charData array. So I can then easily encode & output it in a JSON format.
If my question isn't clear just ask and I'll try to explain the situation better. The JSON data is being outputted into flash for dynamic use.
I managed to fix this final code as follows:
<?php
include "config.php";
error_reporting(0);
$idSuccess = 0;
if(isset($_POST['userName']))
{
$usr = $_POST['userName'];
$pwd = $_POST['userPwd'];
$sqlQ = "SELECT * FROM dr_users where dr_user_name='$usr' AND dr_user_pwd='$pwd' LIMIT 1";
$q = mysql_query($sqlQ);
if(mysql_num_rows($q) > 0)
{
$userO = mysql_fetch_assoc($q);
$userData = array('userID' => $userO['dr_user_id'], 'userName' => $userO['dr_user_name'], 'userPwd' => $userO['dr_user_pwd'], 'userEml' => $userO['dr_user_email'], 'privLvl' => $userO['dr_user_priv_level']);
$idSuccess = 1;
$uID = $userO['dr_user_id'];
$charQ = mysql_query("SELECT * FROM dr_chars WHERE dr_user_id='$uID'");
$chars = array();
while($char = mysql_fetch_array($charQ))
{
$charID = $char['dr_char_id'];
$itemQ = mysql_query("SELECT * FROM dr_items WHERE dr_char_id='$charID'");
$items = array();
while($item = mysql_fetch_array($itemQ))
{
$items[] = array('itemID' => $item['dr_item_id'], 'itemName' => $item['dr_item_name'], 'itemDesc' => $item['dr_item_desc'], 'itemType' => $item['dr_item_type'], 'itemCost' => $item['dr_item_cost'], 'itemCurType' => $item['dr_item_cur_type'], 'itemAtk' => $item['dr_item_stat_atk'], 'itemDef' => $item['dr_item_stat_def'], 'itemEnd' => $item['dr_item_stat_end'], 'itemLuck' => $item['dr_item_stat_luck'], 'itemFileURL' => $item['dr_item_file_url'], 'itemStaff' => $item['dr_item_staff'], 'itemEquipped' => $item['dr_char_eqp'], 'spX' => $item['dr_static_pos_x'], 'spY' => $item['dr_static_pos_y']);
}
$chars[] = array('charID' => $char['dr_char_id'], 'charName' => $char['dr_char_name'], 'charRace' => $char['dr_char_race'], 'charLvl' => $char['dr_char_lvl'], 'charItems' => $items);
}
$resData = array('idSuccess' => $idSuccess, 'user' => $userData, 'chars' => $chars);
} else {
$resData = array('idSuccess' => $idSuccess);
}
echo json_encode($resData);
}
?>
Related
I have source code like this:
elseif($postjson['aksi']=='load_Kelas'){
$data = array();
$query = mysqli_query($mysqli, "SELECT * FROM t_kelas_kuliah");
while($row = mysqli_fetch_array($query)){
$data[] = array(
'kd_kelas' => $row['kd_kelas'],
'kd_matkul' => $row['kd_matkul'],
'kd_dosen' => $row['kd_dosen'],
'waktu' => $row['waktu'],
'ruang' => $row['ruang'],
'hari' => $row['hari'],
);
}
if($query) $result = json_encode(array('success'=>true, 'result'=>$data));
else $result = json_encode(array('success'=>false));
echo $result;
}
in ionic code
async loadKelas() {
return new Promise(resolve => {
let body = {
aksi: 'load_Kelas',
};
this.accsPrvds.postData(body, 'proses_api.php').subscribe((kelas: any) => {
for (let kelaskuliah of kelas.result) {
this.datakelas.push(kelaskuliah);
}
resolve(true);
console.log(kelas);
});
});
}
result console is
result: (2) […]
0: {…}
hari: "Senin"
kd_dosen: "SPK"
kd_kelas: "A1157"
kd_matkul: "IFKK1051"
ruang: "B310"
waktu: "09.30 - 12.00"
<prototype>: Object { … }
1: {}
hari: "Senin"
kd_dosen: "SPK"
kd_kelas: "A1158"
kd_matkul: "IFKK1051"
ruang: "B310"
waktu: "12.00 - 14.30"
how to create array by category 'Hari' then I have output like this
Data = [
{
Hari : "Senin",
Kuliah = [
{ kd_dosen: "SPK", kd_kelas: "A1157", kd_matkul: "IFKK1051", ruang: "B310", waktu: "09.30 - 12.00" },
{ kd_dosen: "SPK", kd_kelas: "A1157", kd_matkul: "IFKK1051", ruang: "B310", waktu: "09.30 - 12.00" }
]}
]}
Try to replace:
elseif($postjson['aksi']=='load_Kelas'){
$data = array();
$query = mysqli_query($mysqli, "SELECT * FROM t_kelas_kuliah");
while($row = mysqli_fetch_array($query)){
$data[] = array(
'kd_kelas' => $row['kd_kelas'],
'kd_matkul' => $row['kd_matkul'],
'kd_dosen' => $row['kd_dosen'],
'waktu' => $row['waktu'],
'ruang' => $row['ruang'],
'hari' => $row['hari'],
);
}
if($query) $result = json_encode(array('success'=>true, 'result'=>$data));
else $result = json_encode(array('success'=>false));
echo $result;
}
with this:
elseif($postjson['aksi']=='load_Kelas'){
$temp = array();
$data = array();
$query = mysqli_query($mysqli, "SELECT * FROM t_kelas_kuliah");
while($row = mysqli_fetch_array($query)){
$temp[$row['hari']][] = array(
'kd_kelas' => $row['kd_kelas'],
'kd_matkul' => $row['kd_matkul'],
'kd_dosen' => $row['kd_dosen'],
'waktu' => $row['waktu'],
'ruang' => $row['ruang'],
'hari' => $row['hari'],
);
};
foreach($temp as $key => $val){
$data[] = array(
"Hari" => $key,
"Kuliah" => $val
);
};
if($query) $result = json_encode(array('success'=>true, 'result'=>$data));
else $result = json_encode(array('success'=>false));
echo $result;
}
I am retrieving 5000 plus data in my MYSQL database. I takes 5-10 minutes to retrieve the data (it is only just local slower when over the network) is there a way to improve the speed without using a plugin?
$ContactID = $_GET["Contact"];
$sql = "SELECT * FROM tblContacts WHERE Coordinator = '$ContactID'";
$result = mysqli_query($conn, $sql);
$count = mysqli_num_rows($result);
if($count > 0){
while ($row = mysqli_fetch_array($result)) {
$supdate = date("Y-m-d h:i", strtotime($row['ServerUpdate']));
$mupdate = date("Y-m-d h:i", strtotime($row['MobileUpdate']));
$ar[] = array(
'ContactID' => $row['ContactID'],
'FileAs' => $row['FileAs'],
'FirstName' => $row['FirstName'],
'MiddleName' => $row['MiddleName'],
'LastName' => $row['LastName'],
'Position' => $row['Position'],
'Company' => $row['Company'],
'CompanyID' => $row['CompanyID'],
'ContactType' => $row['ContactType'],
'RetailerType' => $row['RetailerType'],
'PresStreet' => $row['PresStreet'],
'PresBarangay' => $row['PresBarangay'],
'PresDistrict' => $row['PresDistrict'],
'PresTown' => $row['PresTown'],
'PresProvince' => $row['PresProvince'],
'PresCountry' => $row['PresCountry'],
'Landmark' => $row['Landmark'],
'Telephone1' => $row['Telephone1'],
'Telephone2' => $row['Telephone2'],
'Mobile' => $row['Mobile'],
'Email' => $row['Email'],
'Employee' => $row['Employee'],
'Customer' => $row['Customer'],
'Coordinator' => $row['Coordinator'],
'ServerUpdate' => $supdate,
'MobileUpdate' => $mupdate
);
}
print json_encode($ar);
}
$ContactID = $_GET["Contact"];
$ar = array();
$sql = "SELECT ContactID,FileAs, FirstName ,MiddleName ,LastName ,Position ,Company ,CompanyID ,ContactType ,RetailerType ,PresStreet
,PresBarangay ,PresDistrict ,PresTown , PresProvince ,PresCountry ,Landmark ,Telephone1 , Telephone2 ,Mobile
,Email ,Employee ,Customer ,Coordinator FROM tblContacts WHERE Coordinator = '$ContactID'";
$result = mysqli_query($conn, $sql);
$count = mysqli_num_rows($result);
if($count > 0){
$rowCount = 0;
while ($row = mysqli_fetch_array($result)) {
$supdate = date("Y-m-d h:i", strtotime($row['ServerUpdate']));
$mupdate = date("Y-m-d h:i", strtotime($row['MobileUpdate']));
$ar[$rowCount] = $row;
$ar[$rowCount]['ServerUpdate'] = $supdate;
$ar[$rowCount]['ServerUpdate'] = $mupdate;
$rowCount++;
}
print json_encode($ar);
}
You must declare the variable of array first in the top.
You can directly call from mysql what data needed. Hope can help.
It could be like this way.
$contactID = $_GET["Contact"];
$sql = "SELECT * FROM tblContacts WHERE Coordinator = '$contactID'";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0){
$data = array();
while($row = mysqli_fetch_array($result)){
$row['supdate'] = date("Y-m-d h:i", strtotime($row['ServerUpdate']));
$row['supdate'] = date("Y-m-d h:i", strtotime($row['MobileUpdate']));
$data[] = $row;
}
print json_encode($data);
}
updated for multiple records
I'm working on a php web application for a website, and I would like to pass some variables from one php file to another. I have tried the old fashion way with the include file but was not successful. I also tried to set the variables in global scope but still not working. The code in first.php file is:
function rc_getAvailableVehicles($pickup_timestamp, $return_timestamp, $vehicle_classes=array()) {
global
$wpdb;
$rc_currency = RC_Registry::get('rc_currency');
$where_classes = "";
if ($vehicle_classes) {
foreach($vehicle_classes as $vehicle_class) {
$where_classes[] = " v.class = '". $wpdb->escape($vehicle_class) ."'";
}
$where_classes = "AND (".implode(' OR ', $where_classes).") ";
}
$sql = "SELECT *
FROM ".$wpdb->rc_vehicles." v
WHERE v.quantity > (SELECT COUNT(*) FROM ".$wpdb->rc_bookings." b WHERE b.vehicle_id = v.vehicle_id AND ((UNIX_TIMESTAMP(b.pickup_date) >= '". (int)$pickup_timestamp ."' AND UNIX_TIMESTAMP(b.pickup_date) < '". (int)$return_timestamp ."') || (UNIX_TIMESTAMP(b.pickup_date) < '". (int)$pickup_timestamp ."' AND UNIX_TIMESTAMP(b.return_date) >= '". (int)$pickup_timestamp ."')) AND (b.status != 'new' AND b.status != 'canceled') AND b.trash = '0') ".$where_classes."AND v.status = '1' AND v.archive = '0'
ORDER BY v.rent ASC";
$results = $wpdb->get_results($sql,ARRAY_A);
$vehicles = array();
$rental_days = ceil(($return_timestamp-$pickup_timestamp)/91800);
$xfee = get_field('another_location_fee');
$xfee2 = get_field('return_to_another_location_fee_2');
if ($results) {
foreach ($results as $result) {
if ($result['image'] && file_exists(RC_UPLOADS_DIR . "vehicle_" . $result['image'])) {
$image = RC_UPLOADS_URL . 'vehicle_' . $result['image'];
$image_thumb = RC_UPLOADS_URL . 'cache/' . rc_image_resize(RC_UPLOADS_DIR . "vehicle_" . $result['image'], 220, 160);
} else {
$image = '';
$image_thumb = '';
}
$result['description'] = do_shortcode($result['description']);
$description = html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8');
global $GeneralPrice, $discount_price, $rent1, $rent5;
include_once ('file2.php');
//$GeneralPrice = $result['rent'];
$rent1 = $vehicle_meta['rent1'];
$rent2 = $vehicle_meta['rent2'];
$rent3 = $vehicle_meta['rent3'];
$rent4 = $vehicle_meta['rent4'];
$rent5 = $vehicle_meta['rent5'];
$rent6 = $vehicle_meta['rent6'];
$rent7 = $vehicle_meta['rent7'];
$rent8 = $vehicle_meta['rent8'];
$rent9 = $vehicle_meta['rent9'];
$rent10 = $vehicle_meta['rent10'];
$rent11 = $vehicle_meta['rent11'];
$rent12 = $vehicle_meta['rent12'];
$rent13 = $vehicle_meta['rent13'];
$rent14 = $vehicle_meta['rent14'];
$rent15 = $vehicle_meta['rent15'];
$rent16 = $vehicle_meta['rent16'];
$rent17 = $vehicle_meta['rent17'];
$rent18 = $vehicle_meta['rent18'];
$rent19 = $vehicle_meta['rent19'];
$rent20 = $vehicle_meta['rent20'];
$rent21 = $vehicle_meta['rent21'];
$rent22 = $vehicle_meta['rent22'];
$rent23 = $vehicle_meta['rent23'];
$rent24 = $vehicle_meta['rent24'];
$rent25 = $vehicle_meta['rent25'];
$rent26 = $vehicle_meta['rent26'];
$rent27 = $vehicle_meta['rent27'];
$rent28 = $vehicle_meta['rent28'];
$rent29 = $vehicle_meta['rent29'];
$rent30 = $vehicle_meta['rent30'];
$rent31 = $vehicle_meta['rent31'];
$rent32 = $vehicle_meta['rent32'];
$rent33 = $vehicle_meta['rent33'];
$rent34 = $vehicle_meta['rent34'];
$rent35 = $vehicle_meta['rent35'];
$rent36 = $vehicle_meta['rent36'];
$vehicle_meta = rc_getVehicleMeta($result['vehicle_id']);
$vehicles[$result['vehicle_id']] = array(
'vehicle_id' => $result['vehicle_id'],
'title' => $result['manufacturer']." ".$result['series'],
'manufacturer' => $result['manufacturer'],
'series' => $result['series'],
'year' => $result['year'],
'class' => $result['class'],
'image' => $image,
'image_thumb' => $image_thumb,
'description' => $description,
'seats' => $vehicle_meta['seats'],
'doors' => $vehicle_meta['doors'],
'baggages' => $vehicle_meta['baggages'],
'conditioning' => $vehicle_meta['conditioning'],
'transmission' => $vehicle_meta['transmission'],
'rent1' => $vehicle_meta['rent1'],
'rent2' => $vehicle_meta['rent2'],
'rent3' => $vehicle_meta['rent3'],
'rent4' => $vehicle_meta['rent4'],
'rent5' => $vehicle_meta['rent5'],
'rent6' => $vehicle_meta['rent6'],
'rent7' => $vehicle_meta['rent7'],
'rent8' => $vehicle_meta['rent8'],
'rent9' => $vehicle_meta['rent9'],
'rent10' => $vehicle_meta['rent10'],
'rent11' => $vehicle_meta['rent11'],
'rent12' => $vehicle_meta['rent12'],
'rent13' => $vehicle_meta['rent13'],
'rent14' => $vehicle_meta['rent14'],
'rent15' => $vehicle_meta['rent15'],
'rent16' => $vehicle_meta['rent16'],
'rent17' => $vehicle_meta['rent17'],
'rent18' => $vehicle_meta['rent18'],
'rent19' => $vehicle_meta['rent19'],
'rent20' => $vehicle_meta['rent20'],
'rent21' => $vehicle_meta['rent21'],
'rent22' => $vehicle_meta['rent22'],
'rent23' => $vehicle_meta['rent23'],
'rent24' => $vehicle_meta['rent24'],
'rent25' => $vehicle_meta['rent25'],
'rent26' => $vehicle_meta['rent26'],
'rent27' => $vehicle_meta['rent27'],
'rent28' => $vehicle_meta['rent28'],
'rent29' => $vehicle_meta['rent29'],
'rent30' => $vehicle_meta['rent30'],
'rent31' => $vehicle_meta['rent31'],
'rent32' => $vehicle_meta['rent32'],
'rent33' => $vehicle_meta['rent33'],
'rent34' => $vehicle_meta['rent34'],
'rent35' => $vehicle_meta['rent35'],
'rent36' => $vehicle_meta['rent36'],
'total_price' => $rc_currency->format((($rental_days*$GeneralPrice)-(($rental_days*$GeneralPrice) * ($discount_price)/100)+ $utime)),
'total_price_return_fee' => $rc_currency->format((($rental_days*$result['rent']) - ($rental_days*$result['rent']) * ($discount_price)/100)+ $xfee),
'total_price_return_fee2' => $rc_currency->format((($rental_days*$result['rent']) - ($rental_days*$result['rent'])*($discount_price)/100)+ $xfee2),
'daily_price' => $rc_currency->format((($GeneralPrice)-($GeneralPrice)*($discount_price)/100)+ ($utime / $rental_days)),
'daily_price_return_fee' => $rc_currency->format(($result['rent']-($result['rent'])* ($discount_price)/100)+ ($xfee / $rental_days)),
'daily_price_return_fee2' => $rc_currency->format(($result['rent']-($result['rent'])*($discount_price)/100) + ($xfee2 / $rental_days))
);
}
}
return $vehicles;
}
and I want to echo all the $rent variables in file2.php
I have tried the
echo $rent; without success!
You have problems with the order of the code
$vehicle_meta = rc_getVehicleMeta($result['vehicle_id']);
Should be BEFORE you populate rent variables, and
include_once ('file2.php');
Should be AFTER you populated rent variables.
You need to call a function in file1.php that exists in file2.php
ex. in file1.php you could call the function return_rent
return_rent($rent);
in file2 then you make the function
function return_rent($var){
echo $rent;
}
$pass = "anything";
include_once ('file2.php');
From file2.php you can access $pass variable.
My code just populates the last array in the array $workshops
my loop function populates array $workshops, like so
while (ECSE_purchase::have_products()) {
ECSE_purchase::the_product();
$prodid = ECSE_purchase_product::get_the_ID();
$workshop_data = ETICKETDATA::get_workshop_details_helper($prodid);
$workshops = array();
if($prodid == 565) {
............
} else {
$workshopsTmp = array();
$workshopsTmp['workshop_barcode'] = $data['workshop_barcode'];
$workshopsTmp['workshop_id'] = ECSE_purchase_product::get_the_ID();
$workshopsTmp['workshop_title'] = ECSE_purchase_product::get_the_name();
$workshopsTmp['workshop_time'] = $workshop_data['time'];
$workshopsTmp['workshop_room'] = $workshop_data['room'];
$workshopsTmp['workshop_num_of_tickets'] = ECSE_purchase_product::get_the_QTY();
$workshops[] = $workshopsTmp;
}
But here i just get the last array and not like this:
$workshops = array(array('workshop_barcode' => '0101010101',
'workshop_id' => '589',
'workshop_title' => 'Swimming',
'workshop_time' => '12:00',
'workshop_room' => 'Room 1',
'workshop_num_of_tickets' => '2'),
array('workshop_barcode' => '03030303003',
'workshop_id' => '568',
'workshop_title' => 'Running',
'workshop_time' => '15:00',
'workshop_room' => 'Room 3',
'workshop_num_of_tickets' => '3'),
array('workshop_barcode' => '0505050505',
'workshop_id' => '570',
'workshop_title' => 'Biking',
'workshop_time' => '16:00',
'workshop_room' => 'Room 2',
'workshop_num_of_tickets' => '2'));
Any pointers appreciated.
regards,
just edit the loop
You keep redefining your initial array. Try putting $workshops array before while loop
$workshops = array();
while (ECSE_purchase::have_products()) {
ECSE_purchase::the_product();
$prodid = ECSE_purchase_product::get_the_ID();
$workshop_data = ETICKETDATA::get_workshop_details_helper($prodid);
if($prodid == 565) {
............
} else {
$workshopsTmp = array();
$workshopsTmp['workshop_barcode'] = $data['workshop_barcode'];
$workshopsTmp['workshop_id'] = ECSE_purchase_product::get_the_ID();
$workshopsTmp['workshop_title'] = ECSE_purchase_product::get_the_name();
$workshopsTmp['workshop_time'] = $workshop_data['time'];
$workshopsTmp['workshop_room'] = $workshop_data['room'];
$workshopsTmp['workshop_num_of_tickets'] = ECSE_purchase_product::get_the_QTY();
$workshops[] = $workshopsTmp;
}
I'm struggling trying to create a three-dimensional array from my DB, and encoding it to JSON.
My DB contains 3 tables, timeline_table, content_table and pic_table. I want the following structure for my JSON:
{"timeline:"{"content":{"pictures:"{}}}}
Here's my current PHP code:
$get = 1;
$results = mysql_query("
SELECT timeline_table.*, content_table.*, pic_table.*
FROM timeline_table
JOIN content_table
ON content_table.tl_ID = timeline_table.tl_ID
JOIN pic_table
ON pic_table.content_ID = content_table.content_ID
WHERE timeline_table.tl_ID = $get
") or die(mysql_error());
while($row = mysql_fetch_assoc($results)){
$timeline['timeline'][] = array(
'tl_ID' => $row['tl_ID'],
'tl_name' => $row['tl_name'],
'tl_date' => $row['tl_date'],
'tl_desc' => $row['tl_desc'],
);
$timeline['timeline']['content'][] = array(
'content_ID' => $row['content_ID'],
'tl_ID' => $row['tl_ID'],
'content_time' => $row['content_time'],
'content_date' => $row['content_date'],
'content_title' => $row['content_title'],
'content_content' => $row['content_content'],
'content_category' => $row['content_category'],
'content_mapLat' => $row['content_mapLat'],
'content_mapLng' => $row['content_mapLng'],
'content_zoomLvl' => $row['content_zoomLvl'],
);
$timeline['timeline']['content']['pictures'][] = array(
'pic_ID' => $row['pic_ID'],
'content_ID' => $row['content_ID'],
'pic_path' => $row['pic_path'],
'pic_desc' => $row['pic_desc'],
'pic_link' => $row['pic_link']
);
}
echo stripslashes(json_encode($timeline));
}
I have also tried with 1 query for each table, and using 3 while loops to fill the array. I believe one query is the better way to go, but please correct me if I'm wrong. This php gives me the following JSON:
{
"timeline":{
"0":{
"tl_ID":"1",
"tl_name":"Tidslinje 1",
"tl_date":"2013-01-16",
"tl_desc":"Test av tl_table"
},
"content":{
"0":{
"content_ID":"1",
"tl_ID":"1",
"content_time":"16:00:00",
"content_date":"2013-01-17",
"content_title":"Test",
"content_content":"Test content number one.",
"content_category":"test",
"content_mapLat":null,
"content_mapLng":null,
"content_zoomLvl":null
},
"pictures":[
{
"pic_ID":"1",
"content_ID":"1",
"pic_path":"http://i.imgur.com/F6RmDFt.jpg",
"pic_desc":"katt",
"pic_link":"http://i.imgur.com/F6RmDFt.jpg"
},
{
"pic_ID":"3",
"content_ID":"3",
"pic_path":"http://i.imgur.com/POum7eK.jpg",
"pic_desc":"seamonster",
"pic_link":"http://i.imgur.com/POum7eK.jpg"
}
]
}
}
}
All pictures regardless of content_ID comes in one array, and if I add more content, contents with ID 2,3 etc comes under the picture array. I want the pictures in arrays under the content_ID they belong to, and the content under the timeline they belong to. I also want the array keys to be "timeline", "content" and "pictures", instead of integers.
Hopefully this is understandable, any help is greatly appreciated!
EDIT: Solved!
$get = 1;
$result = mysql_query("
SELECT t.tl_ID, t.tl_name, t.tl_date, t.tl_desc, c.content_ID, c.content_time, c.content_date, c.content_title, c.content_content, c.content_category, p.pic_ID, p.pic_path, p.pic_desc, p.pic_link
FROM timeline_table t
LEFT JOIN content_table c ON t.tl_ID = c.tl_ID
LEFT JOIN pic_table p ON c.content_ID = p.content_ID
WHERE t.tl_ID = $get
ORDER BY t.tl_ID, c.tl_ID, p.content_ID
") or die(mysql_error());
$jsonData = array();
$tl_ID = 0;
$content_ID = 0;
$timelineIndex = -1;
$contentIndex = -1;
while($row = mysql_fetch_assoc($result)){
if($tl_ID != $row['tl_ID']){
$timelineIndex++;
$contentIndex = -1;
$tl_ID = $row['tl_ID'];
$jsonData[$timelineIndex]['tl_ID'] = $row['tl_ID'];
$jsonData[$timelineIndex]['tl_name'] = $row['tl_name'];
$jsonData[$timelineIndex]['tl_date'] = $row['tl_date'];
$jsonData[$timelineIndex]['tl_desc'] = $row['tl_desc'];
$jsonData[$timelineIndex]['content'] = array();
}
if($content_ID != $row['content_ID']){
$contentIndex++;
$content_ID = $row['content_ID'];
$jsonData[$timelineIndex]['content'][$contentIndex]['content_ID'] = $row['content_ID'];
$jsonData[$timelineIndex]['content'][$contentIndex]['content_time'] = $row['content_time'];
$jsonData[$timelineIndex]['content'][$contentIndex]['content_date'] = $row['content_date'];
$jsonData[$timelineIndex]['content'][$contentIndex]['content_title'] = $row['content_title'];
$jsonData[$timelineIndex]['content'][$contentIndex]['content_content'] = $row['content_content'];
$jsonData[$timelineIndex]['content'][$contentIndex]['content_category'] = $row['content_category'];
$jsonData[$timelineIndex]['content'][$contentIndex]['pictures'] = array();
}
$jsonData[$timelineIndex]['content'][$contentIndex]['pictures'][] = array(
'pic_ID' => $row['pic_ID'],
'pic_path' => $row['pic_path'],
'pic_desc' => $row['pic_desc'],
'pic_link' => $row['pic_link']
);
}
echo stripslashes(json_encode($jsonData));
}
Edited, try this one:
$timeline['timeline'][] = array(
'tl_ID' => $row['tl_ID'],
'tl_name' => $row['tl_name'],
'tl_date' => $row['tl_date'],
'tl_desc' => $row['tl_desc'],
'content' => array(
'content_ID' => $row['content_ID'],
'tl_ID' => $row['tl_ID'],
'content_time' => $row['content_time'],
'content_date' => $row['content_date'],
'content_title' => $row['content_title'],
'content_content' => $row['content_content'],
'content_category' => $row['content_category'],
'content_mapLat' => $row['content_mapLat'],
'content_mapLng' => $row['content_mapLng'],
'content_zoomLvl' => $row['content_zoomLvl'],
'pictures' => array(
'pic_ID' => $row['pic_ID'],
'content_ID' => $row['content_ID'],
'pic_path' => $row['pic_path'],
'pic_desc' => $row['pic_desc'],
'pic_link' => $row['pic_link']
);
);
);