2016-03-19 170 views
-1

我需要PHP代碼將數據保存到JSON文件。如何使用MySQL數據庫在PHP中創建一個JSON arrray數據庫

所需結果: 「日曆」:[{ 「Pariharam」: 「pariharam」, 「Palamozhi」: 「palamozhi」, 「八字」:[ { 「HoroscopeID」: 「daily_horoscope_id」, 「Rasiname」: 「拉西」, 「SHORTDESCRIPTION」: 「shot_desc」, 「LongDescription」: 「long_desc」 }, } ]

我的代碼:

$sql=mysql_query("select * from daily_calendar_tbl"); 
$response = array(); 
$posts = array(); 
//$result=mysql_query($sql); 
while($row=mysql_fetch_array($sql)) 
{ 
$caldate=$row['cal_date']; 
$tamilyear=$row['tamil_year']; 
$tamilmonth=$row['tamil_month']; 
$tamildate=$row['tamil_date']; 
$gtms=$row['gt_mrg_str']; 
$gtme=$row['gt_mrg_end']; 
$gtes=$row['gt_eve_str']; 
$gtee=$row['gt_eve_end']; 
$ggtms=$row['gow_mrg_str']; 
$ggtme=$row['gow_mrg_end']; 
$ggtes=$row['gow_eve_str']; 
$ggtee=$row['gow_eve_end']; 
$rst=$row['ragu_str']; 
$ret=$row['ragu_end']; 
$gst=$row['guli_str']; 
$get=$row['guli_end']; 
$yst=$row['yema_str']; 
$yet=$row['yema_end']; 
$sut=$row['sur_udha']; 
$suras=$row['sur_astha']; 
$yogam=$row['yogam']; 
$chantrs=$row['chandrashtama']; 
$soolam=$row['soolam']; 
$phariharam=$row['pariharam']; 
$palamozhi=$row['palamozhi']; 


$selrasi=mysql_query("select * from daily_horoscope_tbl where horoscope_date='$caldate'"); 

while($rasidata=mysql_fetch_array($selrasi)){ 
    $horoid=$rasidata["daily_horoscope_id"]; 
    $rasiname=$rasidata["rasi"]; 
    $shortdesc=$rasidata["shot_desc "]; 
    $long_desc=$rasidata["long_desc"]; 
    $horoscopedata[]=array('HoroscopeID'=> $horoid, 'Rasiname'=> $rasiname, 'ShortDescription'=>$shortdesc,'LongDescription'=> $long_desc); 
} 

$horoscopesres=$horoscopedata; 

$posts[] = array('CalendarDate'=> $caldate, 'TamilYear'=> $tamilyear, 'TamilMonth'=>$tamilmonth,'TamilDate'=> $tamildate, 'GoodTimeMorningStart'=> $gtms, 
'GoodTimeMorningEnd'=>$gtme,'GoodTimeEveningStart'=> $gtes, 'GoodTimeEveningEnd'=> $gtee, 'GowriGoodTimeMorningStart'=>$ggtms,'GowriGoodTimeMorningEnd'=> $ggtme, 'GowriGoodTimeEveningStart'=> $ggtes, 
'GowriGoodTimeEveningEnd'=>$ggtee,'RahuStartTime'=> $rst, 'RahuEndTime'=> $ret, 'GulikaiStartTime'=>$gst,'GulikaiEndTime'=> $get, 'YamagandamStartTime'=> $yst, 
'YamagandamEndTime'=>$yet,'SuryaUdhayamTime'=> $sut, 'SuryaAsthamanam'=> $suras, 'Yogam'=>$yogam,'Chandrashtama'=> $chantrs, 'Soolam'=> $soolam, 'Pariharam'=>$phariharam,'Palamozhi'=>$palamozhi,'horoscopes'=>$horoscopesres); 

} 

$response['calendardata'] = $posts; 

$fp = fopen('articles.json', 'w'); 
fwrite($fp, json_encode($response)); 
fclose($fp); 
+0

你可以通過數組變量'json_encode()的數組轉換成JSON'函數 –

+1

請提供你的代碼已經嘗試了,使我們可以幫助您更好。 – gmuraleekrishna

回答

0

您的JSON甚至沒有一個有效的JSON試試這個

{ 
    "calendar": { 
     "Pariharam": "pariharam", 
     "Palamozhi": "palamozhi", 
     "horoscopes": [{ 
      "HoroscopeID": "daily_horoscope_id", 
      "Rasiname": "Rasi", 
      "ShortDescription": "shot_desc", 
      "LongDescription": "long_desc" 
     }] 
    } 
} 

其次你想去哪兒,如果你想八字比它看起來像

{ 
    "calendar": { 
     "Pariharam": "pariharam", 
     "Palamozhi": "palamozhi", 
     "horoscopes": [{ 
      "HoroscopeID": "daily_horoscope_id", 
      "Rasiname": "Rasi", 
      "ShortDescription": "shot_desc", 
      "LongDescription": "long_desc" 
     }, { 
      "HoroscopeID": "daily_horoscope_id", 
      "Rasiname": "Another", 
      "ShortDescription": "shot_desc", 
      "LongDescription": "long_desc" 
     }] 
    } 
} 
0

東西數組傳遞數組像這樣會適合你.. 你可能需要爲其他動態元素做foreach循環..你可以從這裏獲得一個想法。如果你發佈你的其他代碼,我可以更新我的答案。

$Pariharam = "pariharam"; 
    $Palamozhi="palamozhi"; 
    $horoscope= array('HoroscopeID'=>'daily_horoscope_id','Rasiname'=>'Rasi','ShortDescription'=>'shot_desc','LongDescription'=>'long_desc'); 

    $calender=array(); 


     $calender['calender']['Pariharam']=$Pariharam; 
     $calender['calender']['Palamozhi']=$Pariharam; 
     $calender['calender']['horoscope']=$horoscope; 



    print_r(json_encode($calender));exit; 

輸出

{ 
    "calender":{ 
     "Pariharam":"pariharam", 
     "Palamozhi":"pariharam", 
     "horoscope":{ 
     "HoroscopeID":"daily_horoscope_id", 
     "Rasiname":"Rasi", 
     "ShortDescription":"shot_desc", 
     "LongDescription":"long_desc" 
     } 
    } 
} 
相關問題