1

looking at the php function bellow i have this scenario: there is more then one IdUserfollow each id have to be passed on result2 and all the retrieved values have to be combined and returned to Xcode

 function viewhome($IdUser){
    $result1 = query("SELECT * FROM follows  WHERE IdUser = '%s' ", $IdUser);
    $IUser = $result['result'][0]['IdUserfollow'];
    $result2 = query("SELECT * FROM photos WHERE IdUser = '%s' ORDER BY IdPhoto DESC LIMIT    50", $IUser);
    print json_encode($result2);
}

how can i do that.

i sow this problem Using a Loop Inside a Function which was similar to mine but still I'm confused. considering that my return will be using JSON and also i don't know how to set the loop limit from the first query result. is it possible that i will have to do two functions, the first one will return the number of repetition and the second one will only have the loop and return the result? if yes then how can i combine two results - or whatever the value of the repetition is- and send it back using json

any help would be appreciated.

1 Answer 1

0

You may collect all retrieving data from second query to result array like this:

 function viewhome($IdUser){
    $result1 = query("SELECT * FROM follows  WHERE IdUser = '%s' ", $IdUser);
    $IUser = $result['result'][0]['IdUserfollow'];
    $result = array();
    foreach ( $result['result'] as $data ) {
       $result[] = query("SELECT * FROM photos WHERE IdUser = '%s' ORDER BY IdPhoto DESC LIMIT    50", $data['idUserfollow']);
    }
    print json_encode($result); // all the results for each IdUserfollow from first query
}

But all the same, I advice you not to send several queries to DB, but build only one query, wich solve you problem.

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.