Re: random images fom db

From: Date: Fri, 16 Nov 2001 20:11:50 +0000
Subject: Re: random images fom db
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-74929@lists.php.net to get a copy of this message
Got it...if anyone's interested the code is below :) /connect to db $connectionToDBid = odbc_connect("codesnipits", "joecode", "joecode"); // sql statement $sqlb = "SELECT imagepath FROM IMAGES"; / run the query and dump into $numberofrecords variable $row = odbc_do($connectionToDBid, $sqlb); // create an array $myArray = Array(); / seed random number generator srand ((float) microtime() * 10000000); / loop through each record and dump image paths into array while(odbc_fetch_row($row)){ // grab and assign all image paths to array $imagepath = odbc_result($row,1); $myArray[] = $imagepath ; } / grab a random index from $myArray here $imagearrayindex = array_rand($myArray); $randomimage = $myArray[$imagearrayindex]; print "<br><br><br><div align='center'>"; print "<table width='400' border=0 cellpadding=2>" ; print "<tr><td align='left'>" ; print "<font color='#ffffff' face='verdana'><h4>" . "Welcome back " . $firstname . "</h4></font>"; print "</td></tr>"; print "<tr><td align='center'>" ; print "<img src='" . $randomimage ."' align='center' border='0'><br>"; print "</td></tr>"; print "</table>"; "Joe Van Meer" <joe@coastaldestination.com> wrote in message news:20011116191000.87224.qmail@pb1.pair.com... > Hi there. I have s a php page that randomly grabs an image's path from a db > field and displays it on my php page. My problem is that sometimes when I > refresh the image won't display. So I guess you can say I'm getting > intermittent images...sometimes it displays and sometimes not. I've checked > all of my paths for all of the images and they are all the same. My code is > below. > > Thx, Joe > > /connect to db > $connectionToDBid = odbc_connect("codesnipits", "joecode", > "joecode"); > > > // sql statement > $sqlb = "SELECT image_id FROM IMAGES"; > > /run the query and dump into $row variable > $row = odbc_do($connectionToDBid, $sqlb); > > // create an array > $myArray = Array(); > srand ((float) microtime() * 10000000); > > /loop through each record and dump image_ids into array > > while(odbc_fetch_row($row)){ > // grab and assign ids to array > $imageid = odbc_result($row,1); > $myArray[] = $imageid ; > > } > > /loop through and print out array values > foreach ($myArray as $value){ > print "$value"; > } > > > /create $randomimageid variable here > $randomimageid = array_rand($myArray,1); > > > > > > /create query statement to grab random image path and dump into variable --> > randomimage > $sqlr = "SELECT imagepath FROM IMAGES WHERE image_id = $randomimageid"; > > > /run the sql statement on the connection made > $resultset = odbc_do($connectionToDBid, $sqlr); > > /dump into $randomimage here > $randomimage = odbc_result($resultset, 1); > > > /close connection to db > odbc_close($connectionToDBid); > > print "<br><br><br><div align='center'>"; > print "<table width='400' border=0 cellpadding=2>" ; > print "<tr><td align='left'>" ; > print "<font color='#ffffff' face='verdana'><h4>" . > "Welcome back " . > $firstname . "</h4></font>"; > print "</td></tr>"; > print "<tr><td align='center'>" ; > print "<img src='$randomimage' align='center' > border='0'><br>"; > print "</td></tr>"; > print "</table>"; > >

« previous php.general (#74929) next »