具有唯一值PHP

时间:2016-11-13 14:50:29

标签: php mysql foreach

如果有类似的问题,我道歉......我找不到它们!我想要做的是在数据库中选择一些行,然后获取它们,但是基于唯一行在组中回显它们。让我用一个例子来解释(为了简洁,我省略了一些东西):

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

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

现在,这给了我一张基于此的表格:

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

我想要发生的是,为每个独特的“运动名称”提供一个新的表格。 (例如)。锻炼可能只有1次锻炼,或者可能有20次,每次锻炼的次数不同,如下所示:

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来执行此操作...但我无法得到它。有什么建议吗?

3 个答案:

答案 0 :(得分: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>';

答案 1 :(得分:4)

如果查询结果按<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="ticket"> <div class="btop">Day 1</div> <div class="bbottom">Price 20</div> </div> <div class="ticket"> <div class="btop">Day 2</div> <div class="bbottom bbottom2">Price 99</div> </div> <div class="ticket"> <div class="btop">Day 3</div> <div class="bbottom bbottom2">Price 149</div> </div>排序,我会这样做

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

ExerciseName

答案 2 :(得分:2)

[编辑] 没有空<table>如果你没有任何练习

对于干净的东西,你可以使用它:

$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";