I have 2 database tables like so:
CREATE TABLE IF NOT EXISTS `banners` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`long_name` varchar(50) NOT NULL,
`visible` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;
And:
CREATE TABLE IF NOT EXISTS `locations` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`banner_id` int(2) NOT NULL,
`district_id` int(2) NOT NULL,
`number` int(5) NOT NULL,
`location` varchar(50) NOT NULL,
`visible` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=511 ;
I use the code below to pull a list of locations based on their district_id and create a drop-down menu:
$query = 'SELECT id, banner_id, location FROM locations WHERE district_id =' . $district_id;
$result = mysql_query($query, $connection);
if (!result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['id'] . '"';
if($row['id'] == $location) { echo ' selected';} ;
echo '>' . $row['location'] . '</option>';
}
echo '</select>';
But I'd also like to add to the list the name
from the banners
table that matches the banner_id
for each location. So I can echo out $row['name']
with each $row['location']
:
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['id'] . '"';
if($row['id'] == $location) { echo ' selected';} ;
echo '>' . $row['name'] . ' ' . $row['location'] . '</option>';
}