-2

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • So inspect the results in your while loop... if CreatedUser = 5, then add to table A, else, add to table B. P.S. mysql_ is deprecated. – devlin carnate Dec 13 '16 at 18:59
  • I don't know the CreatedUsers number......Its not static as it changes – Mr Castrovinci Dec 13 '16 at 19:04
  • I imagine that your output loop would simply close and open the HTML table any time a new `CreatedUser` value is detected. Simply track the "current" value and check in each iteration of the loop if the new value is different from the current one. – David Dec 13 '16 at 19:04
  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Dec 13 '16 at 19:04
  • Then build your HTML tables in variables and output the variables where ever you want them on the page – RiggsFolly Dec 13 '16 at 19:06
  • @David yes, this is what I am looking for....Can you provide an example? – Mr Castrovinci Dec 13 '16 at 19:06
  • @RiggsFolly......Wow if I had a dollar for everytime I heard that – Mr Castrovinci Dec 13 '16 at 19:07
  • 1
    Well maybe you should consider listening then – RiggsFolly Dec 13 '16 at 19:09
  • @RiggsFolly.....Maybe if you read my post and see it says I want to use mysql.....why waste your time posting such? – Mr Castrovinci Dec 13 '16 at 19:10
  • a ternary operator would work here, rather than `if($row['x'] == 5){...}` but you could and then output accordingly in the said html tables. – Funk Forty Niner Dec 13 '16 at 19:10
  • @MrCastrovinci - your post says you want to use MySQL, which is a database flavor. What's being suggested is that you stop using mysql_ extensions and start using PDO or mysqli_. No one has suggested that you stop using MySQL. – devlin carnate Dec 13 '16 at 19:17
  • devlin carnate I think it perfectly implies on the adding injection part which was the downfall of ......anyway has nothing to do with the answer, so I hope you get your day back. Thanks cheers! – Mr Castrovinci Dec 13 '16 at 20:19

1 Answers1

2

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.

David
  • 208,112
  • 36
  • 198
  • 279