如何在一个while循环中使用2个fetch_assoc?

时间:2015-07-20 10:23:42

标签: php mysql

我需要列出这样的东西。 显示第一个表格使用此代码。

self.view.translatesAutoresizingMaskIntoConstraints = NO;

enter image description here

另一方面是分配列表我不想手动编写它只是从数据库获取我该怎么办?

 <?php
    if (isset($_REQUEST['asign'])) {
        include 'includes/connection.php';

        $sql12= $cid->query("SELECT name FROM user WHERE designation='Technical' AND status='1'");
        $query = "SELECT * FROM allinone WHERE flag=3 ORDER BY id ASC";

        $rs = $cid -> query($query);

        $n = $rs -> num_rows;
        echo "<br /><span style='color:red;'><center>$n records found</center></span>";
        echo "<div id='record'>";
        echo "<table border='1' width='80%' align='center' cellpadding='0' cellspacing='0' id='unsolTable'>";
        echo "<tr>";
        echo "<th>ID</th>";
        echo "<th>Ticket Number</th>";
        echo "<th>KOID</th>";
        echo "<th>PROBLEM</th>";
        echo "<th>COMMENT</th>";
        echo "<th>DATE</th>";
        echo "<th>TIME</th>";
        echo "<th>STATUS</th>";
        echo "<th>SOLVED BY</th>";
        echo "<th>Assign</th>";
        echo "<th>Assigned to</th>";
        echo "</tr>";
        while ($row = $rs -> fetch_assoc() ) {

            echo "<tr>";
            echo "<td>" . $row['id'] . "</td>";
            echo "<td>" . $row['token'] . "</td>";
            echo "<td>" . $row['koid'] . "</td>";
            echo "<td>" . $row['problem'] . "</td>";
            echo "<td>" . $row['comment'] . "</td>";
            echo "<td>" . $row['date'] . "</td>";
            echo "<td>" . $row['time'] . "</td>";
            echo "<td>" . $row['status'] . "</td>";
            echo "<td>" . $row['solvedBy'] . "</td>";
    echo  "<td><a href='assign.php?qType=technical&name=anshul&id=" . $row['id'] . "'>anshul</a>
            <br />
            <a href='assign.php?qType=technical&name=kiran&id=" . $row['id'] . "'>kiran</a>
            <br />
            <a href='assign.php?qType=technical&name=akhilesh&id=" . $row['id'] . "'>akhilesh</a></td>";
            echo "<td>" . $row['assTo'] . "-" . $row['assTime'] . "-" . $row['assDate'] . "</td>";
            echo "</tr>";
            }
            echo"</table>";
            echo "</div>";
            }
            ?>

这是我想在第一段代码中添加的另一个代码。

1 个答案:

答案 0 :(得分:1)

没有什么能阻止你在循环中执行另一个SQL查询;只需将echo...anshul...echo...kiran...部分替换为包含您已提供代码的另一个while循环。

您只需确保使用不同的变量,否则第二个查询会覆盖您的$row变量。因此,你可以这样做:

// ...
echo "<td>" . $row['solvedBy'] . "</td>";

$resultSetAssignedUsers = $cid->query("SELECT name FROM user WHERE designation = 'Technical' AND status = '1'");
while ($rowAssignedUsers = $resultSetAssignedUsers->fetch_assoc()) {
    echo "<td><a href='assign.php?qType=technical&name=" . $rowAssignedUsers['name'] . "&id=" . $row['id'] . "'>" . $rowAssignedUsers['name'] . "</a>";
}

echo "<td>" . $row['assTo'] . "-" . $row['assTime'] . "-" . $row['assDate'] . "</td>";
// ...