Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Array an database row

    I have a table called answers.

    The structure of the table is:

    id
    candidate_id
    question_id
    answer

    I want to create an array based on candidate_id answer and in question_id order.

    For example,

    Answer of question(question_id) 1 is 1(answer) answered by candidate(candidate_id) 2
    Answer of question_id 2 is 2 answered by candidate_id 2
    Answer of question_id 3 is 1 answered by candidate_id 2
    Answer of question_id 1 is 1 answered by candidate_id 2
    Answer of question_id 1 is 2 answered by candidate_id 2

    So the array would be

    PHP Code:
    $array = array(
    'candidate_2' => array(1,2,1,1,2);

    I have around 15 candidates and 15 questions.

  • #2
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts

    Macintosh

    How about:

    PHP Code:
    if ($result mysql_query("select * from mytable") === FALSE) die ("error!");
    while (
    $row mysql_fetch_assoc($result)) $binned["candidate_".$row["candidate_id"]][] = $row["answer"];
    mysql_free_result($result); 
    That would produce an array in $binned like the one you specify in your code snippet.
    Last edited by mlse; 08-17-2007 at 10:10 PM.

  • #3
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlse View Post
    How about:

    PHP Code:
    if ($result mysql_query("select * from mytable") === FALSE) die ("error!");
    while (
    $row mysql_fetch_assoc($result)) $binned["candidate_".$row["candidate_id"]][] = $row["answer"];
    mysql_free_result($result); 
    That would produce an array in $binned like the one you specify in your code snippet.
    Very nice, thank you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •