I would like to convert an HTML ul list into a json format.
The html list is in this format:
<li><span class="orgname">Adams Center Free Library</span> <span class="status">Closed</span></li>
<li><span class="orgname">Weight Watchers Watertown</span> <span class="status">Delayed</span></li>
I'd like it to be in a json format like this:
{
"meta": {
"closed_count": "1",
"delayed_count": "1"
},
"list": [
{
"data": {
"orgname": "Adams Center Free Library",
"status": "Closed"
}
},
{
"data": {
"orgname": "Weight Watchers Watertown",
"status": "Delayed"
}
}
]
}
Can someone help me out with this?
I assume you are outputting data from some database. So add values to array and than encode to json:
$delayed = 0;
$close = 0;
$data = [];
echo "<ul>";
foreach ($myArray as $row) {
echo "<li><span class='orgname'>{$row['orgname']}</span> <span class='status'>{$row['status']}</span></li>";
$data[] = ['data' => ['orgmane' => $row['orgname'], 'status' => $row['status']]];
// or if $row only has 'orgname' and 'status'
$data[] = ['data' => $row];
if (strtolower($row['status']) == 'delayed') {
$delayed++;
} else {
$closed++;
}
}
echo "</ul>";
json_encode(
[
'meta' => ['closed_count' => $closed, 'delayed_count' => $delayed],
'list' => $data,
]
);
Related
I have the code below, but the result of the json response doesn't match
public function tes_get(){
$kode= $this->M_mymodel->tbl_kode('302'); // row
$res = array();
foreach ($kode as $key=> $value) {
$win = $this->M_mymodel->db_aa('302'); //row_array
$res = $this->M_mymodel->db_bb('302','LOSE'); //row_array
$res['data'][] = array(
'win' => $win['menang'],
'lose' =>$res['kalah']
);
}
$response = $this->set_response($res,200);
}
Below is the result of the response from the code I made above
{
"data": [
{
"win": "2",
"lose": "11"
}
]
}
How to make json response like below?
{
"data": [
{
"win": "2",
}
{
"lose": "11"
}
]
}
You can try :
$res['data'][] = array(
array('win' => $win['menang']),
array('lose' =>$res['kalah'])
);
I am trying to display data for a plugin with a predefined json format, but after I try the data does not match the format how the problem?
JSON Required
[
{
latLng: [-6.17343444,1206.834234294],
name: 'XY'
},
{
latLng: [-6.1742343244,106.898987294],
name: 'XK'
}
]
Result my JSON
[
{
"latLng": "-6.17343444,1206.834234294",
"name": "XK"
},
{
"latLng": "-6.1742343244,106.898987294",
"name": "XY"
}
]
myscript PHP
public function lat_lang() {
foreach($this->model->name_model() as $row){
$data[] = array(
'latLng' => $row->lat_long,
'name' => $row->city
);
}
header('content-type: application/json');
echo json_encode($data);
}
call JSON
$.parseJSON('<?php echo base_url().'mycontrollers';?>', function(datas) {
console.log(datas);
});
You can use explode() to transform the string "-6.17343444,1206.834234294" into an array [-6.17343444,1206.834234294]:
public function lat_lang() {
foreach($this->model->name_model() as $row){
$data[] = array(
'latLng' => explode(',',$row->lat_long),
'name' => $row->city
);
}
header('content-type: application/json');
echo json_encode($data);
}
If you want to get floats (for all value in the JSON), you could use JSON_NUMERIC_CHECK:
json_encode($data, JSON_NUMERIC_CHECK);
Or, just for a specific value:
$latLng = explode(',', $row->lat_long);
$latLng = array_map('floatval', $latLng);
$data[] = array(
'latLng' => $latLng,
'name' => $row->city
);
$emparray=array();
while($row = mysqli_fetch_array($result,MYSQL_NUM))
{
foreach ($row as $key => $value)
{
if ($value == '')
{
$row[9] ='We follow-up you';
}
}
$emparray[] = array
('success' => true,
'message' => "audio saved Successfully!",
array('client_cname' => $row[2],
'future_followup' => $row[8],
'notes' => $row[9]));
}
** as you can see above this is my code for fetching records from db using while loop.
problem is im getting all records but with diffrent arrys. i want it in only 1 array with indexes**
[
{
"success": true,
"message": "audio saved Successfully!",
"0": {
"client_cname": "Hhhs",
"future_followup": "4 Jan 2017 05:17 PM",
"notes": "hey"
}
},
{
"success": true,
"message": "audio saved Successfully!",
"0": {
"client_cname": "Hcjc",
"future_followup": "4 Jan 2017 06:17 PM",
"notes": "hey"
}
}
]
above is my O/P
and i want it in below format please help me with the same.
{
"status": "true",
"message": "Audio Saved successfully!"
[
{
"client_cname": "Atul",
“future_followup": “”,
“notes": “”,
},
{
"client_cname": "Atul",
“future_followup": “”,
“notes": “”,
}
]
}
if i remove ->[ ] from $emparray then i will get only last row of table like below code
{
"success": true,
"message": "audio saved Successfully!",
"0": {
"client_cname": "Hcjc",
"future_followup": "4 Jan 2017 06:17 PM",
"notes": "hey"
}
}
but i want this kind of result with index 0,1,2 and so on.
please help me with the same. thanks in advance.
Here's what you need to do:
if(mysqli_num_rows($result)>0) { //if there are any results
//initialise the array with the first two values
$emparray = array(
'success' => true,
'message' => "audio saved Successfully!");
}
while($row = mysqli_fetch_array($result,MYSQL_NUM))
{
foreach ($row as $key => $value)
{
if ($value == '')
{
$row[9] ='We follow-up you';
}
}
//append each data item as an array
$emparray[] = array('client_cname' => $row[2],
'future_followup' => $row[8],
'notes' => $row[9]));
}
This will also automatically index all the other arrays numerically, starting from 0.
May be this is more better way to deal with.
$dataSet = array();
while ($row = mysqli_fetch_array($result, MYSQL_NUM)) {
foreach ($row as $key => $value) {
if ($value == '') {
$row[9] = 'We follow-up you';
}
}
$dataSet[] = array('client_cname' => $row[2],
'future_followup' => $row[8],
'notes' => $row[9]);
}
$emparray = array
('success' => true,
'message' => "audio saved Successfully!",
'data' => $dataSet);
echo json_encode($emparray);
Here's what I want to do in my php array to be exact json format below:
JSON
{
"suggestions": [
{ "value": "Alex - alex#email.com", "data": {"id": 1, "name": Alex } },
{ "value": "John - john#email.com", "data": {"id": 2, "name": John } },
{ "value": "Diaz - diaz#email.com", "data": {"id": 3, "name": Diaz } }
]
}
Query result in my php array:
array(
0 => array('id'=>'1' 'email'=>'alex#email.com', 'name'=>'Alex'),
1 => array('id'=>'2' 'email'=>'john#email.com', 'name'=>'John'),
2 => array('id'=>'3' 'email'=>'diaz#email.com', 'name'=>'Diaz')
);
Do you have any idea how will you make my php array to that JSON format way?
You can simply use json_encode(); function for that.
json_encode($array);
This should help you for your JSON format
foreach($query as $key => $val){
$json[$key]['value'] = $val['name']." - ".$val['email'];
$json[$key]['data']["id"] = $val['id'];
$json[$key]['data']["name"] = $val['name'];
}
echo json_encode($json);
foreach ($your_array as $key => $val) {
foreach ($val as $k => $v) {
if ($v == 'email') {
//get the value of 'email' key in 'value'
$newArr['suggestions']['value'] = current($v);
}
else {
//if value is not email push it in 'data' key
$newArr['suggestions']['data'] = $v;
}
}
}
//lastly encode the required array
echo json_encode($newArr);
I am trying to get my JSON output like this.
{"allterms":[{"group":{"Name":"Test 1"},{"group":{"Name":"Test2","Id":"298"}}]
My current code is
while($r = mysql_fetch_assoc($rs)) {
$rows['allterms']['group'][] = $r;
}
Which gives me this
{"allterms":{"group":[{"Name":"Test1", "Id":"1740"},{"Name":"Test2","Id":"631"}}]
How can I adjust my code so that each item has a parent term group.
Change the loop like so:
while($r = mysql_fetch_assoc($rs)) {
$rows['allterms'][]['group'] = $r;
}
which will generate:
array(
'allterms' => array(
0 => array(
'group' => array(...),
),
1 => array(
'group' => array(...),
)
...
)
which as json will be:
{
"allterms": [
{
"group": {
{
"Name": "Test1",
"Id": "1740"
},
{
"group": {
{
"Name": "Test2",
"Id": "631"
}
}
]
}
You could use the array_push() function from PHP.
while($r = mysql_fetch_assoc($rs)) {
array_push($rows['allterms'], $r);
}