Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

php - Separate MYSQL results into separate HTML Tables

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...