gpt4 book ai didi

php - 尝试从数据库中抓取多个随机视频,但它始终只显示同一个视频

转载 作者:行者123 更新时间:2023-11-29 14:55:35 24 4
gpt4 key购买 nike

我正在尝试从我的数据库中抓取几个随机视频,并将它们显示在页面上。然而,它只是继续抓取相同的视频,而不是几个不同的视频。所以我最终得到了 5 个相同的视频,而不是 5 个不同的视频。

这是抓取随机 PHP 代码文件...后面是模板输出文件。

//=====================================================
// Random | Previous | Next
//=====================================================
$show['random'] = $db->quick_fetch(
"SELECT file_id, title, title_seo, category_id, thumb FROM files
WHERE files.category_id = '".$show['main']['category_id']."'
AND files.verified=1 ORDER BY RAND() LIMIT 0,1;
");

这是模板 CSS html 代码,我已将其粘贴 5 次以显示 5 个随机视频

            <td  valign="top" width="53%"><?

$sql="select * from files ORDER BY rand() limit 0,5"; $res=@mysql_query($sql); $data=@mysql_result($res,0,'filename'); $id=@mysql_result($res,0,'file_id'); $title=@mysql_result($res,0,'title'); $title2=str_replace(" ", "-",$title); $path="{$siteurl}/media/{$file.random.file_id}/{$file.random.title_seo}/"; $img="{$siteurl}/thumbs/{$file.random.thumb}"; echo "
{$file.random.title}"

最佳答案

您不能只粘贴 5 次。您还必须运行查询 5 次。但这也不是正确的方法。您应该运行一次查询,并将 LIMIT 0,1 更改为 LIMIT 0,5 或仅 LIMIT 5。然后循环 smarty 模板中的 5 个随机结果。

以下是如何在 smarty 中循环关联数组(又名 CSS html 代码):

http://www.smarty.net/docsv2/en/language.function.section.tpl

编辑

好吧,这段代码看起来并没有好多少。让我们把它分解一下。您是否在 phpmyadmin 或类似工具中运行查询以确保它返回您想要的内容?首先执行此操作,确保查询良好。

第二,你用的是 smarty 吗?我只是猜测,因为它看起来就是这样。

第三,确保您发送给 smarty 的数组格式正确,并且包含您需要的所有数据。在 PHP 端,在分配变量之前执行 print_r

最后,这里有一些伪代码,告诉您这应该如何工作(如果它实际上很聪明的话):

Run the query

Loop through the results, building an associative array of the data you want to send to smarty
(print it out and make sure its correct, for debugging purposes)

Assign the created array to a variable available to the smarty template

In the smarty template, use the section (loop) code and loop over the array of results to display it.

关于php - 尝试从数据库中抓取多个随机视频,但它始终只显示同一个视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4709662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com