具有唯一值PHP的MySQL回显组 [英] MySQL echo groups with unique value PHP

查看:54
本文介绍了具有唯一值PHP的MySQL回显组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有类似问题,我深表歉意...我找不到他们!我想做的是在数据库中选择一些行,然后提取它们,但是根据唯一的行按组将它们回显.让我用一个例子来解释(为简洁起见,我省略了一些东西):

I apologize if there are similar questions...I had trouble finding them! What I would like to do is select some rows in a database, and then fetch them, but echo them in groups based on unique rows. Let me explain with an example (I have omitted some things for brevity):

我有一个这样的选择查询:

I have a select query like this:

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 off of 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>';
}

看起来像这样:

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

我想做的是,为每个唯一的"ExerciseName"(例如)回显一个新表.一项锻炼可能只有一项锻炼,或者可能有20项锻炼,每种锻炼的锻炼量不同,如下所示:

What I would like to happen though, is to have a new table echoed for each unique 'ExerciseName' (for example). A workout may have only 1 exercise, or it may have 20, with a different amount of sets for each one, like this:

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

我觉得有一种方法可以将其保留为一个查询,并且只需在PHP中使用某种类型的foreach即可完成此操作……但是我一直无法获得它.有什么建议吗?

I feel like there is a way to keep this as one query, and just use some type of foreach in the PHP to do this...but I have been unable to get this. Any suggestions?

推荐答案

如果查询结果按ExerciseName排序,我会这样做

if the query result is sorted by the ExerciseName I would do it like this

这是您的循环,我只添加几行

this is your loop , I will just add couple of lines

$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>';
}

这篇关于具有唯一值PHP的MySQL回显组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