PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I'm using a very simple php script to return random images for use in signatures on forums, and when it is loaded more than once on one page, it still displays the same one image. It randomises correctly, but I would like, if possible, for it to display a different image each time it is loaded, by clearing the cache or whatever is required.
What you could actually do which I think would be easier, is make an array at the top of your page with all the images in. Then with the number you pick you could just output it into an array, and that array slot would correspond with a string, so you wouldn't have to have an whole bunch of if statements.
My code I showed may have been a bad idea, but the 2nd part with advice isn't (...at least until someone comes and tells me differently)
I'm want to see why this is happening though. It seems like it's just not loading it a second time.
Also, I would suggest modifying your script to just pick a random image out of a directory, rather than hardcoding in the filenames. That will allow you to just throw images into the directory, and have this script use them.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
THAT's how I did it. Good thinking. I forgot...and my example was, well basically completely useless. I actually made different directories and only selected one image from each, because I was doing something slightly different. Psh, shouldn't have been that hard to think of.
<?
//create connection to DB
$check_session = false;
require_once('/path/to/security/file');
$DBSession = new DB();//db abstraction class
$image_id = $_GET['image_id'];
$table = ($_GET['thumb'] == 'true') ? "thumbnails" : "images";
$query = <<<SQL
SELECT
*
FROM
$table
WHERE
entry_id = '$image_id'
SQL;
$result = $DBSession->sql_query($query,' retrieving image data');//maps to mysql_query($query)
$row = $DBSession->get_data($result);//equivalent to mysql_fetch_assoc();
header('Content-length: '.strlen($row[data]));//this line is important
header("Content-type: .$row[mime]");//this could be assumed if you didn't want to have multiple different filetypes.
echo $row[data];
?>
Keep in mind, this function takes the image data right from the db, not from the filestructure. It could be modified however, to load the image data from an address stored in the db.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
<?
//create connection to DB
$check_session = false;
require_once('/path/to/security/file');
$DBSession = new DB();//db abstraction class
$image_id = $_GET['image_id'];
$table = ($_GET['thumb'] == 'true') ? "thumbnails" : "images";
$query = <<<SQL
SELECT
*
FROM
$table
WHERE
entry_id = '$image_id'
SQL;
$result = $DBSession->sql_query($query,' retrieving image data');//maps to mysql_query($query)
$row = $DBSession->get_data($result);//equivalent to mysql_fetch_assoc();
header('Content-length: '.strlen($row[data]));//this line is important
header("Content-type: .$row[mime]");//this could be assumed if you didn't want to have multiple different filetypes.
echo $row[data];
?>
Keep in mind, this function takes the image data right from the db, not from the filestructure. It could be modified however, to load the image data from an address stored in the db.
While I do like the idea better of just having the image source be a php script and having it choose an image rather then inserting PHP code into the XHTML page (your right, it's a lot cleaner), I'm not sure how your script is a random script. You seem to be calling a specific image.
Also I'm using MySQL which I don't believe supports storing images. Or else I need to redo my registering page for avatars.
That script was just meant as an example of another way to output an image. This script should do the same thing from a file structure, and make it a random image. It's untested, but all the code here is from other functions I've written, so there shouldn't be a problem.