1

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 ']}}}';
1
  • 2
    Dont build JSON Strings manually. Create objects and arrays in the form you want the data and then use json_encode($data); Commented Apr 5, 2016 at 14:21

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

6 Comments

thank you so much but for bank paymentmethods":{"Bank":{"accname":"0506796049","country":"DE690240002520511717801","Iban":"ARABIC BANK NAME","Bankname":"ARABIC BANK NAME"} it is comming like this what i need is "Bank": [{ "accname": "Zakat Acc", "accnameEn": "حساب الزكــــــاة", "country": "UAE", "acc": "0034430430012" }, { "accname": "Zakat Acc", "accnameEn": "حساب الزكــــــاة", "country": "UAE", "acc": "00344304332412" }]
you want array for the "Bank"=[] or you want it as object "Bank"={} ??
thank you for your reply..i want it as "Bank": [{ }]
If you want "Bank" as array then simply convert it as array! Check my updated anwser
thank you :)can you please tel me how to do that for sms also :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.