0

I am revamping an application that is using PHP on the serverside which outputs JSON format.

{"by":"Industrie LLC","dead":false,"descendants":396,"id":"396","kids":[1,396],"score":396,"time":"396","title":"Industrie LLC","type":"comment","url":"www.nytimes.com"}

as it is i am getting the last column of mysql data.i know it is something with the loops but i have no idea what in specific.

My PHP code is here

$sql_metro_company_doc_legal = "SELECT * FROM ".$configValues['CONFIG_DB_TBL_PRE']."posts where post_type='company'";
$res_metro_company_doc_legal = $dbSocket->query($sql_metro_company_doc_legal);

while($row_metro_company_doc_legal = $res_metro_company_doc_legal->fetchRow()) { 
    $notice2[]  = $row_metro_company_doc_legal[5]; 
    $notice8[]  = strtotime($row_metro_company_doc_legal[0]);
    $notice9[]  = $row_metro_company_doc_legal[0];
    $notice3[]  = $row_metro_company_doc_legal[0];
    $notice  = array("id" => "".$row_metro_company_doc_legal[1]."","title"=>"".$row_metro_company_doc_legal[0].""); 
    $notice10[]  = $row_metro_company_doc_legal[0]; 
    $notice6[]  = $row_metro_company_doc_legal[0]; 
    $notice11[]  = $row_metro_company_doc_legal[5]; 
    $notice7[]  = strtotime($row_metro_company_doc_legal[2]); 
    $notice12[]  = 'www.nytimes.com'; 
    $notice7[]  = "comment"; 
}
foreach ($notice2 as $status2) {
    $_page['by'] = $status2;
}
foreach ($notice8 as $status8) {
    $_page['dead'] = $status8;
}
foreach ($notice9 as $status9) {
    $_page['descendants'] = (int)$status9;
}
foreach ($notice3 as $status3) {
    $_page['id'] = $status3;
}
foreach ($notice as $status) {
    $_page['kids'][] = (int)$status;
}
foreach ($notice10 as $status10) {
    $_page['score'] = (int)$status10;
}
foreach ($notice6 as $status6) {
    $_page['time'] = $status6;
}
foreach ($notice11 as $status11) {
    $_page['title'] = $status11;
}
foreach ($notice7 as $status7) {
    $_page['type'] = $status7;
}
foreach ($notice12 as $status12) {
    $_page['url'] = $status12;
}
foreach ($notice4 as $status4) {
    $_page['parent'] = (int)$status4;
}
foreach ($notice5 as $status5) {
    $_page['text'] = $status5;
}               

//sets the response format type
header("Content-Type: application/json");

//converts any PHP type to JSON string
echo json_encode($_page); 
6
  • Each time through the foreach loops you're assigning to the same variable. At the end of the loop it will have the value from the last element. What do you want the resulting JSON to look like? Commented Oct 24, 2015 at 0:36
  • I want something like this: {"by":"Industrie LLC","dead":false,"descendants":396,"id":"396","kids":[1,396],"score":396,"time":"396","title":"Industrie LLC","type":"comment","url":"www.nytimes.com"} Commented Oct 24, 2015 at 0:38
  • Why not create an array inside your while loop? Commented Oct 24, 2015 at 0:44
  • @philipwanekeya It looks like that's what you'll get with your code. It will be the last row of the query. Why do you say you're getting the last column? Commented Oct 24, 2015 at 0:47
  • Are you sure you don't want an array of objects, [{ ...}, { ... }, ...]? Commented Oct 24, 2015 at 0:49

1 Answer 1

1

You need to make a 2-dimensional array in $_page.

$_page = array();
foreach ($notice2 as $i => $status) {
    $_page[] = array(
        'by' => $status,
        'dead' => $status8[$i],
        'descendants' => (int)$status9[$i],
        'id' => $status3[$i],
        // and so on for the rest
    );
}
header ("Content-type: application/json");
echo json_encode($_page);
Sign up to request clarification or add additional context in comments.

Comments

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.