簡體   English   中英

如何將兩個MySQL行合並為一個並使用PHP在表中顯示它們?

[英]How do I combine two MySQL rows into one and display them in a table using PHP?

我有一張桌子“exercise_results”。 人們在開始時投入他們的結果,然后在兩個月之后將他們的結果放在他們的結果中,看看他們有多少改進。 它們的開始集的exercise_type_id為“1”,而結束集的exercise_type_id為“2”。

我需要一種方法將其顯示到HTML表中,如下所示:

通常我會這樣做

一個foreach循環,但這是單行。 我在將兩行合並為一行時遇到了麻煩。 我想這可能就像某種MySQL加入一樣簡單? 我們通過person_unique_id識別每個人。

這是我的領域:

id | person_unique_id | person_name | exercise_type_id | mile_running_time | bench_press_weight_lbs | squat_weight_lbs | date_of_exercise_performed

示例行:

1 | J123 | John Smith | 1 | 8  | 200 | 300 | 2010-03-20
2 | J123 | John Smith | 2 | 7  | 250 | 400 | 2010-05-20
3 | X584 | Jane Doe   | 1 | 10 | 100 | 200 | 2010-03-20
4 | X584 | Jane Doe   | 2 | 8  | 150 | 220 | 2010-05-20

我嘗試了一些解決方案,但我迷路了。 任何幫助都會很棒。 謝謝!

編輯:

在回應下面的評論時,我希望得到一些數據:

array  0 => 
array
  'Exercise' => 
    array
      'person_unique_id' => string 'J123'
      'person_name' => string 'John Smith'
      'begin_mile_running_time' => string '8'
      'end_mile_running_time' => string '7'
1 => 
array
  'Exercise' => 
    array
      'person_unique_id' => string 'X584'
      'person_name' => string 'Jane Doe'
      'begin_mile_running_time' => string '10'
      'end_mile_running_time' => string '8'

您可以使用GROUP_CONCAT()來獲得兩行結果,如下所示:

SELECT person_unique_id, person_name, 
       group_concat( mile_running_time ) AS miles,  
       group_concat( bench_press_weight_lbs ) AS bench, 
       GROUP_CONCAT( squat_weight_lbs ) AS squat
FROM exercise_result
GROUP BY person_unique_id

你的結果將是:

J123  |  John Smith  |  8,7  |  200,250  |  300,400

X584  |  Jane Doe  |  10,8  |  100,150  |  200,220

然后你可以使用php explode和結果字段來獲得每種類型的結果。

$query = mysql_query("select ...your data");

 while ($row = mysql_fetch_assoc ($query) ) {
    if ($row['exercise_type_id']==1)  
      $exe1[]=$row;
   else  
      $exe2[]=$row;


 }

 print_r($exe1);
 print_r($exe2);

從我的理解

編輯:

嘗試這個

$query = mysql_query("select ...your data");

 while ($row = mysql_fetch_assoc ($query) ) {

   $rows[]=array('Exercise'=>$row);


 }

 print_r($rows);

提取整個表格,或者對您感興趣的行,對人員ID進行排序,將每行的人員ID與下一行進行比較,以查看是否有針對HTML表格中所有列打印的結果。 如果沒有,跳到下一個並將字段留空(或其他一些解決方案,可能會忽略那些沒有填寫這兩個字段的人?)。

我的PHP技能有限,所以沒有代碼示例。

如果您在person_unique_id exercise_type_id上訂購,則可以執行此操作。 如果每個人都有兩行,則可以省略if(僅使用else):

for( $i = 0; $i < count($exercise_results); $i++ )
{
  $first = $exercise_results[$i];
  if( !isset($exercise_results[$i+1])
      || $first['person_unique_id'] != $exercise_results[$i+1]['person_unique_id' ) {
    $second = array(
      'person_name'=>$other['person_name'],
      'mile_running_time' => null // fill in the rest with defaults (like null)
    );
  } else {
    $second = $exercise_results[$i+1];
    $i++; // skip ahead one, since you've already used the second result.
  }
  // perform your normal printing, but use $beginning and $end to get the respective results
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM