将所有图像连接到它们的相关页面
Join all the images to their relative page
我在 mysql 数据库中有两个表:页面和图像(目前,我更喜欢“图像”是文本)。我需要每一页都显示他们所有的相关图像。例如:第一页显示 3 个图像,第二页显示 4 个图像,第三页没有图像等。为了做到这一点,我创建了(遵循基本的 cms 教程
https://www.youtube.com/watch?v=UbsAdx58ch0&list=PLfdtiltiRHWF0O8kS5D_3-nTzsFiPMOfM)
两个表到一个数据库中:
Pages:
id,label,title,body,slug,created,updated
Images:
id,page_id,content (for now, I prefer that the “image” is text)
不幸的是,我的查询只能显示一张图片,而不是与页面相关的所有图片,我不知道为什么。这里是 page.php:
的查询
require 'app/start.php';
if (empty($_GET['page'])) {
$page = false;
} else {
$slug = $_GET['page'];
$page = $db->prepare("
SELECT pages.id, pages.label, pages.title, pages.body, pages.slug, pages.created, pages.updated, images.id, images.page_id, images.content AS image_content
FROM pages
LEFT JOIN images
ON pages.id = images.page_id
WHERE slug = :slug
");
$page->execute(['slug' => $slug]);
$page = $page->fetch(PDO::FETCH_ASSOC);
var_dump($page);
if ($page) {
$page['created'] = new DateTime($page['created']);
if ($page['updated']) {
$page['updated'] = new DateTime($page['updated']);
}
}
}
require VIEW_ROOT . '/page/show.php';
此查询生成以下源,相对于例如第一页:
array(9) {
["id"]=>
string(1) "1"
["label"]=>
string(20) "This is the first page"
["title"]=>
string(22) "This is the first page"
["body"]=>
string(8) "Some text"
["slug"]=>
string(10) "first-page"
["created"]=>
string(19) "2015-10-05 10:55:54"
["updated"]=>
string(19) "2015-10-05 10:55:54"
["page_id"]=>
string(1) "1"
["image_content"]=>
string(8) "groundhog"
}
你怎么看,image_content 只显示了一个结果,但是图像表的 page_id 记录了多张链接到页面 ID 的图像。我哪里错了?
然后我尝试了这个解决方案:
<?php
require 'app/start.php';
if (empty($_GET['page'])) {
$page = false;
} else {
$slug = $_GET['page'];
$page = $db->prepare("
SELECT pages.id, pages.label, pages.title, pages.body, pages.slug, pages.created, pages.updated, images.id, images.page_id, images.content AS image_content
FROM pages
LEFT JOIN images
ON pages.id = images.page_id
WHERE slug = :slug
");
$page->execute(['slug' => $slug]);
$page = $page->fetchAll(PDO::FETCH_ASSOC);
foreach ($page as $key) {
$select[] = array('created'=>$key['created'], 'updated'=>$key['updated'], 'image_content'=>$key['image_content'], 'title'=>$key['title'], 'body'=>$key['body']);
}
var_dump($key['image_content']);
if ($page) {
$key['created'] = new DateTime($key['created']);
if ($key['updated']) {
$key['updated'] = new DateTime($key['updated']);
}
}
}
require VIEW_ROOT . '/page/show.php';
现在我离我的目标更近了一点,但是使用这段代码,我得到了相同名称的图像,重复次数为页面 ID 在 page_id 列中重复的次数。
所以第 1 页有 4 张图片,最终结果为:"horse horse horse horse"。
第 2 页有 3 张图片,因此:"cat cat cat"
你在这儿 show.php:
<?php require VIEW_ROOT .'/templates/header.php'; ?>
<?php if (!$page): ?>
<p>No page found, sorry.</p>
<?php else: ?>
<h2><?php echo e($key['title']); ?></h2>
<p><?php echo e($key['body']); ?></p>
<p class="faded">
Created on <?php echo e(($key['created']->format('jS M Y'))); ?>
<?php if ($key['updated']): ?>
last updated <?php echo e(($key['updated']->format('jS M Y h:i a'))); ?>
<?php endif; ?>
<br/>
Images:
<?php foreach($select as $result): ?>
<?php echo e($key['image_content']); ?><br/>
<?php endforeach; ?>
</p>
<?php endif;?>
<?php require VIEW_ROOT .'/templates/footer.php'; ?>
您只是 fetching one row of your result. If you want to acces the whole result of your SQL-Query you either have to fetch the result with fetchAll 或在循环遍历结果时单独处理每一行。
使用fetchAll:
$page_array = $page->fetchAll(PDO::FETCH_ASSOC);
使用循环:
while($page_row = $page->fetch(PDO::FETCH_ASSOC))
{ //do stuff }
编辑:
如果您在 show.php 中更改循环,它应该可以工作。
<?php foreach($page as $result): ?>
<?php echo e($result['image_content']); ?><br/>
<?php endforeach; ?>
fetchAll:
fetchAll returns 多维关联数组。对于查询结果中的每一行,都会将另一行添加到数组中。如果您的查询 returns 3 行,您可以通过 $page[0]['variables'] 到 $page[2][variables] 访问它们。参见 here。
关键元素
我会考虑在您的 foreach 循环之后使用最后一个键数组,这是非常糟糕的做法。请参阅 manual-page.
上的第一个警告
我在 mysql 数据库中有两个表:页面和图像(目前,我更喜欢“图像”是文本)。我需要每一页都显示他们所有的相关图像。例如:第一页显示 3 个图像,第二页显示 4 个图像,第三页没有图像等。为了做到这一点,我创建了(遵循基本的 cms 教程
https://www.youtube.com/watch?v=UbsAdx58ch0&list=PLfdtiltiRHWF0O8kS5D_3-nTzsFiPMOfM)
两个表到一个数据库中:
Pages:
id,label,title,body,slug,created,updated
Images:
id,page_id,content (for now, I prefer that the “image” is text)
不幸的是,我的查询只能显示一张图片,而不是与页面相关的所有图片,我不知道为什么。这里是 page.php:
的查询require 'app/start.php';
if (empty($_GET['page'])) {
$page = false;
} else {
$slug = $_GET['page'];
$page = $db->prepare("
SELECT pages.id, pages.label, pages.title, pages.body, pages.slug, pages.created, pages.updated, images.id, images.page_id, images.content AS image_content
FROM pages
LEFT JOIN images
ON pages.id = images.page_id
WHERE slug = :slug
");
$page->execute(['slug' => $slug]);
$page = $page->fetch(PDO::FETCH_ASSOC);
var_dump($page);
if ($page) {
$page['created'] = new DateTime($page['created']);
if ($page['updated']) {
$page['updated'] = new DateTime($page['updated']);
}
}
}
require VIEW_ROOT . '/page/show.php';
此查询生成以下源,相对于例如第一页:
array(9) {
["id"]=>
string(1) "1"
["label"]=>
string(20) "This is the first page"
["title"]=>
string(22) "This is the first page"
["body"]=>
string(8) "Some text"
["slug"]=>
string(10) "first-page"
["created"]=>
string(19) "2015-10-05 10:55:54"
["updated"]=>
string(19) "2015-10-05 10:55:54"
["page_id"]=>
string(1) "1"
["image_content"]=>
string(8) "groundhog"
}
你怎么看,image_content 只显示了一个结果,但是图像表的 page_id 记录了多张链接到页面 ID 的图像。我哪里错了?
然后我尝试了这个解决方案:
<?php
require 'app/start.php';
if (empty($_GET['page'])) {
$page = false;
} else {
$slug = $_GET['page'];
$page = $db->prepare("
SELECT pages.id, pages.label, pages.title, pages.body, pages.slug, pages.created, pages.updated, images.id, images.page_id, images.content AS image_content
FROM pages
LEFT JOIN images
ON pages.id = images.page_id
WHERE slug = :slug
");
$page->execute(['slug' => $slug]);
$page = $page->fetchAll(PDO::FETCH_ASSOC);
foreach ($page as $key) {
$select[] = array('created'=>$key['created'], 'updated'=>$key['updated'], 'image_content'=>$key['image_content'], 'title'=>$key['title'], 'body'=>$key['body']);
}
var_dump($key['image_content']);
if ($page) {
$key['created'] = new DateTime($key['created']);
if ($key['updated']) {
$key['updated'] = new DateTime($key['updated']);
}
}
}
require VIEW_ROOT . '/page/show.php';
现在我离我的目标更近了一点,但是使用这段代码,我得到了相同名称的图像,重复次数为页面 ID 在 page_id 列中重复的次数。 所以第 1 页有 4 张图片,最终结果为:"horse horse horse horse"。 第 2 页有 3 张图片,因此:"cat cat cat"
你在这儿 show.php:
<?php require VIEW_ROOT .'/templates/header.php'; ?>
<?php if (!$page): ?>
<p>No page found, sorry.</p>
<?php else: ?>
<h2><?php echo e($key['title']); ?></h2>
<p><?php echo e($key['body']); ?></p>
<p class="faded">
Created on <?php echo e(($key['created']->format('jS M Y'))); ?>
<?php if ($key['updated']): ?>
last updated <?php echo e(($key['updated']->format('jS M Y h:i a'))); ?>
<?php endif; ?>
<br/>
Images:
<?php foreach($select as $result): ?>
<?php echo e($key['image_content']); ?><br/>
<?php endforeach; ?>
</p>
<?php endif;?>
<?php require VIEW_ROOT .'/templates/footer.php'; ?>
您只是 fetching one row of your result. If you want to acces the whole result of your SQL-Query you either have to fetch the result with fetchAll 或在循环遍历结果时单独处理每一行。
使用fetchAll:
$page_array = $page->fetchAll(PDO::FETCH_ASSOC);
使用循环:
while($page_row = $page->fetch(PDO::FETCH_ASSOC)) { //do stuff }
编辑:
如果您在 show.php 中更改循环,它应该可以工作。
<?php foreach($page as $result): ?>
<?php echo e($result['image_content']); ?><br/>
<?php endforeach; ?>
fetchAll:
fetchAll returns 多维关联数组。对于查询结果中的每一行,都会将另一行添加到数组中。如果您的查询 returns 3 行,您可以通过 $page[0]['variables'] 到 $page[2][variables] 访问它们。参见 here。
关键元素
我会考虑在您的 foreach 循环之后使用最后一个键数组,这是非常糟糕的做法。请参阅 manual-page.
上的第一个警告