i want this json format in php but i unable to do this.
{
"couresList_PVP": [
{
"pvp_ad_chptr_id": "9",
"pvp_ad_chptr_un_id": "1526249608",
"pvp_ad_chptr_name": "54654",
"offer_chapter_PVP": [
{
"pvp_ad_chptr_offr_id": "4",
"pvp_ad_chptr_un_id": "1526249608",
"pvp_ad_chptr_offr_un_id": "Offer-1526249608"
},
{
"pvp_ad_chptr_offr_id": "3",
"pvp_ad_chptr_un_id": "1526249608",
"pvp_ad_chptr_offr_un_id": "Offer-1526249608"
}]
},]
}
my PHP code is here. how I will do this? I'll get JSON response but different array not in a single with the array in array.
$sql = mysqli_query($this->db,"SELECT * from `pvp_admin_chptr_list` order by pvp_ad_chptr_id desc");
if(mysqli_num_rows($sql) > 0){
while($res=mysqli_fetch_assoc($sql)){
$url=$this->site_url.'images_console/chapter_banner_console/'.$res['pvp_ad_chptr_banner'];
$video=$this->site_url.'video_console/'.$res['pvp_ad_chptr_video_name'];
$images=array('pvp_ad_chptr_banner'=>$url,'pvp_ad_chptr_video_name'=>$video);
$data['couresList_PVP'][]=array_merge($res,$images);
$ofr_sql = mysqli_query($this->db,"SELECT * from `pvp_admin_chptr_offers` where pvp_ad_chptr_un_id='$res[pvp_ad_chptr_un_id]' order by pvp_ad_chptr_offr_id desc");
if(mysqli_num_rows($ofr_sql) > 0){
while($ofr_res=mysqli_fetch_assoc($ofr_sql)){
$data['offer_chapter_PVP'][]=$ofr_res;
}
}else{
$data['offer_chapter_PVP'][]=array("status"=>"No Data FOund");
}
}
//$data2=array_merge($data['couresList_PVP'],$data['offer_chapter_PVP']);
//$data[]=array_push( $data['couresList_PVP'],$data['offer_chapter_PVP']);
//$data=
// If success everything is good send header as "OK" and user details
$this->response($this->json($data), 200);
}
you can use combination of array and then encode it in json. please check below code.
$response = array();
$responseObj = array();
$courseList = array();
$courseArr = array();
while($res=mysqli_fetch_assoc($sql)){
$courseList['pvp_ad_chptr_id'] = $res['id'];
$courseList['pvp_ad_chptr_un_id'] = $res['pvp_ad_chptr_un_id'];
$courseList['pvp_ad_chptr_name'] = $res['pvp_ad_chptr_name'];
$ofr_sql = mysqli_query($this->db,"SELECT * from `pvp_admin_chptr_offers` where pvp_ad_chptr_un_id='$res[pvp_ad_chptr_un_id]' order by pvp_ad_chptr_offr_id desc");
if(mysqli_num_rows($ofr_sql) > 0){
while($ofr_res=mysqli_fetch_assoc($ofr_sql)){
$courseList['offer_chapter_PVP'][]=$ofr_res;
}
}else{
$courseList[$i]['offer_chapter_PVP'][]=array("status"=>"No Data FOund");
}
$courseArr[] = $courseList;
}
$response['couresList_PVP'] = $courseArr;
$responseObj = $response;
echo '<pre>';
echo json_encode($responseObj);
echo '</pre>';
I am trying to display all records using jason in php.
but display all filed with null value.
I'm using postman for testing purpose.
I don't know what is the problem with that code. I getting null value only.
here is my code :
<?php
header('Content-Type: application/json');
$checkFields = "";
$REQUEST = $_SERVER['REQUEST_METHOD'];
if ($REQUEST == "POST")
{
include "DB/db.php";
$userlist = mysql_query("SELECT * FROM reg_services");
if(mysql_num_rows($userlist) > 0)
{
$p = 0;
$ph = array();
while($userlistdata = mysql_fetch_row($userlist))
{
$ph[$p]["UserId"] = $userlistdata['id'];
$ph[$p]["FirstName"] = $userlistdata['fname'];
$ph[$p]["LastName"] = $userlistdata['lname'];
$ph[$p]["Email"] = $userlistdata['email'];
$ph[$p]["Mobile"] = $userlistdata['mobile'];
$ph[$p]["Password"] = $userlistdata['password'];
$p++;
}
$json = array("success" => 1, "All_User_List" => $ph);
$jsonarray = json_encode($json);
}
}
else
{
$json = array("success" => 0, "message" => "Invalid Request Type(Use POST Method)");
$jsonarray = json_encode($json);
}
echo $jsonarray;
?>
please help me if you are know what is the error in code.
just replace this code with old one
$p = 0;
$ph = array();
while($userlistdata = mysql_fetch_array($userlist))
{
$ph[$p] = array();
$ph[$p]["UserId"] = $userlistdata['id'];
$ph[$p]["FirstName"] = $userlistdata['fname'];
$ph[$p]["LastName"] = $userlistdata['lname'];
$ph[$p]["Email"] = $userlistdata['email'];
$ph[$p]["Mobile"] = $userlistdata['mobile'];
$ph[$p]["Password"] = $userlistdata['password'];
$p++;
}
You need to tell PHP about arrays
while($userlistdata = mysql_fetch_row($userlist))
{
$ph[$p] = array(); // let PHP know it is an array
$ph[$p]["UserId"] = $userlistdata['id'];
$ph[$p]["FirstName"] = $userlistdata['fname'];
$ph[$p]["LastName"] = $userlistdata['lname'];
$ph[$p]["Email"] = $userlistdata['email'];
$ph[$p]["Mobile"] = $userlistdata['mobile'];
$ph[$p]["Password"] = $userlistdata['password'];
$p++;
}
just replace this while loop condition with olde one.
while($userlistdata = mysql_fetch_array($userlist))
now it's work
I am using the following PHP code to fetch some data from my database. It contains chats and messages between users in those chats. I want to return the information of both users plus the messages they exchanged. My test data has two chats with ID's 1 and 2. There are two messages, both in chat 1, however for some reason they are returned for both chats 1 and 2. I'm not sure what the problem in my code is.
$response = array();
$myArray = array();
while($row = $user_chats->fetch_array())
{
$myArray["chatId"] = $row["chat_id"];
$myArray["user1_id"] = $row["user1"];
$myArray["user2_id"] = $row["user2"];
$myArray["user1_name"] = $user1_name;
$myArray["user2_name"] = $user2_name;
$myArray["user1_profile_pic"] = $result_user1["profile_pic"];
$myArray["user2_profile_pic"] = $result_user2["profile_pic"];
$messages = array();
$chat_idd = $row["chat_id"];
$chat_messages = mysqli_query($conn,"SELECT * FROM messages WHERE chatID = '$chat_idd' ORDER BY timestamp ASC");
$count = 1;
while($roww = $chat_messages->fetch_array()) {
if ($row["chat_id"] == $roww["chatID"]) {
$messages["message_id"] = $roww["message_id"];
$messages["sender"] = $roww["sender"];
$messages["chatId"] = $roww["chatID"];
$messages["text"] = $roww["text"];
$messages["timestamp"] = $roww["timestamp"];
$myArray["message"][$count] = $messages;
$count = $count + 1;
}
else {
$myArray["message"]= 0;
}
}
$response[] = $myArray;
}
echo json_encode($response);
produces the following response:
[{"chatId":"1","user1_id":"32132132","user2_id":"2121","user1_name":"dwqd",
"user2_name":"dqdwdw","user1_profile_pic":"http:\/\/graph.facebook.com\/dwqwqdqdwdw\/picture?type=large","user2_profile_pic":"WDQdwqwqddqwdqwdq","message":{"1":{"message_id":"24242241","sender":"32132132","chatId":"1","text":"hello i am",
"timestamp":"2016-05-24 17:13:08"},"2":{"message_id":"421421","sender":"32132132",
"chatId":"1","text":"great","timestamp":"2016-05-24 17:15:08"}}},{"chatId":"2","user1_id":"23413524635","user2_id":"32132132","user1_name":false,
"user2_name":"dwqd","user1_profile_pic":
WDQdwqwqddqwdqwdq" ,"user2_profile_pic":"http:\/\/graph.facebook.com\/32132132\/picture?type=large",
"message":{"1":{"message_id":"24242241","sender":"32132132","chatId":"1","text":"hello i am",
"timestamp":"2016-05-24 17:13:08"},"2":{"message_id":"421421","sender":"32132132","chatId":"1",
"text":"great","timestamp":"2016-05-24 17:15:08"}}}]
You need to initialize $myArray at each iteration through the loop, e.g.
while($row = $user_chats->fetch_array()) {
$myArray = array();
I have an array containing several variables from my sql database.
{"gold":"0","silver":"0","bronze":"0","gdp":"12959563902","population":"3205000","country_name":"Albania"}, {"gold":"1","silver":"0","bronze":"0","gdp":"188681000000","population":"35468000","country_name":"Algeria"}
I have an additional variable called $score that uses information from the database to calculate this score. I want to know how I can loop through and add the correct score to each country in the array.
My Original Code:
$row = $res->fetchRow();
$resGold = $row['gold'];
$resSilver = $row['silver'];
$resBronze = $row['bronze'];
$resGdp = $row['gdp'];
$resPopulation = $row['population'];
$resCountry = $row['country_name'];
$gold_score = ($resGold * $gold_value);
$silver_score = ($resSilver * $silver_value);
$bronze_score = ($resBronze * $bronze_value);
if($population == true){
$score = (($gold_score + $silver_score + $bronze_score)/$resPopulation);
}
else if($gdp == true){
$score = (($gold_score + $silver_score + $bronze_score)/$resGdp);
}
$result = $res->fetchAll();
$result[] = array('score' => $score);
echo json_encode($result);
Your code will be something like this:
$json_data = '{"gold":"0","silver":"0","bronze":"0","gdp":"12959563902","population":"3205000","country_name":"Albania"},
{"gold":"1","silver":"0","bronze":"0","gdp":"188681000000","population":"35468000","country_name":"Algeria"}';
$countries_info_new = array();
$countries_info = json_decode($json_data);
foreach($countries_info as $country_info){
$country_info['score'] = Get_country_score($country_info['country_name']);
$countries_info_new[]=$country_info;
}
$new_json_data = json_encode($countries_info_new);
I have a PHP MySQL fetch while loop as shown below in my script:
$result2211 = mysql_query("select * from products where is_config = 'yes' ");
while($row2211 = mysql_fetch_assoc($result2211))
{
$sn = $row2211['sn'];
$allparrentselectq = mysql_query("SELECT * FROM parrentpro where parrentsn = $sn");
while($allparrentselect = mysql_fetch_assoc($allparrentselectq))
{
$childarr = unserialize($allparrentselect['childsn']);
$subpro = '{"catname":"'.$allparrentselect['childname'].'",';
$i = 0;
foreach($childarr as $childarr):
$subpro .= '"Pro'.$i++.'":"'.$childarr.'",';
endforeach;
$subpro1[] = substr($subpro, 0, -1)."}";
$subproa = "[".implode(",",$subpro1)."]";
}
$prodObj2 = new ProductDetails();
$prodObj2->productname = $row2211['productname'];
$prodObj2->price = $row2211['productprice'];
$prodObj2->discount = $row2211['discount'];
$prodObj2->discountprice = $row2211['discountprice'];
$prodObj2->imageURL = $row2211['productimageurl'];
$prodObj2->category = $row2211['productcat'];
$prodObj2->configurablepone = $subproa;
$prodObj2->configurable = 'yes';
array_push($totArr, $prodObj2);
}
In that I have a problem. I get the result as like below (JSON):
[
{
"productname":"Veg.Pizaa",
"price":"350",
"discount":"",
"discountprice":"350",
"imageURL":"http:\/\/farm8.staticflickr.com\/7154\/6694188161_9ee692d854_s.jpg",
"category":"Pizaa",
"configurablepone":[{"catname":"Extra","Pro0":"Extra 25g Cheese","Pro1":"Extra 75g Cheese"},"catname":"Nuts","Pro0":"Almonds","Pro1":"Peanuts","Pro2":"Pistachios"}],
"configurable":"yes"
},
{
"productname":"Core i7 Pc",
"price":"48000",
"discount":"2",
"discountprice":"47040",
"imageURL":"http:\/\/www.4to40.com\/images\/science\/Basic_Computer_Parts\/Computer.jpg",
"category":"Pc",
"configurablepone":[{"catname":"Extra","Pro0":"Extra 25g Cheese","Pro1":"Extra 75g Cheese"},{"catname":"Nuts","Pro0":"Almonds","Pro1":"Peanuts","Pro2":"Pistachios"},{"catname":"Harddisk","Pro0":"Segate 500Gb","Pro1":"Samsung 250Gb"},{"catname":"Ram","Pro0":"8Gb Ram","Pro1":"4Gb Ram","Pro2":"2Gb Ram"}],
"configurable":"yes"
}
]
But I need the result as like below:
[
{
"productname":"Veg.Pizaa",
"price":"350",
"discount":"",
"discountprice":"350",
"imageURL":"http:\/\/farm8.staticflickr.com\/7154\/6694188161_9ee692d854_s.jpg",
"category":"Pizaa",
"configurablepone":[{"catname":"Extra","Pro0":"Extra 25g Cheese","Pro1":"Extra 75g Cheese"},"catname":"Nuts","Pro0":"Almonds","Pro1":"Peanuts","Pro2":"Pistachios"}],
"configurable":"yes"
},
{
"productname":"Core i7 Pc",
"price":"48000",
"discount":"2",
"discountprice":"47040",
"imageURL":"http:\/\/www.4to40.com\/images\/science\/Basic_Computer_Parts\/Computer.jpg",
"category":"Pc",
"configurablepone":[{"catname":"Harddisk","Pro0":"Segate 500Gb","Pro1":"Samsung 250Gb"},{"catname":"Ram","Pro0":"8Gb Ram","Pro1":"4Gb Ram","Pro2":"2Gb Ram"}],
"configurable":"yes"
}
]
As you can see configurablepone JSON is getting repeated for every loop so I am getting the value of the first product in second product also but I need to seperate like below:
First Product As Like Below
"configurablepone":[{"catname":"Extra","Pro0":"Extra 25g Cheese","Pro1":"Extra 75g Cheese"},{"catname":"Nuts","Pro0":"Almonds","Pro1":"Peanuts","Pro2":"Pistachios"}]
Second Product As Like Below
"configurablepone":[{"catname":"Harddisk","Pro0":"Segate 500Gb","Pro1":"Samsung 250Gb"},{"catname":"Ram","Pro0":"8Gb Ram","Pro1":"4Gb Ram","Pro2":"2Gb Ram"}]
I have tried changing the loop but I haven't found any solutions. Kindly help me to solve this.
I think, your problem in line $subpro1[] = substr($subpro, 0, -1)."}";.
So, first call of this line save data from "Veg.Pizaa" into $subpro1[0].
Second call of this line save data from "Core i7 Pc" into $subpro1[1].
Then, line $subproa = "[".implode(",",$subpro1)."]"; merged all array elements.
Just Use unset($subpro1); after the array_push($totArr, $prodObj2);.
Below Is An example Source
Try This One This May Work
$result2211 = mysql_query("select * from products where is_config = 'yes' ");
while($row2211 = mysql_fetch_assoc($result2211))
{
$sn = $row2211['sn'];
$allparrentselectq = mysql_query("SELECT * FROM parrentpro where parrentsn = $sn");
while($allparrentselect = mysql_fetch_assoc($allparrentselectq))
{
$childarr = unserialize($allparrentselect['childsn']);
$subpro = '{"catname":"'.$allparrentselect['childname'].'",';
$i = 0;
foreach($childarr as $childarr):
$subpro .= '"Pro'.$i++.'":"'.$childarr.'",';
endforeach;
$subpro1[] = substr($subpro, 0, -1)."}";
$subproa = "[".implode(",",$subpro1)."]";
}
$prodObj2 = new ProductDetails();
$prodObj2->productname = $row2211['productname'];
$prodObj2->price = $row2211['productprice'];
$prodObj2->discount = $row2211['discount'];
$prodObj2->discountprice = $row2211['discountprice'];
$prodObj2->imageURL = $row2211['productimageurl'];
$prodObj2->category = $row2211['productcat'];
$prodObj2->configurablepone = $subproa;
$prodObj2->configurable = 'yes';
array_push($totArr, $prodObj2);
unset($subpro1);
}
while($allparrentselect = mysql_fetch_assoc($allparrentselectq))
{
$childarr = unserialize($allparrentselect['childsn']);
$subpro = '{"catname":"'.$allparrentselect['childname'].'",';
$i = 0;
foreach($childarr as $childarr):
$subpro .= '"Pro'.$i++.'":"'.$childarr.'",';
endforeach;
$subpro1[] = substr($subpro, 0, -1)."}";
$subproa = "[".implode(",",$subpro1)."]";
$subpro1 = array();
}
try this in second while loop