MySQL ping groups with unique PHP value

I apologize if there are similar questions ... I could not find them! What I would like to do is select several rows in the database and then extract them, but repeat them in groups based on unique rows. Let me explain with an example (I briefly omitted some things):

I have a request to select:

SELECT
    e.exercisename ExerciseName,
    eh.reps Reps,
    eh.weight Weight
FROM workouts w
JOIN users u ON w.userid = u.id
JOIN workouts_history wh ON w.id = wh.workoutid
JOIN exercises e ON wh.exerciseid = e.id
JOIN exercises_history eh ON wh.id = eh.workouts_historyid

Right now, this gives me a table based on this, while:

while($workoutrowridge = $workoutresultridge->fetch_assoc()) {
    $workoutoutputridge .= '<tr>';
        $workoutoutputridge .= '<td>'.$workoutrowridge['ExerciseName'].'</td>';
        $workoutoutputridge .= '<td>'.$workoutrowridge['Reps'].'</td>';
        $workoutoutputridge .= '<td>'.$workoutrowridge['Weight'].'</td>';               
    $workoutoutputridge .= '</tr>';
}

as follows:

Exercise    | Reps | Weight
---------------------------
Squats      |   8  |  135
Squats      |   8  |  225
Squats      |   6  |  315
Squats      |   2  |  405
Squats      |   1  |  485
Bench (DB)  |   8  |  60
Bench (DB)  |   6  |  80
Bench (DB)  |   4  |  90
Bench (DB)  |   2  |  95
Pullup      |   4  |  0
Pullup      |   3  |  25
Pullup      |   1  |  45
Pullup      |   1  |  70

What I would like to do is make a new table appear for each unique "ExerciseName" (for example). A workout can only have 1 exercise, or it can be 20, with a different number of sets for each of them, for example:

Exercise    | Reps | Weight
---------------------------
Squats      |   8  |  135
Squats      |   8  |  225
Squats      |   6  |  315
Squats      |   2  |  405
Squats      |   1  |  485

Exercise    | Reps | Weight
---------------------------
Bench (DB)  |   8  |  60
Bench (DB)  |   6  |  80
Bench (DB)  |   4  |  90
Bench (DB)  |   2  |  95

, - foreach PHP ... . ?

+4
3

ExerciseName,

,

$lastExercise = ""; // my edit
while($workoutrowridge = $workoutresultridge->fetch_assoc()) {
    if($workoutrowridge['ExerciseName'] != $lastExercise ){
         $workoutoutputridge .= "<tr><td>Exercise</td><td>Reps</td><td>Weight</td></tr>";
    }
    $lastExercise = $workoutrowridge['ExerciseName'];

//the rest is your original code
    $workoutoutputridge .= '<tr>';
         $workoutoutputridge .= '<td>'.$workoutrowridge['ExerciseName'].'</td>';
         $workoutoutputridge .= '<td>'.$workoutrowridge['Reps'].'</td>';
         $workoutoutputridge .= '<td>'.$workoutrowridge['Weight'].'</td>';               
    $workoutoutputridge .= '</tr>';
}
+4

- :

$currentExercise = '';
echo '<table>';
while($workoutrowridge = $workoutresultridge->fetch_assoc()) {
    if($currentExercise !== '' && $workoutoutputridge['ExerciseName'] !== $currentExercise) {
        echo '</table><table>';
    }
    $workoutoutputridge .= '<tr>';
    $workoutoutputridge .= '<td>'.$workoutrowridge['ExerciseName'].'</td>';
    $workoutoutputridge .= '<td>'.$workoutrowridge['Reps'].'</td>';
    $workoutoutputridge .= '<td>'.$workoutrowridge['Weight'].'</td>';
    $workoutoutputridge .= '</tr>';

    $currentExercise = $workoutrowridge['ExerciseName'];
}
echo '</table>';
+4

[edit] without empty <table>if you have no exercises


For something clean you can use this:

$currentEx = false;

while($workoutrowridge = $workoutresultridge->fetch_assoc()) {

    if(!$currentEx) // first round
        $workoutoutputridge .= "<table>\n";
    else if($currentEx AND $workoutrowridge['ExerciseName'] != $currentEx){
        $workoutoutputridge .= "</table>\n";
        $workoutoutputridge .= "<table>\n";
        $currentEx = $workoutrowridge['ExerciseName'];
    }

    $workoutoutputridge .= "<tr>\n";
    $workoutoutputridge .=      "<td>".$workoutrowridge['ExerciseName']."</td>\n";
    $workoutoutputridge .=      "<td>".$workoutrowridge['Reps']."</td>\n";
    $workoutoutputridge .=      "<td>".$workoutrowridge['Weight']."</td>\n";         
    $workoutoutputridge .= "</tr>\n";

}

if($workoutoutputridge)
    $workoutoutputridge .= "</table>\n";
+2
source

Source: https://habr.com/ru/post/1660628/


All Articles