基本上,我的数据库中有一个vid表,包含以下列-urlv、textacv、descibacv、languagev、levelv,我需要显示数据库中的所有视频(使用url-urlv),我想在显示视频的同时显示每个视频的名称(textacv)和描述(descibacve)以及一些标签(languagev、levelv)。
目前,我所取得的成就是,我有一个页面,我所有的视频都是通过url从数据库中显示出来的。以下是我如何制作的:
$conn = mysqli_connect('localhost','root','','people');
$query = "SELECT urlv, languagev,
levelv, textacv, decribacv FROM vid";
$fetch = mysqli_query($conn,$query);
$videos = array();
while ($row = mysqli_fetch_assoc($fetch)) {
$videos[] = $row['urlv'];
$language[] = $row['languagev'];
$levelv[] = $row['levelv'];
$textac[] = $row['textacv'];
$decribac[] = $row['decribacv'];
}
foreach ($videos as $urlv) {
echo "<div class='wrap'>
<div class='video-wrap'>
<div class='blockvidname'>
<div class='name'><legend class='name-top'>".
$textac."</legend></div>
<video class='video'controls>
<source src='".$urlv."'>
</video>
<div class='name-bot'><legend class='name-bottom'>
This is the name of the video</legend></div>
</div>
<div class='side-bar-wrap'> dfdfdf </div>
<button class='side-bar- button'>More Info</button>
</div>
</div>";
}
正如你所看到的,我已经使用foreach循环从数据库中检索url并显示视频,现在我需要从我的表视频中为每个视频"附加"名称、描述和一些标签。以下是我所做的:
while ($row = mysqli_fetch_assoc($fetch)) {
$videos[] = $row['urlv'];
$language[] = $row['languagev'];
$levelv[] = $row['levelv'];
$textac[] = $row['textacv'];
$decribac[] = $row['decribacv'];
}
while($row) {
echo "<div class='wrap'>
<div class='video-wrap'>
<div class='blockvidname'>
<div class='name'><legend class='name-top'>".
$textac."</legend></div>
<video class='video'controls>
<source src='".$urlv."'>
</video>
<div class='name-bot'><legend class='name-bottom'>
This is the name of the video</legend></div>
</div>
<div class='side-bar-wrap'> dfdfdf </div>
<button class='side-bar-button'>More Info</button>
</div>
</div>";
}
但它根本没有显示任何东西!我在谷歌上搜索了"foreach循环中的多个参数",但没有发现任何对我有帮助的东西。
问题是-不能在一个循环中显示来自数据库的多个数据(因为我的html结构中有嵌套,多个循环会阻止它)。注意:我想显示表中的所有视频,以及每个名称、描述等。我想根据数据库将每个视频附加到数据库中。
非常感谢!我真的被这件事卡住了。提前谢谢。
您试图将列放入自己的数组中,这有点过于复杂了。这是可以管理的,但不是必须的。只是在最初的while循环中执行所有操作。
while ($row = mysqli_fetch_assoc($fetch)) { ?>
<div class='wrap'>
<div class='video-wrap'>
<div class='blockvidname'>
<div class='name'>
<legend class='name-top'><?php echo $row['textacv'] ?></legend>
</div>
<video class='video' controls>
<source src="<?php echo $row['urlv']?>">
</video>
<div class='name-bot'>
<legend class='name-bottom'>
This is the name of the video
</legend>
</div>
</div>
<div class='side-bar-wrap'> dfdfdf </div>
<button class='side-bar-button'>More Info</button>
</div>
</div>
<?php } ?>
然后只需在回声时使用行的索引。应该是非常直接的从这里。
您可以使用函数each()
与数组$videos
一起迭代其他数组。
试试这个代码:
$conn = mysqli_connect('localhost','root','','people');
$query = "SELECT urlv, languagev,
levelv, textacv, decribacv FROM vid";
$fetch = mysqli_query($conn,$query);
$videos = array();
while ($row = mysqli_fetch_assoc($fetch)) {
$videos[] = $row['urlv'];
$language[] = $row['languagev'];
$levelv[] = $row['levelv'];
$textac[] = $row['textacv'];
$decribac[] = $row['decribacv'];
}
foreach ($videos as $urlv) {
$textacValue = each($textac);
$languageValue = each($language);
$levelvValue = each($levelv);
$decribacValue = each($decribac);
echo "<div class='wrap'>
<div class='video-wrap'>
<div class='blockvidname'>
<div class='name'><legend class='name-top'>".
$textacValue."</legend></div>
<video class='video'controls>
<source src='".$urlv."'>
</video>
<div class='name-bot'><legend class='name-bottom'>
This is the name of the video</legend></div>
</div>
<div class='side-bar-wrap'> dfdfdf </div>
<button class='side-bar- button'>More Info</button>
</div>
</div>";
}