Jump to content

Random image Help needed


TennesseeGuy

Recommended Posts

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?
Link to comment
https://forums.phpfreaks.com/topic/6619-random-image-help-needed/
Share on other sites

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.

[code]
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)
    }
}
[/code]
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:
[code]
<?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";
?>
[/code]
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.

[code]
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'];
[/code]

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

[code]
<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>
[/code]
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>";
}
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++;
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.