php - 如何通过 PHP 从 SQL 调用同一组的多个图像
问题描述
发布编辑:
多亏了 drtechno 的代码片段、ADyson 和 Jhecht 的主要帮助,我想通了。我是愚蠢的,没有他们我不会明白为什么。
这是现在的代码
<?php
require_once("php/connect.php");
require_once('php/db.php');
$contID = $_GET['contentID'];
$sql = "SELECT imageURL,altText FROM images WHERE imageGroup='$contID' ";
$result = mysqli_query($con, $sql) or die ("Bad Query: $sql");
?>
再加上drtechno的代码片段
<?php
echo'<div id="myCarousel" class="carousel slide" data-ride="carousel">';
echo '<div class="carousel-inner">';
$a=0;
while ($l = $result->fetch_row()) {
// on the first one, make active first, then others fallow
if ($a=="0"){
echo '<div class="carousel-item active">';
}else{
echo '<div class="carousel-item">';
}
for ($cont=0; $cont<$result->field_count; $cont++) {
if ($cont=="0"){
$dbimg="";
// now we shift our 1st column data into a temporary variable
$dbimg=$l[$cont];
}else{
// load the 1 column variable, while getting the 2nd
echo '<img src="'.$dbimg.'" alt="'.$l[$cont].'">';
}
}
echo '</div>';
$a=$a+1;
}
echo '
</div>
</div>';
?>
上面的两个片段都在 article.php 模板页面中,我还有一个 db.php 包含,它正在获取我的 contentID 以传递给 $contID。这是下面
<?php
if(isset($_GET['contentID'])){
require_once('connect.php');
$id = mysqli_real_escape_string($con, $_GET['contentID']);
$sql = "SELECT * FROM content WHERE contentID='$id'";
$result = mysqli_query($con, $sql) or die ("Bad Query: $sql");
$row = mysqli_fetch_array($result);
}else {header('Location: index.php');}
?>
再次感谢,我希望这对其他人也有帮助。
预编辑:
正如标题所述,我正在尝试通过 php 从 sql 数据库中调用多个图像。我需要分组的图像填充与文章页面相关的 BootStrap (BS) 轮播。
即我有 12 篇文章,每篇文章点击打开一篇文章/页面,其中包含 php 分页数据,这些页面每个都应该包含 3 个(不同的)图像,这些图像应该显示在 BS 轮播中。所以总共有 12 个图像组,每组有 3 个图像。
到目前为止,我只设法使用多个 SQL 查询来做到这一点,而且看起来很混乱而且冗长。
查询如下
<?php
require_once('db.php');
require_once("connect.php");
$img1Result = mysqli_query($con, "SELECT imageRef FROM images WHERE imageID = 1 AND contentID = 1");
$img2Result = mysqli_query($con, "SELECT imageRef FROM images WHERE imageID = 2 AND contentID = 1");
$img3Result = mysqli_query($con, "SELECT imageRef FROM images WHERE imageID = 3 AND contentID = 1");
$img4Result = mysqli_query($con, "SELECT imageRef FROM images WHERE imageID = 4 AND contentID = 2");
$img5Result = mysqli_query($con, "SELECT imageRef FROM images WHERE imageID = 5 AND contentID = 2");
$img6Result = mysqli_query($con, "SELECT imageRef FROM images WHERE imageID = 6 AND contentID = 2");
$result = mysqli_query($con, $sql) or die ("Bad Query: $sql");
?>
我使用以下方法获取数据
if(basename($_SERVER['REQUEST_URI']) == 'article.php?contentID=1'){
while($row = mysqli_fetch_assoc($img1Result)){
echo "
<div class='carousel-item active'>
<img src='{$row['imageRef']}' class='img-responsive' alt='alt'>
</div>";
}
while($row = mysqli_fetch_assoc($img2Result)){
echo "
<div class='carousel-item'>
<img src='{$row['imageRef']}' class='img-responsive' alt='alt'>
</div>";
}
while($row = mysqli_fetch_assoc($img3Result)){
echo "
<div class='carousel-item'>
<img src='{$row['imageRef']}' class='img-responsive' alt='alt'>
</div>";
}
}
该if
语句检查从前一页上的链接单击时创建了哪个页面并加载与该文章相关的图像。
我也尝试过使用这样的连接(但知识有限)
<?php
require_once('db.php');
require_once("connect.php");
$sql = "SELECT content.contentID,content.title,content.description, images.imageURL, images.imageGroup, images.altText
FROM content
INNER JOIN images ON content.contentID=images.imageGroup
";
$result = mysqli_query($con, $sql) or die ("Bad Query: $sql");
?>
但不知道我将如何调用以访问每篇文章的分组图像。
我当前的迭代使用 3 个表:用户、内容和图像,请参见下面的屏幕截图。
图像表的屏幕截图目前只有 2 个组,但这将增长到 12 个,我只是将其作为视觉辅助。
任何帮助将不胜感激,如果答案是 RTFM,您能否向我提供相关知识源的链接和/或向我解释我实际上正在尝试做什么,因为我真的不确定。我也很难使用源文档来学习,作为一个菜鸟,我发现如果我通过问题/解决方案来学习,我会学得更快。
理想情况下(并且希望)有人会足够虔诚地提供代码示例和解释来帮助我。
提前致谢。
扭曲的
解决方案
好吧,它形成了您的查询,看来您需要帮助。
所以让我们看一下图像表,你有一个用于识别女巫的列和要放入你的 img 标签的 alt 文本。
因此,如果您从 index.html 页面构建您的页面,如 article1.php、article2.php.. 等,您可以只调用该文章的图片组。
像这样:
$img1qry = "SELECT imageURL,altText FROM images WHERE imageGroup = 1";
$result = $con->query($img1qry)or die (mysqli_error($con));
现在,我们不是一个一个地取出每个个体,而是循环输出,因为现在我们有更多对象,并且我们构建轮播,因为数据库的输出返回时使用浮动变量来控制轮播对象构建。像这样:
<?php
echo'<div id="myCarousel" class="carousel slide" data-ride="carousel">';
echo '<div class="carousel-inner">';
$a=0;
while ($l = $result->fetch_row()) {
// on the first one, make active first, then others fallow
if ($a=="0"){
echo '<div class="carousel-item active">';
}else{
echo '<div class="carousel-item">';
}
for ($c=0; $c<$result->field_count; $c++) {
if ($c=="0"){
$dbimg="";
// now we shift our 1st column data into a temporary variable
$dbimg=$l[$c];
}else{
// load the 1 column variable, while getting the 2nd
echo '<img src="'.$dbimg.'" alt="'.$l[$c].'">';
}
}
echo '</div>';
$a=$a+1;
}
echo '</div></div>';
推荐阅读
- c# - 将浮点变量设置为数组值
- vba - 尝试打开 ppt 文件时出错 & 其他几个疑问 (powerpoint)
- powershell - psql:使用 powershell 变量查询失败
- selenium - 如何找到一个复选框并知道它在表格单元格中的选中状态
- python - 无法使用 Python 读取 .cdb 文件
- azure - 微软博览会 OAuth 登录
- javascript - 将 1 天添加到日期选择器中的选定日期
- css - 如何在 Wordpress 的 Gutenberg 块中设置列表样式
- python - django 模板继承:如何不显示来自父模板的块?
- .net - 使用 .NET 以编程方式运行流分析转换查询