输出中的HTML表

时间:2016-09-14 09:02:15

标签: php jquery

这可能是非常基本的,但我已经搜索了一天,尝试各种不同的事情,但都无济于事。

如果存在多于一行,我试图输出超过1行。到目前为止,它只向我显示最后一行!

我的代码如下:

$query = "SELECT CaseID FROM Cases WHERE ClientID='".$_GET['ClientID']."'";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_array($result))
{
    $CaseID = $row['CaseID'];
}

$result = mysqli_query($con,"SELECT dbUser FROM SalesUser WHERE ID='".$SalesID."';");
while($row = mysqli_fetch_array($result))
{
    $SalesUser = $row['dbUser'];
}

?>

<table class="table table-striped">
  <thead>
    <tr>
      <th>Case ID</th>
      <th>Sales User</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><? echo $CaseID; ?></td>
      <td><? echo $SalesUser; ?></td>
    </tr>
  </tbody>
</table>

基本上,我想搜索一个客户端ID,并让它返回所有针对该特定客户端的案例。

2 个答案:

答案 0 :(得分:1)

您必须将caseID行存储到数组中,然后循环遍历它:

<?php

$query = "SELECT CaseID FROM Cases WHERE ClientID='".$_GET['ClientID']."'";
$result = mysqli_query($con, $query);
$CaseIDs = array();
while($row = mysqli_fetch_array($result)) {
    $CaseIDs[] = $row['CaseID'];
}

$result = mysqli_query($con,"SELECT dbUser FROM SalesUser WHERE ID='".$SalesID."';");
while($row = mysqli_fetch_array($result)) {
    $SalesUser = $row['dbUser'];
}

?>

<table class="table table-striped">
    <thead>
        <tr>
            <th>Case ID</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($CaseIDs as $CaseID) { ?>
        <tr>
            <td><? echo $CaseID; ?></td>
        </tr>
    <?php } ?>
    </tbody>
</table>

编辑:同样适用于SalesID

编辑2:只有一个数组:

<?php

$query = "SELECT Cases.CaseID, SalesUser.dbUser FROM Cases LEFT JOIN SalesUser ON [...] WHERE [...] "; // Can't help you with your query as I don't know your DB structure
$result = mysqli_query($con, $query);
$resultsArray = array();
while($row = mysqli_fetch_array($result)) {
    $resultArray = array('CaseID' => $row['CaseID'], 'dbUser' => $row['dbUser']);
    $resultsArray[] = $resultArray;
}

?>

<table class="table table-striped">
    <thead>
        <tr>
            <th>Case ID</th>
            <th>Sales User</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($resultsArray as $resultArray) { ?>
        <tr>
            <td><?php echo $resultArray['CaseID']; ?></td>
            <td><?php echo $resultArray['dbUser']; ?></td>
        </tr>
    <?php } ?>
    </tbody>
</table>

答案 1 :(得分:0)

对于任何可能遇到此问题的人,@ roberto06友善地协助,现在已经修复并正在运作。

$query = "SELECT CaseID, Provider, FROM Cases WHERE ClientID='".$_GET['ClientID']."'";
$result = mysqli_query($con, $query);
$resultsArray = array();
while($row = mysqli_fetch_array($result))
{
    $resultArray = array('CaseID' => $row['CaseID'], 'Provider' => $row['Provider'] );
    $resultsArray[] = $resultArray;
}

?>

<table class="table table-striped">
    <thead>
        <tr>
            <th>Case ID</th>
            <th>Provider</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($resultsArray as $resultArray) { ?>
        <tr>
            <td><? echo $resultArray['CaseID']; ?></td>
            <td><? echo $resultArray['Provider']; ?></td>
        </tr>
    <?php } ?>
    </tbody>
</table>