2016-11-13 65 views
4

如果有类似的问题,我很抱歉...我很难找到它们!我想要做的是在数据库中选择一些行,然后获取它们,但是根据唯一的行将它们分组回显。让我用一个例子解释(我省略了一些东西为简洁起见):具有唯一值的MySQL回声组PHP

我有这样的选择查询:

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 

我想做些什么,虽然发生了,是为h为每个独特的“练习名称”(例如)附上新的表格。锻炼可能只有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来做到这一点......但我一直无法得到这个。有什么建议么?

回答

4

如果查询结果由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>'; 
} 
+2

这是一个很好的解决方案,谢谢。我将接受另一个答案作为正确答案,因为这是第一个答复。但我仍然会给你一个这个答案的投票。 –

+0

@RidgeRobinson没关系我只是想帮忙,你很有礼貌:),是他的回答来之前米恩:) –

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

这是一个很好的解决方案......如此简单。我不知道为什么我没有想到这一点......我试图变得更复杂,有时候简单的方法是最好的! –

+0

是的,你也可以创建一个新的阵列与另一个子级练习名称 – Max

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"; 
+0

*“为了干净的东西,你可以使用这个”* - 其实,为了更“干净”的东西,为了避免让HTML源代码在一行中看起来像一大堆代码,在每个元素产生干净的HTML之后添加'“\ n”';当需要通过(HTML)源代码进行调试时,这是一个有价值的方法;使用HTML源代码在Web开发中也是一个很好的工具。 –

+0

@ Fred-ii-空间,\ t和\ n占用网络空间,现在任何调试工具都会自动缩进,担心“干净的html”在干净的php文件中有点无用,请看一下google源代码XD;但是最后你可以放上'。“\ n”',是的。 – Blag

+0

*“任何调试工具现在自动缩进”* - 我自己不依赖于浏览器调试工具。他们很好,不要误解我的意思。但是,有人想抓取来源,这是另一回事。无论如何,我总是在所有代码中添加'“\ n”“。 –