473,320 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Pulling random images from a database?

LRW
For an e-commerce site, I'm wanting to have it pull 4 random images
for the front page from a select list of items from the DB.
I can get it to pull randomly and place the images, but I can't figure
out how to keep it from potentially repeating images; using the same
one more than once.

The best I can come up with is the following...which makes duplicates
a little less common, but certainly doesn't stop it.

I'm wondering what piece of the puzzle I'm missing. Like is there an
undocumented feature of rand() that lets you exclude a number? =)

Thanks for any pointers!
Liam

$sql_home_feature = "SELECT pr_id,pr_name,pr_thumb FROM tbl_product
WHERE pr_feature = '1'";
$result_home_feature = @mysql_query($sql_home_feature, $dbh);
$num_feat_items = mysql_num_rows($result_home_feature);
$x = 0;
while ($row_hfeat = mysql_fetch_array($result_home_feature)) {
//$pr_id = $row_hfeat[pr_id];
$pr_name[$x] = $row_hfeat[pr_name];
$pr_thumb[$x] = $row_hfeat[pr_thumb];
$x++;
}
$a = rand(0, 4);
$homeimg1 = "<a href=\"#\"><img
src=\"prodimages/".$pr_thumb[$a]."\"></a>";
$homeitem1 = "<img src=\"images/dotarrow.gif\"
align=\"absmiddle\">&nbsp;".$pr_name[$a];
function funb($a) {
$b = rand(0, 4);
if ($b == $a) {
$b = rand(0, 4);
return $b;
} else {
return $b;
}
}
$b = funb($a);
$homeimg2 = "<a href=\"#\"><img
src=\"prodimages/".$pr_thumb[$b]."\"></a>";
$homeitem2 = "<img src=\"images/dotarrow.gif\"
align=\"absmiddle\">&nbsp;".$pr_name[$b];

function func($a,$b) {
$c = rand(0, 4);
if (($c == $a) || ($c == $b)) {
$c = rand(0, 4);
return $c;
} else {
return $c;
}
}
$c = func($a,$b);
$homeimg3 = "<a href=\"#\"><img
src=\"prodimages/".$pr_thumb[$c]."\"></a>";
$homeitem3 = "<img src=\"images/dotarrow.gif\"
align=\"absmiddle\">&nbsp;".$pr_name[$c];

function fund($a,$b,$c) {
$d = rand(0, 4);
if (($d == $a) || ($d == $b) || ($d == $c)) {
$d = rand(0, 4);
return $d;
} else {
return $d;
}
}
$d = fund($a,$b,$c);
$homeimg4 = "<a href=\"#\"><img
src=\"prodimages/".$pr_thumb[$d]."\"></a>";
$homeitem4 = "<img src=\"images/dotarrow.gif\"
align=\"absmiddle\">&nbsp;".$pr_name[$d];
Jul 17 '05 #1
4 1953
LRW wrote:
For an e-commerce site, I'm wanting to have it pull 4 random images
for the front page from a select list of items from the DB.
I can get it to pull randomly and place the images, but I can't figure
out how to keep it from potentially repeating images; using the same
one more than once.

The best I can come up with is the following...which makes duplicates
a little less common, but certainly doesn't stop it.

I'm wondering what piece of the puzzle I'm missing. Like is there an
undocumented feature of rand() that lets you exclude a number? =)

Thanks for any pointers!
Liam

$sql_home_feature = "SELECT pr_id,pr_name,pr_thumb FROM tbl_product
WHERE pr_feature = '1'";
$result_home_feature = @mysql_query($sql_home_feature, $dbh);
$num_feat_items = mysql_num_rows($result_home_feature);
$x = 0;
while ($row_hfeat = mysql_fetch_array($result_home_feature)) {
//$pr_id = $row_hfeat[pr_id];
$pr_name[$x] = $row_hfeat[pr_name];
$pr_thumb[$x] = $row_hfeat[pr_thumb];
$x++;
}
$a = rand(0, 4);
$homeimg1 = "<a href=\"#\"><img
src=\"prodimages/".$pr_thumb[$a]."\"></a>";
$homeitem1 = "<img src=\"images/dotarrow.gif\"
align=\"absmiddle\">&nbsp;".$pr_name[$a];
function funb($a) {
$b = rand(0, 4);
if ($b == $a) {
$b = rand(0, 4);
return $b;
} else {
return $b;
}
}
$b = funb($a);
$homeimg2 = "<a href=\"#\"><img
src=\"prodimages/".$pr_thumb[$b]."\"></a>";
$homeitem2 = "<img src=\"images/dotarrow.gif\"
align=\"absmiddle\">&nbsp;".$pr_name[$b];

