开发者

Pushing multiple rows of a SQL query into a single PHP array item

I'm having a brain fart right now. I'm pulling questions and possible answers from a database to dynamically generate a quiz using PHP and MySQL. Here is the output I get:

    Response id: 3 Question id: 1 Question: What is my middle name? Title: How Well Do You Know Michael
    Array (
        [0] => Array (
            [r_id] => 3
            [question_id] => 1
            [question] => What is my middle name?
            [title] => How Well Do You Know Michael ) ) 
    Array (
        [0] => Array (
            [0] => 1
            [1] => Abe )
        [1] => Array (
            [0] => 2
            [1] => Andrew )
        [2] => Array (
            [0] => 3
            [1] => Andre )
        [3] => Array (
            [0] => 4
            [1] => Anderson ) ) 

from this PHP script:

// Grab the question data from the database to generate the form
    $query = "SELECT qr.response_id AS r_id, qr.question_id, q.question, quiz.title " . 
         "FROM quiz_response AS qr " . 
         "INNER JOIN question AS q USING (question_id) " . 
         "INNER JOIN quiz USING (quiz_id) " .
         "WHERE qr.user_id = '" . $_SESSION['user_id'] . "'";
    $data = mysqli_query($dbc, $query) or die("MySQL error: " . mysqli_error($dbc) . "<hr>\nQuery: $query");
    $questions = array(); 
    while ($row = mysqli_fetch_array($data, MYSQL_ASSOC)) {
        echo 'Response id: ' . $row['r_id'] . 'Question id: ' . $row['question_id'] . ' Question: ' . $row['question'] . ' Title: ' . $row['title'] . '<br />';
        array_push($questions, $row);
        // Pull up the choices for each question
        $query2 = "SELECT choice_id, choice FROM question_choice " .
        "WHERE question_id = '" . $row['question_id'] . "'";
        $data2 = mysqli_query($dbc, $query2);
        $choices = array();
        while ($row2 = mysqli_fetch_array($data2, MYSQL_NUM))
            array_push($choices, $row2);
    }
    print_r($questions);
    print_r($choices);

However, I would ideally like to have just one item in the $choices array, as opposed to 4 separate items. For example, I'd like the the choices array to look like the following:

Array (
    [0] => Array (
        [0] => 1
        [1] => Abe
        [2] => 2
        [3] => Andrew
        [4] => 3
        [5] => Andre
        [6] => 4
        [7] => Anderson )

My question: **Although I will have开发者_StackOverflow社区 four separate rows from the `

while ($row2 = mysqli_fetch_array($data2, MYSQL_NUM))
            array_push($choices, $row2);

`is there anyway to push them all into the same item of the array $choices?**


Replace this:

array_push($choices, $row2);

With

$choices[0][] = $row2[0];
$choices[0][] = $row2[1];

Anyway, I think it would be more useful not to have an array of 1 element inside an array. Why don't you do this?

$choices[] = $row2[0];
$choices[] = $row2[1];

My last comment is that I would go for your current option (the array of rows with each row as an array containing the columns) because it is closer to the way data is really represented.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