Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following in which I am trying to make separate HTML tables after each group:

$query = "SELECT * FROM NonBillableTime WHERE TimeIn LIKE '$MondayP2%' ORDER BY CreatedUser ASC";
$members = mysql_query($query) or die($query."<br/><br/>".mysql_error());
while ($row = mysql_fetch_assoc($members)) {

Example HTML Table 1 would return everything with CreatedUser number 5 Then the table would end and a new table would start and return everything with the CreatedUser number 6.

I am facing two issues :-

  1. I don't know ahead how many users I have since it changes.
  2. It needs to execute from while ($row = mysql_fetch_assoc($members)) { since I am doing other calculations on the fly

Yes I want to use MYSQL and am aware of adding injection prevention.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
254 views
Welcome To Ask or Share your Answers For Others

1 Answer

Keeping the code pretty generic here, but presumably you're currently doing something like this:

// output a table header
while ($row = mysql_fetch_assoc($members)) {
    // output a table row
}
// output a table footer

If you want to begin a new table periodically in that loop, you'd need to add a condition to determine when to do that. So the structure would be more like this:

$currentUser = 1;
// output a table header
while ($row = mysql_fetch_assoc($members)) {
    // output a table row
    if ($row["CurrentUser"] != $currentUser) {
        // output a table footer
        // output a table header
        $currentUser = $row["CurrentUser"];
    }
}
// output a table footer

This is pretty off-the-cuff, so there may be a logical mistake in here by which a partial table is displayed under certain conditions or something of that nature, admittedly. But hopefully the gist of the idea is being conveyed. Essentially within the loop you can close and re-open the table (putting whatever information from the data you have into those headers/footers) based on a condition. You just have to track the data being used in that condition. In this case, the "current" CurrentUser value of the results.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...