function func($a,$b) {
$c = rand(0, 4);
if (($c == $a) || ($c == $b)) {
$c = rand(0, 4);
return $c;
} else {
return $c;
}
}
$c = func($a,$b);
$homeimg3 = "<a href=\"#\"><img
src=\"prodimages/".$pr_thumb[$c]."\"></a>";
$homeitem3 = "<img src=\"images/dotarrow.gif\"
align=\"absmiddle\">&nbsp;".$pr_name[$c];

function fund($a,$b,$c) {
$d = rand(0, 4);
if (($d == $a) || ($d == $b) || ($d == $c)) {
$d = rand(0, 4);
return $d;
} else {
return $d;
}
}
$d = fund($a,$b,$c);
$homeimg4 = "<a href=\"#\"><img
src=\"prodimages/".$pr_thumb[$d]."\"></a>";
$homeitem4 = "<img src=\"images/dotarrow.gif\"
align=\"absmiddle\">&nbsp;".$pr_name[$d];
I think you're missing shuffle() (and a nice clean separation of your
presentation and logic code, but that's a different matter).

How about throwing out most of your code and using something like:
$sql_home_feature = "SELECT pr_id,pr_name,pr_thumb FROM tbl_product
WHERE pr_feature = '1'";
$result_home_feature = @mysql_query($sql_home_feature, $dbh);
$num_feat_items = mysql_num_rows($result_home_feature);
$x = 0;
while ($row_hfeat = mysql_fetch_array($result_home_feature)) {
//$pr_id = $row_hfeat[pr_id];
$pr_name[$x] = $row_hfeat[pr_name];
$pr_thumb[$x] = $row_hfeat[pr_thumb];
$x++;
}


// build an array of four numbers -- because you only want four
// images on your home page
$random_keys = array(0, 1, 2, 3);
// shuffle() is a built-in function that randomizes an array's order
shuffle($random_keys);

foreach ($random_keys as $key) {
$homeimg = '<a href="#"><img src="prodimages/' . $pr_thumb[$key] .
'"></a>';
$homeitem = '<img src="images/dotarrow.gif" align="absmiddle">&nbsp;'
.. $pr_name[$key] . '">;
}

Dan
Jul 17 '05 #2
"LRW" wrote:
For an e-commerce site, I’m wanting to have it pull 4 random
images
for the front page from a select list of items from the DB.
I can get it to pull randomly and place the images, but I can’t
figure
out how to keep it from potentially repeating images; using the same one more than once.

The best I can come up with is the following...which makes duplicates a little less common, but certainly doesn’t stop it.

I’m wondering what piece of the puzzle I’m missing. Like
is there an
undocumented feature of rand() that lets you exclude a number? =)

Thanks for any pointers!
Liam

$sql_home_feature = "SELECT pr_id,pr_name,pr_thumb FROM tbl_product WHERE pr_feature = ’1’";
$result_home_feature = @mysql_query($sql_home_feature, $dbh);
$num_feat_items = mysql_num_rows($result_home_feature);
$x = 0;
while ($row_hfeat = mysql_fetch_array($result_home_feature)) {
//$pr_id = $row_hfeat[pr_id];
$pr_name[$x] = $row_hfeat[pr_name];
$pr_thumb[$x] = $row_hfeat[pr_thumb];
$x++;
}
$a = rand(0, 4);
$homeimg1 = "<a href=\"#\"><img
src=\"prodimages/".$pr_thumb[$a]."\"></a>";
$homeitem1 = "<img src=\"images/dotarrow.gif\"
align=\"absmiddle\">*".$pr_name[$a];
function funb($a) {
$b = rand(0, 4);
if ($b == $a) {
$b = rand(0, 4);
return $b;
} else {
return $b;
}
}
$b = funb($a);
$homeimg2 = "<a href=\"#\"><img
src=\"prodimages/".$pr_thumb[$b]."\"></a>";
$homeitem2 = "<img src=\"images/dotarrow.gif\"
align=\"absmiddle\">*".$pr_name[$b];

