DisplayName DisplayName - 2 months ago 12
jQuery Question

HTML table from output

This may be very basic but I have searched for over a day trying various different things, all to no avail.

I am trying to output more than 1 row if more than 1 row exists. So far it is only showing me the last row!

My code is as follows:

$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>


Basically, I want to search for a Client ID, and have it return all cases which are for that particular client.

Answer

You have to store your caseID rows into an array and then loop over it :

<?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>

EDIT : Same goes for SalesID.

EDIT 2 : With only one array :

<?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>
Comments