开发者

How to display the random images without repetition from database(MySql) using php

开发者 https://www.devze.com 2023-01-05 06:48 出处:网络
I have a program in php that should di开发者_如何转开发splay the random images without repetition,I was able to store the images in the database, but I don\'t have any idea to do this can anyone help

I have a program in php that should di开发者_如何转开发splay the random images without repetition,I was able to store the images in the database, but I don't have any idea to do this can anyone help me with this.

Thank you,


Save which ones have been shown in $_SESSION and just construct the query to select one which is not among them?

ex.

session_start();
$result = mysql_query("Select * from images WHERE id NOT IN (".implode(',',$_SESSION['used']).") LIMIT 1;");
$result = mysql_fetch_array($result);
$_SESSION['used'][] = $result['id'];
// show image


Robus was close, but what about obtaining that first image? That code would error when $_SESSION['used'] is not set / empty.

rand() may be slow, but definitely a valid option for a smaller database.

Assuming the ID's in the database are sequential without gaps, the last inserted ID could be pulled and used as the max of a rand(1, max) to produce a random image up front (without using rand()).

Shruti, when you say you're storing the images in the database, are they actually IN the database, or just a record of the image? If you happen to have the images in a single directory (not really advisable, but that's for another post) you could run scandir() against the directory, and then array_rand() to get a random image (zero database overhead, but be careful about having a single directory overloaded with files and be careful to skip '.' and '..' when pulling the random entry)


function get_items_by_params($quant,$mode="props",$prop="",$pv="")
{
    $DB = new MySQLTable;
    $DB->TblName = 'shop_goods';
    $where['good_switch']['='] = 'on';
    $cr = $DB->Select('COUNT(*)', $where);
    if($cr !== false)
    {
        if(mysql_num_rows($cr)>0)
        {
            $row_count = mysql_result($cr,0);
            if($row_count>$quant)
            {
                $rand_vals = array();
                for($i = 0; $i<$quant; $i++)
                {
                   $rv = mt_rand(0,$cr-1);
                   $try = 0;
                   while(in_array($rv,$rand_vals))
                   {
                       $rv = mt_rand(1,$row_count);
                       $try++;
                       if($try>$quant+1000)
                       {
                           break;
                       }
                   }
                   $rand_vals[] = $rv;
                }
            } else {
                return false;
            }

            $query = array();
            foreach($rand_vals as $random_row)
            {
              $query[] = '(SELECT `good_id`,`good_img`,`good_name`,`good_props` FROM `shop_goods` WHERE `good_switch` = "on" LIMIT '.$random_row.', 1)';
            }
            $query = implode(' UNION ', $query);
            $res = $DB->SimpleQuery($query);
            $out = array();
            if($res !== false)
            {
                while($row = mysql_fetch_array($res))
                {
                    $out[] = array($row['good_id'],$row['good_img']);
                }
                return $out;
            }
        }
    }
    return false;
}

This function uses my own mysql-work class. But there are queries:
$cr = $DB->Select('COUNT(*)', $where);//SELECT COUNT(*) FROM shop_goods WHERE good_switch = 'on'
$res = $DB->SimpleQuery($query); // like simple mysql_query() The point is to generate random values by PHP to avoid random generation by MySQL
Function returns array with pairs id/image


SELECT * FROM `images` ORDER BY RAND() LIMIT 1;
0

精彩评论

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