0

I have 2 table users(id,userid,fn,ln) and userdetails(id,userid,image,history,location,activity) And i have written a query for first table to retrieve all the data and i want only history and location ,from second table.

i have retrieved the array and i am sending it to json_encode. Now i want to retrieve the history,location and create a new key History and i want to add history location values to history key.

I need a query and json format for these.

  • For particular user i need to retrieve is own history In both tables user id in common Thanks in advance

    $sth = mysql_query("SELECT * FROM users");$result = mysql_fetch_assoc($sth); $i=0; foreach($result as $data) { $final_array[$i]['id'] = $data['id']; $final_array[$i]['email'] = $data['email']; $final_array[$i]['fname'] = $data['fname']; $final_array[$i]['lname'] = $data['lname'];

        $sth2 = mysql_query("SELECT id,places,act FROM user_dates WHERE user_id= '".$data['email']."'");
        $result2 = mysql_fetch_assoc($sth2);
        $j=0;
        $history_array = array();
        foreach($result2 as $data2) { 
          $history_array[$j] = array("id" => $data2['id'],"places" => $data2['places'], "act " => $data2['act ']);
          $j++;
        }
        $final_array[$i]['history'] = $history_array;
    
    
        $i++;
    } 
    echo json_encode($final_array);
    

[ { "id": "81", "user_id": "2011", "fn": "asd.", "ln": "wer", "History": [ { "id": "350", "history": "make1", "Location": "qwe" } ] }, { "id": "82", "user_id": "2012", "fn": "asd1", "ln": "wer1", "History": [ { "id": "350", "history": "make2", "Location": "qwe2" } ] } ]

1 Answer 1

0

Userdetails table contains multiple records per user. So you need to do sub query and get the results and then form a mulch-dimensional array. Finally, encode as a JSON.

$sth = $dbh->prepare("SELECT * FROM users");
$sth->execute();
$result = $sth->fetchAll();

$i=0;
foreach($result as $data) { 
    $final_array[$i]['id'] = $data['id'];
    $final_array[$i]['userid'] = $data['userid'];
    $final_array[$i]['fn'] = $data['fn'];
    $final_array[$i]['ln'] = $data['ln'];

    $sth2 = $dbh->prepare("SELECT location,activity FROM userdetails WHERE userid= ".$data['id']."");
    $sth2->execute();
    $result2 = $sth2->fetchAll();
    $j=0;
    $history_array = array();
    foreach($result2 as $data2) { 
      $history_array[$j] = array("location" => $data2['location'], "activity " => $data2['activity ']);
      $j++;
    }
    $final_array[$i]['history'] = $history_array;


    $i++;
} 
echo json_encode($final_array);
13
  • Suppose if i want to fetch all data from table1 ie., users and only history and activity from second table2 ie., user details Nov 15, 2014 at 14:21
  • you can simply add "select users.*, , userdetails.history, , userdetails.activity"
    – Asik
    Nov 15, 2014 at 14:24
  • for history i have missing square brackets "History": [ { "id": "350", "history": "make1", "Location": "qwe" } ] as i shown above what can be done for these Nov 15, 2014 at 14:43
  • $final_array[$i]['history'] = array(array("id" => $data['userDetailsId'], "history" => $data['history'], , "Location" => $data['Location']));
    – Asik
    Nov 15, 2014 at 14:47
  • The query is running depend on 2 table if i want 2 run a query depend on 1 table For example: i have only two users for that 2 users there will be to n number of histories stored in table 2 ,i need only 2 rows in the output all history should be inside particular row of the user. can u tell me what solves my problem please Nov 15, 2014 at 14:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.