Reputation: 69
Hello I need to create a json encode api for the code which i am building
Currently my project have 3 tables item table,payment table and sms table
item table contains the details of my project and the item table is linked to payment and sms table
i am listing the json form which i want to make
{
"Data": {
"Projects": [{
"id": "10023",
"Info": "Arabic project info ",
"paymentMethods": {
"Bank": [{
"accname": "Zakat Acc",
"accnameEn": "حساب الزكــــــاة",
"country": "UAE",
"acc": "0034430430012"
}, {
"accname": "Zakat Acc",
"accnameEn": "حساب الزكــــــاة",
"country": "UAE",
"acc": "00344304332412"
}],
"SMS": [{
"operatorEn": "etisalat",
"shortcode": "4236",
"operator": "إتصالات"
}, {
"operatorEn": "etisalat",
"shortcode": "4346",
"operator": "إتصالات"
}],
"CC": {
"-URL":
"http://www.sharjahcharuty.org/donations/"
}
}
}, {
"id": "10026",
"Info": "Arabic project info ",
"InfoEn": "project info in english",
"paymentMethods": {
"Bank": [{
"accname": "Zakat Acc",
"accnameEn": "حساب الزكــــــاة",
"country": "UAE",
"acc": "0034430430012"
}, {
"accname": "Zakat Acc",
"accnameEn": "حساب الزكــــــاة",
"country": "UAE",
"acc": "00344304332412"
}],
"SMS": [{
"operatorEn": "etisalat",
"shortcode": "4236",
"operator": "إتصالات"
}, {
"operatorEn": "etisalat",
"shortcode": "4346",
"operator": "إتصالات"
}],
"CC": {
"-URL": "http://www.sharjha.org/donations/"
}
}
}]
}
}
I have created php code for the system But this code i have create using brackets and seperated by queries but this system not giving the result what i expected
echo $string='{"Data":{';
//echo $string='"Projects":';
$sql2 = "SELECT * FROM wg_items where cat_id=6007;";
$query = $this->db->query($sql2);
echo $string='"Projects":';
echo $string='[';
$intcount=0;
$scateg="";
if ($query->num_rows() > 0) {
foreach ($query->result() as $row){
//$jsonrows[]=array("id"=>$row->cat_id,"name"=>$row->item_name);
echo $string='{';
echo $string = 'id:'.$row->item_id;
echo $string=',';
echo $string = 'name:'.$row->item_name;
echo $string=',';
//-------------------------------------------------------------//
$hasComma = false;
echo $string='"paymentmethods":';
echo $string='"Bank":[';
$sql2 = "SELECT * FROM paymentmethods where cid=587 ";
$query = $this->db->query($sql2);
foreach ($query->result() as $row){
echo '{';
echo $string = 'accname:'.$row->acc.',' ;
echo $string = 'country:'.$row->IBAN.',' ;
echo $string = 'Iban:'.$row->Bankname.',' ;
echo $string = 'Bankname:'.$row->Bankname.',' ;
echo $string = '},';
}
echo $string = '],';
echo $string='"SMS":[';
$sql3 = "SELECT * FROM sms where cid=537 ";
$query = $this->db->query($sql3);
$hasComma = false;
foreach ($query->result() as $rows){
echo '{';
echo 'Operator:'.$rows->operator.',' ;
echo 'shortcode:'.$rows->shortcode.',';
echo 'keyword:'.$rows->keyword.',';
echo 'price:'.$rows->price;
echo $string = '},';
if($hasComma = TRUE)
{
}
$hasComma = TRUE;
}
echo $string = '}],';
echo $string='"CC":{';
echo $string3='"-URL:"';
echo $string3='"HTTP:SHARJAHCHARTITY.COM"';
ECHO '}}}';
echo ',';
}
echo ']}}}';
Upvotes: 1
Views: 682
Reputation: 3816
You are not suppose to append strings to create a json string. Create an array which holds the data and wrap it with json_encode() function For ex:
<?php
$array_data=[];
$array_data['firstname']="Rafique";
$array_data['lastname']="Mohammed";
$array_data['email']="[email protected]";
// or any data which you want in json
$json_output=json_encode($array_data);
echo $json_output;
OUTPUT :
{"firstname":"Rafique","lastname":"Mohammed","email":"[email protected]"}
UPDATE 2 :
In your case
<?php
//.. your code
if ($query->num_rows() > 0) {
foreach ($query->result() as $row){
$jsonrows=array("id"=>$row->item_id,"name"=>$row->item_name);
$jsonrows["paymentmethods"]=array("Bank"=>[]);
$sql2 = "SELECT * FROM paymentmethods where cid=587 ";
$query = $this->db->query($sql2);
foreach ($query->result() as $row){
//convert bank as array
$jsonrows["paymentmethods"]["Bank"][] = array(
"accname"=>$row->acc,
"country"=>$row->IBAN,
"Iban"=>$row->Bankname,
"Bankname"=>$row->Bankname );
}
//DO IT FOR REST OF THE CODE
Upvotes: 3