2011-09-13 100 views
4

我有這樣的查詢:PHP/MySQL查詢幫助

$query = "SELECT gyms.name FROM `fighters_team` INNER JOIN gyms ON gyms.id = fighters_team.gym_id WHERE fighter_id = $fighter_id"; 
    if ($result = $mysqli->query($query)) { 
     while($row = $result->fetch_assoc()) { 
      echo $row['name']; 
      echo "/"; 
     } 
    } 

了就是來了:「團隊1 /團隊2 /」

我想要做的就是擺脫尾隨的在最後一項上削減。所以如果有兩個項目,就應該說「Team 1/Team 2」。

有沒有人有任何想法我可以接近?

謝謝!

回答

5
 
$query = "SELECT gyms.name FROM `fighters_team` INNER JOIN gyms ON gyms.id = fighters_team.gym_id WHERE fighter_id = $fighter_id"; 
    $names = array(); 
    if ($result = $mysqli->query($query)) { 
     while($row = $result->fetch_assoc()) { 
      array_push($names, $row['name']); 
     } 
     echo implode('/', $names); 
    } 

我希望這會奏效。

+0

謝謝,完美的作品:-) – Drew

0

嘗試:

$query = "SELECT gyms.name FROM `fighters_team` INNER JOIN gyms ON gyms.id = fighters_team.gym_id WHERE fighter_id = $fighter_id"; 
    if ($result = $mysqli->query($query)) { 
     while($row = $result->fetch_assoc()) { 
      $teams .= $row['name'].'/'.''; 
      echo rtrim($teams,'/ '); 
     } 
    } 

是你在找什麼?

+0

不幸的是,它和我在上面發佈的查詢做了同樣的事情。謝謝你。 – Drew

+0

我想你應該刪除「echo rtrim($ teams,'/');」來自while循環 –