while循环while while循环只返回第一行

时间:2017-06-08 07:25:14

标签: php mysql pdo while-loop

我正在尝试使用不同的对象填充数组。我有一组页面和一组问题。页面是小组,问题属于他们。

我想查询第一页,然后找出其中有多少问题。之后,为每个问题创建对象,并将它们附加到特定页面中。

// first get pages so we get grouping and order
$getPages = $conn->prepare("SELECT * FROM pages WHERE event_id = :event_id ORDER BY page_order ASC");
$getPages->bindParam(":event_id", $_SESSION['event_id']);
$getPages->execute();
$pageCount = $getPages->rowCount();
$pages = array();

// get pages
while ($row = $getPages->fetch()) {
    $page = array();
    $page['id'] = $row['id'];
    $page['order'] = $row['page_order'];

    // query for that page
    $stmt = $conn->prepare("SELECT * FROM questions WHERE page_id = :page_id");
    $stmt->bindParam(":page_id", $page['id']);
    $stmt->execute();
    while ($getQuestions = $stmt->fetch()) {
        $question = array();
        $question['id'] = $getQuestions['id'];
        $question['content'] = $getQuestions['content'];
        // push question into questions
        $page['questions'] = $question;
    }

    // push page into pages
    $pages[] = $page;
}

//print pages
echo json_encode($pages);

但遗憾的是,每页只返回一行......

Javascript object

1 个答案:

答案 0 :(得分:3)

这是因为你的问题每次都会被覆盖。更改您的代码,如下所示

<?php
$output = shell_exec("sh r.sh"); // r.sh contains the command
preg_match("/1D000700 +\[2\]\s+\K[A-Z0-9]{2}\s+[A-Z0-9]{2}/",$output,$value);
echo "$value A";
?>