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
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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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>";
}
Link to comment
Share on other sites

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++;
}
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.