2017-06-06 58 views
-1

我想刪除重複的一個月MySQL的刪除重複一個月

這是我的數據 enter image description here

我想刪除重複的,並顯示出這樣的

enter image description here

這是我的代碼

<?php 
    include "connect.php"; 
    $query = "select * from mydata"; 
    $result = mysqli_query($con,$query); 
    while($data = mysqli_fetch_array($result)){ 
     $dbasearray[] = $data['month_data'].",".$data['week_data'].",".$data['total_data']; 
    } 
    $s1 = array(); 
    $s2 = array(); 
    $a = array(); 
    $b = array(); 

    echo "<table border='1'> 

      <tr> 
       <th>Month</th> 
       <th>Week</th> 
       <th>Total</th> 
       <th>s1</th> 
       <th>s2</th> 
       <th>a</th> 
       <th>b</th> 
      </tr> 
      "; 
     foreach ($dbasearray as $index => $datah){ 

      $data = explode(",", $datah); 

      echo "<tr>"; 
      echo "<td align='center'>".$data[0]."</td>"; 
      echo "<td align='center'>".$data[1]."</td>"; 
      echo "<td align='center'>".$data[2]."</td>"; 
       if($index==0){ 
        echo "<td>".$s1[$index] = $data[2]."</td>"; 
        echo "<td>".$s2[$index] = $data[2]."</td>"; 
       } 
       else { 
        echo "<td>".$s1[$index] = 0.1*$data[2]+0.9*$s1[$index-1]."</td>"; 
        echo "<td>".$s2[$index] = 0.1*$s1[$index]+0.9*$s2[$index-1]."</td>"; 
       } 
       echo "<td>".$a[$index] = 2*$s1[$index]-$s2[$index]."</td>"; 
       echo "<td>".$b[$index] = (0.1/0.9)*($s1[$index]-$s2[$index])."</td>"; 
      echo "</tr>"; 
     } 
    echo "</table>"; 
    ?> 

我不知道刪除/隱藏我的代碼,我不能使用 幫助我。感謝的

+0

一旦你固定的,你只需要稍作調整,while循環 – Strawberry

+0

@Strawberry我不知道,在我的大學 –

+0

這個問題,並從模塊教育@Strawberry給我的例子,請:( –

回答

1

一個建議,嘗試打印while循環內部的東西,而不是存儲在另一個變量中,並多次迭代。完成後,

只需執行以下操作。你不想要重複的月份打印。 因此,在while循環之前添加這個。

$prev_month = ''; 

然後在while循環中需要輸入正確的月份。

if ($prev_month == $row['month']) 
    { 
     $month = ''; 
    } 
    $prev_month = $row['month']; 

在您的html打印中使用$ month。

完整的代碼寫在下面。檢查並確認它是否有效。

$month = ''; 
$prev_month = ''; 
echo "<table border='3'>"; 
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){ 
    echo "<tr>"; 
    $month = $row['month']; 

    if ($prev_month == $row['month']) 
    { 
     $month = ''; 
    } 
    $week = $row['week']; 
    $total = $row['total']; 
    $s1 = $row['s1']; 
    $s2 = $row['s2']; 
    $a = $row['a']; 
    $b = $row['b']; 

    echo "<td>"; 
    echo $month ; 
    echo "</td>"; 

    echo "<td>"; 
    echo $week ; 
    echo "</td>"; 

    echo "<td>"; 
    echo $total ; 
    echo "</td>"; 

    echo "<td>"; 
    echo $s1 ; 
    echo "</td>"; 

    echo "<td>"; 
    echo $s2 ; 
    echo "</td>"; 

    echo "<td>"; 
    echo $a ; 
    echo "</td>"; 

    echo "<td>"; 
    echo $b ; 
    echo "</td>"; 



    // Fill up the rest of fields and the print them 
    echo "</tr>"; 
    $prev_month = $row['month']; 

} 
echo "</table>"; 
+0

謝謝你的兄弟,這是工作:) –

1

應該從什麼得出我沒有PHP編碼器非常清楚,但它應該給的想法...

的樣本數據集:

DROP TABLE IF EXISTS my_table; 

CREATE TABLE my_table(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY 
,month INT NOT NULL, user VARCHAR(12) NOT NULL 
); 

INSERT INTO my_table (month,user) VALUES 
(1,'Ben'), 
(2,'Ben'), 
(3,'Tom'), 
(1,'Gary'), 
(2,'Jessica'), 
(3,'Dean'), 
(1,'Elizabeth'); 

樣品的查詢和結果:

SELECT * FROM my_table ORDER BY month,id; 
+----+-------+-----------+ 
| id | month | user  | 
+----+-------+-----------+ 
| 1 |  1 | Ben  | 
| 4 |  1 | Gary  | 
| 7 |  1 | Elizabeth | 
| 2 |  2 | Ben  | 
| 5 |  2 | Jessica | 
| 3 |  3 | Tom  | 
| 6 |  3 | Dean  | 
+----+-------+-----------+ 

樣品的PHP代碼...

<?php 

require('path./to/connection/stateme.nts'); 

$query = "SELECT month, user FROM my_table ORDER BY month , id;"; 

$result = mysqli_query($conn,$query); 

$x = null; 

echo "<table>"; 
while($row = mysqli_fetch_assoc($result)){ 
if($row['month']==$x) 
    { $month = ''; } 
    else 
    { $month = $row['month'];} 

echo "<tr><td>$month</td><td>".$row['user']."</td></tr>"; 

$x = $row['month']; 
} 
echo "</table>"; 
?> 

輸出:

1 Ben 
    Gary 
    Elizabeth 
2 Ben 
    Jessica 
3 Tom 
    Dean 
1
<?php 
include "koneksi.php"; 
$chkArry=array(); 
$query = "select * from mydata"; 
$result = mysql_query($query); 
while($data = mysql_fetch_array($result)){ 
    $dbasearray[] = $data['month_data'].",".$data['week_data'].",".$data['total_data']; 
} 
$s1 = array(); 
$s2 = array(); 
$a = array(); 
$b = array(); 

echo "<table border='1'> 

     <tr> 
      <th>Month</th> 
      <th>Week</th> 
      <th>Total</th> 
      <th>s1</th> 
      <th>s2</th> 
      <th>a</th> 
      <th>b</th> 
     </tr> 
     "; 
    foreach ($dbasearray as $index => $datah){ 

     $data = explode(",", $datah); 
     if(in_array($data[0], $chkArry)){ 
      $data[0]=''; 
     } 
     else 
     { 
      array_push($chkArry, $data[0]); 
     } 
     echo "<tr>"; 
     echo "<td align='center'>".$data[0]."</td>"; 
     echo "<td align='center'>".$data[1]."</td>"; 
     echo "<td align='center'>".$data[2]."</td>"; 
      if($index==0){ 
       echo "<td>".$s1[$index] = $data[2]."</td>"; 
       echo "<td>".$s2[$index] = $data[2]."</td>"; 
      } 
      else { 
       echo "<td>".$s1[$index] = 0.1*$data[2]+0.9*$s1[$index-1]."</td>"; 
       echo "<td>".$s2[$index] = 0.1*$s1[$index]+0.9*$s2[$index-1]."</td>"; 
      } 
      echo "<td>".$a[$index] = 2*$s1[$index]-$s2[$index]."</td>"; 
      echo "<td>".$b[$index] = (0.1/0.9)*($s1[$index]-$s2[$index])."</td>"; 
     echo "</tr>"; 
    } 
echo "</table>"; 
?> 

嘗試上面的代碼,讓我知道,如果問題仍然存在。