I've just run into a problem, when pulling location data using a link table.
There can be two entries in the link table, like so:
item_id | contentent | country | state
-------------------------------------
123 | 1 | 2 | 20
123 | 1 | 2 | 21
When I echo these out (using the name, not the id), I'll get
location:
north america... USA... missouri
north america... USA... montana
I want to echo out my results as:
north america... USA... missouri, montana
then it can loop through if it is another continent. Same thing for countries. I might have
north america... USA... missouri, montana
Mexico... Tamaulipas, oaxaca
Does anybody know how I might do this?
Right now, I have this:
<?php
require ('databaseconnection.php');
$id = $_GET['id'];
$result = mysql_query("SELECT DISTINCT continents.Name AS continent1,continent_regions.Name,country.Name AS country1, states.Name AS state1, plant_locations_link.plant_id
FROM plant_locations_link
JOIN continents ON plant_locations_link.continents = continents.ID
JOIN continent_regions ON plant_locations_link.continent_regions = continent_regions.ID
JOIN country ON plant_locations_link.country = country.ID
JOIN states ON plant_locations_link.state = states.ID
WHERE plant_locations_link.plant_id = $id");
if (!$result) {
die(mysql_error());
}
while ($row = mysql_fetch_assoc($result)) {
?>
<?php if ($row['continent1']!=""){ ?>
<?php echo $row['continent1']; ?><?php } ?>
<?php if ($row['continentR1']!=""){ ?> ...
<?php echo $row['continentR1']; ?><?php } ?>
<?php if ($row['country1']!=""){ ?>...
<?php echo $row['country1']; ?><?php } ?>
<?php if ($row['state1']!=""){ ?>...
<?php echo $row['state1']; ?><?php echo "</br>"; } ?>
<?php
}
?>