PHP/MySQL Query Help
I have a query like this:
$query开发者_JAVA百科 = "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 " / ";
}
}
What comes up is: "Team 1 / Team 2 /"
What I want to do is get rid of the trailing slash on the last item. So it should just say "Team 1 / Team 2" if there are two items.
Does anyone have any idea how I could approach that?
Thanks!
$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); }
I hope this would work.
Try:
$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,'/ ');
}
}
Is that what you are looking for?
精彩评论