开发者

Trying to grab several random videos from database, but it just shows the same one all the time

开发者 https://www.devze.com 2023-02-04 20:12 出处:网络
I\'m trying to grab several random videos from my database, and have them show on a page. However, it just keeps on grabbing the same video rather than several different ones. So I end up with 5 of th

I'm trying to grab several random videos from my database, and have them show on a page. However, it just keeps on grabbing the same video rather than several different ones. So I end up with 5 of the same video rather than 5 different ones.

Here's the grab random PHP code file... and after it is the template output file.

//=====================================================
// 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;
");

Here's the template CSS html thingy code I have this pasted 5 times to show 5 random videos

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

$sql="select * from files ORDER BY rand() limit 0,5"; $res=@mysql_query($sql); $data=@mysql_result($re开发者_StackOverflow中文版s,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}"


You can't just paste it 5 times. You also have to run the query 5 times. But that's not the right way to do it either. You should run the query once, and change the LIMIT 0,1 to LIMIT 0,5 or just LIMIT 5. Then loop over the 5 random results in the smarty template.

Heres how you loop over an associative array in smarty (aka CSS html thingy code):

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

Edit

Ok, that code doesn't look much better. lets break it down a bit. Have you run your query inside phpmyadmin or some such tool to make sure its returning what you want? Do that first, make sure the query is good.

Second, are you using smarty even? I just guessed because that's what it looked like.

Third, make sure the array you are sending to smarty is properly formed and has all the data you need in it. on the PHP side do a print_r before you assign the variable.

Finally, here is some pseudo code, to tell you how this should work, if it is in fact smarty:

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.


As a general rule you should try to avoid ORDER BY and RAND() in SQL statements because they are inefficient ways of sorting and randomizing results.

You are better off getting all 5 results and randomly selecting them using something like array_rand().

Take care.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号