我有一种情况,我有几个表,我通过 INNER JOIN 从中提取。存在一对多的关系,主表中每个公园都有一条线,但照片表可能有一些公园的多条线。我的代码的工作原理是为每个公园显示一张照片,但我只能显示一张。我怀疑问题出在 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'] 能给我这个循环的所有项目,但它只给了我第一个。我在这里缺少一些基本的东西。