INNER JOIN を使用してプルしているテーブルがいくつかある状況があります。1 対多の関係があり、メイン テーブルには公園ごとに 1 つの行がありますが、写真テーブルには公園によっては複数の行がある場合があります。私のコードは、公園ごとに写真が表示されているという点で機能しますが、表示できるのは 1 つだけです。問題は foreach ループにあると思われますが、少し困惑しています。コードは次のとおりです。
try
{
$sql = 'SELECT parks.id, parks.state, parks.name, parks.description, parks.site, parks.sname, parks.street, parks.city, parks.zip, parks.phone, comments.comment, comments.commentname, events.event, events.date, events.description2, photos.parkid, photos.type, photos.filename, photos.big FROM parks
INNER JOIN comments INNER JOIN photos INNER JOIN events ON parks.parkid = comments.parkid and parks.parkid = photos.parkid and parks.parkid = events.parkid
GROUP BY parks.id
ORDER BY parks.name asc';
$result = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching data: ' . $e->getMessage();
include 'output.html.php';
exit();
}
//This is pulling the information from the database for display. On the foreach it will display each
//line until there are no more lines to display.
foreach ($result as $row)
{
$datas[] = array ('id' =>$row['id'],
'parkid' =>$row['parkid'],
'state' =>$row['state'],
'name' =>$row['name'],
'description' =>$row['description'],
'site' =>$row['site'],
'sname' =>$row['sname'],
'street' =>$row['street'],
'city' =>$row['city'],
'phone' =>$row['phone'],
'zip' =>$row['zip'],
'commentname' =>$row['commentname'],
'comment' =>$row['comment'],
'event' =>$row['event'],
'date' =>$row['date'],
'description2' =>$row['description2'],
'type' =>$row['type'],
'filename' =>$row['filename'],
'big' =>$row['big']);
}
include 'writing.html.php';
と
<?php
foreach ($datas as $name)
{
if ($name['state'] === 'PA')
{
?>
<a href="#header" title="return to the top of the page">Back to top</a>
<input type="hidden" name="id" value="' . $name['id'] . '" />
<h1 id="name"> <?php echo ($name['name']) ?> </h1>
<p id="descriptionlist">
<?php echo ($name['description']) ?>
<br />
<ul id="link">
<li class="l1">
<a href=<?php echo $name['site'] ?> target="_blank"> <?php echo $name['sname'] ?> </a>
</li>
</ul>
</p>
<h2>Location</h2>
<div class = "loc">
<p class="loct">
<a class = "fancyImg" href="maps/<?php echo $name['id'] ?>state.gif"> <img src= "maps/<?php echo $name['id'] ?>state.gif"> </a>
<br />
<php echo ($name['street']) . ?>
<br />
<?php echo ($name['city']) .
($name['state']) .
($name['zip']) ?>
<br>
<?php echo ($name['phone']) ?>
<br> <br>
</p>
</div>
<h2>Trail Map</h2>
<div class = "map">
<p class = "mapt">
Click to Enlarge
<a class ="fancyImg" href= "/maps/<?php echo $name['id'] ?>maplink.gif">
<img src= "/maps/<?php echo $name['id'] ?>.gif"></a> <br> <br>
</p>
</div>
<h2>Photos</h2>
<div class = "pho">
<p class = "phot">
<a class = "fancyImg" href= "/assets/indiv/<?php echo $name['big'] ?>.gif">
<img src= "<?php echo $name['filename'] ?>.gif"></a>**
Submit <i>your</i> photos of <?php echo ($name['name']) ?> through our <ul id = "link"><li><a href="https://www.facebook.com/Ride4Wheel">Facebook Page!</li></ul></a></h3><p> Or go to our Contact Us page for information on how to e-mail us your favorite pictures!
</p>
</div>
当面の問題は、ここの最後の pho div にあります。$name['big'] でこのループのすべてのアイテムが得られることを期待していましたが、最初のアイテムしか得られません。ここで基本的なことが欠けています。