/    Sign up×
Community /Pin to ProfileBookmark

How to add new object to Json

Dear Fellow Webmaster,

I have a question about adding data json

[{“TotalRows”:5,”TopID”:null,
“Rows”:[
{“articles_id”:”4″,”id”:”5621″,”type”:”articles”},
{“articles_id”:”5″,”id”:”5622″,”type”:”articles”},
]
}]

I want new data to be like this

[{“TotalRows”:2,”TopID”:1,
“Rows”:[
{“articles_id”:”4″,”id”:”5621″,”type”:”articles”,”web1″:”true”,”web2″:”true”},
{“articles_id”:”5″,”id”:”5622″,”type”:”articles”,”web1″:”true”,”web2″:”true”},
]
}]

adding object web1 and web2 (could be web1,web2,web3,web4/may change)

any idea how to add dynamically ?

the code

[code=php]$q= db_query(“select * from articles “);
while($row = mysql_fetch_assoc($q)) {
$orders[] = array(
‘articles_id’ => $row[articles_id],
‘id’ => $row[id],
‘type’ => $row[type],
);
$i++;
}
$data[] = array(
‘TotalRows’ => $i,
‘TopID’ => ‘1’,
‘Rows’ => $orders
);
echo json_encode($data);[/code]

Thank

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 03.2015 — You should be able to just do this:
[code=php]

while($row = mysql_fetch_assoc($q)) {
$orders[] = $row;
$i++;
}
$data[] = array(
'TotalRows' => $i,
'TopID' => '1',
'Rows' => $orders
);
[/code]
Copy linkTweet thisAlerts:
@socialmarketauthorMar 04.2015 — Thank for reply

inspire me to create what I want

like this
[code=php]while($row = mysql_fetch_assoc($q)) {
$resul= mnew_db_query("select domain from $dbf_domain.domain where username='".$_GET[username]."' ","");
while($dom = mysql_fetch_assoc($resul)) {
$qm=db_num_rows(mnew_db_query("select id from $dbf_articles.articles_domain where domain='".$dom[domain]."' and articles_id= '".$row['articles_id']."' and username='".$_GET[username]."' ",""));
if($qm=='1'){
$val='true';
}
else{
$val='false';
}
$row[$dom[domain]]=$val;
}
$orders[] = $row;
$i++;
}
$data[] = array(
'TotalRows' => $i,
'TopID' => $rs['articles_id'],
'Rows' => $orders
);[/code]


what I want is to add new name object can be change

if I use only [code=php]$row[/code] only create name object from that table fields.

This problem solved
×

Success!

Help @socialmarket spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...