PDA

View Full Version : random image


googleit
08-31-2006, 05:16 PM
here is some code whic i use on my website for banners

<?php

/*
* Name your images 1.jpg, 2.jpg etc.
*
* Add this line to your page where you want the images to
* appear: <?php include "randomimage.php"; ?>
*/

// Change this to the total number of images in the folder
$total = "11";

// Change to the type of files to use eg. .jpg or .gif
$file_type = ".jpg";

// Change to the location of the folder containing the images
$image_folder = "images/random";

// You do not need to edit below this line

$start = "1";

$random = mt_rand($start, $total);

$image_name = $random . $file_type;

echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />";

?>

pauls74462
12-30-2008, 10:52 PM
here is some code whic i use on my website for banners

<?php

/*
* Name your images 1.jpg, 2.jpg etc.
*
* Add this line to your page where you want the images to
* appear: <?php include "randomimage.php"; ?>
*/

// Change this to the total number of images in the folder
$total = "11";

// Change to the type of files to use eg. .jpg or .gif
$file_type = ".jpg";

// Change to the location of the folder containing the images
$image_folder = "images/random";

// You do not need to edit below this line

$start = "1";

$random = mt_rand($start, $total);

$image_name = $random . $file_type;

echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />";

?>



Very helpful, but on my own programming I created some unknown error. Hope some one can help.

here is my error I'm getting :(


You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= '1'' at line 1



<meta name="generator" content="Namo WebEditor(Trial)">
<?PHP
// Get db connections
include ("config.php");
// Make a MySQL Connection

$link2 = mysql_connect( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());


mysql_select_db($dbname) or die(mysql_error());



$query = "INSERT INTO Image (ImagePath)
VALUES('$ImagePath')";
mysql_query($query) or die(mysql_error());

$ImagePath = ""; // Clear variable so I know the image if from the db

$result = mysql_query("SELECT ImagePath FROM Image where $id = '1'") or die(mysql_error());

// Make sure you actually get a result:
if (mysql_num_rows($result) != 1){
$error = "Bad Login";


echo '<font color="black"><a href="imagedb.php" target="_self">ImagePath</font></a> not found, re-enter';


} else {

echo "<img src=\"$ImagePath\" />";

$ImagePath = "";// Clear variable so I know the old variable will not be reinserted in the db again.

echo '<font color="black">Click here to insert a new <a href="imagedb.php" target="_self">ImagePath</font></a> in the db.';

}


mysql_close();


?>

er4o
01-07-2009, 08:38 AM
Thank you, veru helpful.

kbluhm
01-08-2009, 05:54 AM
Here is a much simpler way. Just upload images (accepted types can be defined within the curly braces) to the specified folder and it will pick one automatically, regardless of name or the number of images:

$folder = 'images/random';
$images = glob( $folder . '/*.{jpg,gif,png}', GLOB_BRACE );
$image = $images[ array_rand( $images ) ];
echo '<img src=" . $folder . '/' . $image . " alt="' . $image . '" />';

CyberPirate
01-10-2009, 01:42 AM
Here is another method :)

$banners = array('/img/banner.jpg', '/img/banner2.jpg', '/img/banner3.jpg');
shuffle($banners);
echo ' <img src="'.$banners[0].'" width="123" height="123" alt="banner" /> ';

kbluhm
01-10-2009, 07:27 PM
oops... missing a few single quotes in there, as well as having included a bit of flawed logic, for all you copy 'n' pasters ;)

$folder = 'images/random';
$images = glob( $folder . '/*.{jpg,gif,png}', GLOB_BRACE );
$image = basename( $images[ array_rand( $images ) ] );
echo '<img src="' . $folder . '/' . $image . '" alt="' . $image . '" />';

CaptainB
03-24-2009, 10:06 AM
Thanks kbluhm - unfortunately I don't see any 'Thank you' button, so you'll just get this :)

pheyz
04-22-2009, 06:43 AM
Here is another method :)

$banners = array('/img/banner.jpg', '/img/banner2.jpg', '/img/banner3.jpg');
shuffle($banners);
echo ' <img src="'.$banners[0].'" width="123" height="123" alt="banner" /> ';

thanks for this method! it help me alot :)