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...whi ch 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_featu re = "SELECT pr_id,pr_name,p r_thumb FROM tbl_product
WHERE pr_feature = '1'";
$result_home_fe ature = @mysql_query($s ql_home_feature , $dbh);
$num_feat_items = mysql_num_rows( $result_home_fe ature);
$x = 0;
while ($row_hfeat = mysql_fetch_arr ay($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=\"prodimage s/".$pr_thumb[$a]."\"></a>";
$homeitem1 = "<img src=\"images/dotarrow.gif\"
align=\"absmidd le\"> ".$p r_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=\"prodimage s/".$pr_thumb[$b]."\"></a>";
$homeitem2 = "<img src=\"images/dotarrow.gif\"
align=\"absmidd le\"> ".$p r_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=\"prodimage s/".$pr_thumb[$c]."\"></a>";
$homeitem3 = "<img src=\"images/dotarrow.gif\"
align=\"absmidd le\"> ".$p r_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=\"prodimage s/".$pr_thumb[$d]."\"></a>";
$homeitem4 = "<img src=\"images/dotarrow.gif\"
align=\"absmidd le\"> ".$p r_name[$d];
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...whi ch 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_featu re = "SELECT pr_id,pr_name,p r_thumb FROM tbl_product
WHERE pr_feature = '1'";
$result_home_fe ature = @mysql_query($s ql_home_feature , $dbh);
$num_feat_items = mysql_num_rows( $result_home_fe ature);
$x = 0;
while ($row_hfeat = mysql_fetch_arr ay($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=\"prodimage s/".$pr_thumb[$a]."\"></a>";
$homeitem1 = "<img src=\"images/dotarrow.gif\"
align=\"absmidd le\"> ".$p r_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=\"prodimage s/".$pr_thumb[$b]."\"></a>";
$homeitem2 = "<img src=\"images/dotarrow.gif\"
align=\"absmidd le\"> ".$p r_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=\"prodimage s/".$pr_thumb[$c]."\"></a>";
$homeitem3 = "<img src=\"images/dotarrow.gif\"
align=\"absmidd le\"> ".$p r_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=\"prodimage s/".$pr_thumb[$d]."\"></a>";
$homeitem4 = "<img src=\"images/dotarrow.gif\"
align=\"absmidd le\"> ".$p r_name[$d];
Comment