function func($a,$b) {
$c = rand(0, 4);
if (($c == $a) || ($c == $b)) {
$c = rand(0, 4);
return $c;
} else {
return $c;
}
}
$c = func($a,$b);
$homeimg3 = "<a href=\"#\"><img
src=\"prodimages/".$pr_thumb[$c]."\"></a>";
$homeitem3 = "<img src=\"images/dotarrow.gif\"
align=\"absmiddle\">*".$pr_name[$c];

function fund($a,$b,$c) {
$d = rand(0, 4);
if (($d == $a) || ($d == $b) || ($d == $c)) {
$d = rand(0, 4);
return $d;
} else {
return $d;
}
}
$d = fund($a,$b,$c);
$homeimg4 = "<a href=\"#\"><img
src=\"prodimages/".$pr_thumb[$d]."\"></a>";
$homeitem4 = "<img src=\"images/dotarrow.gif\"
align=\"absmiddle\">*".$pr_name[$d];


Use "order by rand()" in your select query, and then just limit it
to 4 rows returned.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-Pulling-...ict144604.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=484008
Jul 17 '05 #3
LRW
steve <Us************@dbForumz.com> wrote in message news:<41**********@alt.athenanews.com>...

Use "order by rand()" in your select query, and then just limit it
to 4 rows returned.


Holy simple answers, Batman! That's fantastic! Totally solves my
problem, quick and easy.
I had NO idea you could use rand() in a SQL query like that.
Thanks!! =)
Liam
Jul 17 '05 #4
LRW
Dan Scott <da*******@ca.ibm.com> wrote in message news:<DH*******************@news20.bellglobal.com> ...
LRW wrote:
For an e-commerce site, I'm wanting to have it pull 4 random images
for the front page from a select list of items from the DB.
I can get it to pull randomly and place the images, but I can't figure
out how to keep it from potentially repeating images; using the same
one more than once.

I think you're missing shuffle() (and a nice clean separation of your
presentation and logic code, but that's a different matter).


A "Steve" suggested I simply use "order by rand()" and limit the query
to four items, which works super simple and nice...but I'll look into
shuffle().
That may be a good solution for other issues down the line!
Plus it's always good to learn more. =)

What I'm curious about is your comment about separation and
presentation of logic code.
Since I REALLY want to be able to write clear, clean code, I was
wondering what you meant by that.
Something as simple as placing the functions all together, or more
than that?
(Because this funky function method I set up I was actually
anticipating on being temporary once I found a good solution.)
Otherwise I normally do put all my functions together.

If it's more than that, I'd certainly be interested in hearing your
suggestions!
Thanks!
Liam
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: gene | last post by:
I have some javascript code that displays random images in random places on the page by making changes to the document's css. This works in Safari and IE, but I can't get it to work on Mozilla. ...
6
by: Olly | last post by:
I've found a basic script, however I also need to add alt and title attributes as well, how would I go about doing this? Here's the script I found: Thanks <script language="JavaScript"> <!--...
9
by: Michael Burtenshaw | last post by:
I would like to make a slide show using random images. The problem is my host is 250.com, and they don't support cgi-programs. Is there another way to accomplish random images?
5
by: Linda | last post by:
Greetings: I have found a random image script that I like. It is located here: http://www.javascriptcity.com/scripts/local/simage3.htm I'd like to edit this to have 4 different slots for...
5
by: Haydnw | last post by:
Hi, I have the code below as code-behind for a page which displays two images. My problem is with the second bit of code, commented as " 'Portfolio image section". Basically, the SQL query gets...
3
by: Simon | last post by:
This problem has been driving me mad for months.... Seen a few posts on forums about it but no answers... No mention on MSDN etc. XP Pro SP1, VS.NET (c#) .Net framework 1.1, IIS 5.1. In a...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
7
by: Brian | last post by:
Hi there I have been looking for a script that can randomly rotate 8 different images on 1 page. I used to have a script that did this but can't find it now. I have found loads of script that...
3
by: fran7 | last post by:
Hi, I have this nice code that returns a random image database record. It works great. What I am trying to do now is to be able to get the "alt" description for the image from another field. If I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.