Jump to content


Photo

Random image Help needed


  • Please log in to reply
5 replies to this topic

#1 TennesseeGuy

TennesseeGuy
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 05 April 2006 - 01:25 AM

I am hoping to find someone to hep me out with this slight issue. I have dedicated css blocks throughout a page to hold random thumbnails from member submitted images. Essientially I would like to extract the image path from a mysql database and display the image and the image url into an array of 8 and then use that array of 8 randomly in 8 different spots on a page. Is this possible?

#2 TennesseeGuy

TennesseeGuy
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 05 April 2006 - 12:52 PM

I have a small function I am attempting using to call upon the random image nad matching url but so far it has been unsuccessful. Could someone take a quick look at the code and perhaps see if you might be able to see the error?!? Thanks.

function displayImage($bannerImage,$myconn) {
    $sql = "Select COUNT(*) as id from pages where mid = '1' AND sponPic is not 'NULL'";
    $results = mysql_query($sql,$myconn) or die(mysql_error());
    $settings = mysql_fetch_array($results);
    $rowcount = $settings['id'];
    // Get Any number of Random Ads Quickly
    for( $AdCount = 1; $AdCount <= 8; $AdCount++ ) {
        // Guess a Random Row Number
        $GetRandRow = rand(1,$rowcount);
        // Build the Select Query to get the wanted random row
        // Being done in PHP instead of MySql because you
        // can't use MySql @UserVariables in the order by clause
        $query_bannerImage = "SELECT sponBanner,url,title FROM pages WHERE banner_category='$bannerImage' Limit $GetRandRow, 1";
        // Fetch the Row
        $results = mysql_query($query_bannerImage) or die(mysql_error());
        $row_bannerImage = mysql_fetch_array($results)
    }
}


#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 05 April 2006 - 01:28 PM

i believe your best bet for this sort of a situation would be to populate an array with the 8 random images, and then, just display those images in order down the page (from 0-7) in order. the key is to assure that the order of the images IN THE ARRAY is random. for instance, if i had the src to my images saved in a database, i might run something such as this:
<?php
$sql = mysql_query("SELECT src FROM images LIMT 8 ORDER BY RAND()");
$images = array();
while ($x = mysql_fetch_array($sql))
  $images[] = $x['src'];

// now that i have 8 random images, just output the next 8 lines in your 8 boxes through the page:
echo "<img src='$images[0]' />\n";
echo "<img src='$images[1]' />\n";
echo "<img src='$images[2]' />\n";
echo "<img src='$images[3]' />\n";
echo "<img src='$images[4]' />\n";
echo "<img src='$images[5]' />\n";
echo "<img src='$images[6]' />\n";
echo "<img src='$images[7]' />\n";
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#4 TennesseeGuy

TennesseeGuy
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 05 April 2006 - 02:13 PM

I get the following error when I attempt that solution:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in

Still very much learning here so thank you very much for your assistance. Here is a slighly modified version of what I think should fit my needs...let me know if I did anything wrong with it. Thanks again.

mysql_select_db($database_myconn, $myconn);
$query_images = mysql_query("SELECT title, url, sponBanner FROM pages where mid = '1' and active = 'Yes' LIMIT 8 ORDER BY RAND()");
$titles = array();
$urls = array();
$images = array();
while ($x = mysql_fetch_array($query_images))
  $titles[] = $x['title'];
  $urls[] = $x['urls'];
  $images[] = $x['images'];

And I believe I would use it throughout the page like this:

<a href="<?php $url[0] ?>"><img src="<?php $images[0] ?>" style="float: left; display: inline; width: 139px; height: 51px; margin: 0; padding:0;" alt="<?php $title[0] ?>" /></a>


#5 TennesseeGuy

TennesseeGuy
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 05 April 2006 - 05:25 PM

I think I got it to work....well sorta....what I would like it to do is if there is less then the defined number available in the db then to repeat existing random images until all 8 are displayed on screen. Is this possible? Here is what I have so far that doesn't error out:

mysql_select_db($database_myconn, $myconn);
$maxImages = 8 ;
$query_images = mysql_query ("select title, url, sponPic from pages where mid = '1' and active = 'Yes' and sponPic is not null ORDER BY RAND() LIMIT $maxImages");
while ($row = mysql_fetch_array($query_images))
{
echo "<a href=\"" . $row["url"] . "\"><img src=\"" . $row["sponPic"] . "\" border=0 alt=\"" . $row["title"] . "\"></a>";
}

#6 TennesseeGuy

TennesseeGuy
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 05 April 2006 - 05:45 PM

I got the following to work but I don't think it is still randomizing my selection but rather just repeating the original results. Any help anyone?!? Thanks again.
mysql_select_db($database_myconn, $myconn);
$maxImages = 8 ;
$query_images = mysql_query ("select title, url, sponPic from pages where mid = '1' and active = 'Yes' and sponPic is not null ORDER BY RAND() LIMIT $maxImages");
$countImages = 1 ;
while ($row = mysql_fetch_array($query_images))
{
while ($countImages <= 8) {
echo "<a href=\"" . $row["url"] . "\"><img src=\"" . $row["sponPic"] . "\" border=0 alt=\"" . $row["title"] . "\"></a>";
$countImages++;
}
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users