Pulling random images from a database?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LRW

    Pulling random images from a database?

    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\">&nbsp;".$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\">&nbsp;".$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\">&nbsp;".$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\">&nbsp;".$p r_name[$d];
  • Dan Scott

    #2
    Re: Pulling random images from a database?

    LRW wrote:
    [color=blue]
    > 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\">&nbsp;".$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\">&nbsp;".$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\">&nbsp;".$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\">&nbsp;".$p r_name[$d];[/color]

    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:
    [color=blue]
    > $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++;
    > }[/color]

    // 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="absmiddl e">&nbsp;'
    .. $pr_name[$key] . '">;
    }

    Dan

    Comment

    • steve

      #3
      Re: Pulling random images from a database?

      "LRW" wrote:[color=blue]
      > 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[/color]
      same[color=blue]
      > one more than once.
      >
      > The best I can come up with is the following...whi ch makes[/color]
      duplicates[color=blue]
      > 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[/color]
      tbl_product[color=blue]
      > 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\"> ".$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=\"prodimage s/".$pr_thumb[$b]."\"></a>";
      > $homeitem2 = "<img src=\"images/dotarrow.gif\"
      > align=\"absmidd le\"> ".$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=\"prodimage s/".$pr_thumb[$c]."\"></a>";
      > $homeitem3 = "<img src=\"images/dotarrow.gif\"
      > align=\"absmidd le\"> ".$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=\"prodimage s/".$pr_thumb[$d]."\"></a>";
      > $homeitem4 = "<img src=\"images/dotarrow.gif\"
      > align=\"absmidd le\"> ".$pr_name[$d];[/color]

      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

      Comment

      • LRW

        #4
        Re: Pulling random images from a database?

        steve <UseLinkToEmail @dbForumz.com> wrote in message news:<41336379$ 1_3@alt.athenan ews.com>...[color=blue]
        >
        > Use "order by rand()" in your select query, and then just limit it
        > to 4 rows returned.[/color]

        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

        Comment

        • LRW

          #5
          Re: Pulling random images from a database?

          Dan Scott <dan.scott@ca.i bm.com> wrote in message news:<DHHYc.665 2$CG3.479445@ne ws20.bellglobal .com>...[color=blue]
          > LRW wrote:
          >[color=green]
          > > 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.[color=darkred]
          > > >[/color][/color]
          > I think you're missing shuffle() (and a nice clean separation of your
          > presentation and logic code, but that's a different matter).
          >[/color]

          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

          Comment

          Working...