CodeCharge Studio
search Register Login  

Visual Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 Random Background Image

Print topic Send  topic

Author Message
PeterJ


Posts: 90
Posted: 03/27/2008, 9:32 AM

Hi

I would like a random image from a table to appear when a page opens. I am thinking the best place to create this request would be in the <body> tag. I have tried this and failed. Is there a better place to create the code and can anyone offer any advice please?

Many thanks

PeterJ
View profile  Send private message
DonP
Posted: 03/27/2008, 9:09 AM

I have a separate script with all the coding that gets an image randomly
from the database by using simple SQL, then it creates image headers and
outputs the image. I set it up in such a way that if an ID parameter is
passed to it, it will pull out a specific image but if not, it pulls one
randomly. Then you can call it anywhere - in the body tag or in a style
sheet by calling the script. For example, it can be called like this in
CSS: background-image: url(/php/show_background.php);

Here is my code, which I believe is in an After Initialize event:

$DBconnection = new clsDBconnection();

if (CCGetParam("Type","")) {
$Type = CCGetParam("Type","");
$PrevImg = CCGetSession("RandBackground");
$image_id = CCDlookUp("ID", "background_images", " BackgroundType =
" . $Type . " AND Image IS NOT NULL AND ID != '" . $PrevImg . "' ORDER
BY RAND() LIMIT 1", $DBconnection);
CCSetSession("RandBackground", $image_id);
} elseif (CCGetParam("ID","")) {
$image_id = CCGetParam("ID","");
} else {
$PrevImg = CCGetSession("RandBackground");
$image_id = CCDlookUp("ID", "background_images", " BackgroundType =
1 AND Image IS NOT NULL AND ID != '" . $PrevImg . "' ORDER BY RAND()
LIMIT 1", $DBconnection);
CCSetSession("RandBackground", $image_id);
}

if (!$image_id) $image_id = "1";
$DBconnection->query("select Image, ImageType from background_images
where ID=" . $image_id);
if ($DBconnection->next_record()) {
$mime_type = $DBconnection->f("ImageType");
$Image = $DBconnection->f("Image");
}

Header ("Content-type: image/" . $mime_type);
Header ("Pragma: no-cache");
echo $Image;
exit();

Don (DonP)

PeterJ wrote:
> Hi
>
> I would like a random image from a table to appear when a page opens. I am
> thinking the best place to create this request would be in the <body> tag. I
> have tried this and failed. Is there a better place to create the code and can
> anyone offer any advice please?
>
> Many thanks
>
> PeterJ
> ---------------------------------------
> Sent from YesSoftware forum
> http://forums.codecharge.com/
>
PeterJ


Posts: 90
Posted: 03/27/2008, 11:00 AM

Brilliant and incredibly helpful

thank you Don

Peter
View profile  Send private message
wkempees


Posts: 1679
Posted: 03/27/2008, 11:56 AM

@DonP
5 star, this script does expect the image to be stored in the database, am I right?
$Image to hold the binary data in a (mySql) BLOB.
$image_type holding the extension, later fed to mime_type.

As the original question was about random background image, how do you intend to cater for the size difference (imagesize vs. browser background size), centered/streched/repeated.

*****

Walter


_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)

if you liked this info PAYPAL me: http://donate.consultair.eu
View profile  Send private message
DonB
Posted: 03/27/2008, 6:38 PM

It does, but you could instead store the path/filename pointer to the file,
then modify the last line to be

readfile($Image);

--
DonB



"wkempees" <wkempees@forum.codecharge> wrote in message
news:547ebeddbd944f@news.codecharge.com...
> @DonP
> 5 star, this script does expect the image to be stored in the database, am
> I
> right?
> $Image to hold the binary data in a (mySql) BLOB.
> $image_type holding the extension, later fed to mime_type.
>
> As the original question was about random background image, how do you
> intend
> to cater for the size difference (imagesize vs. browser background size),
> centered/streched/repeated.
>
> *****
>
> Walter
>
>
> ---------------------------------------
> Sent from YesSoftware forum
> http://forums.codecharge.com/
>

DonP
Posted: 03/27/2008, 9:54 PM

Yes, this code loads the image from a BLOB field in the database but it
is virtually identical for an image that is stored on the file system.
As far as sizes, one can use any styles or size values needed just as
though it were loading the image by more conventional means. CSS handles
it just like any other image.

Don (DonP)

wkempees wrote:
> @DonP
> 5 star, this script does expect the image to be stored in the database, am I
> right?
> $Image to hold the binary data in a (mySql) BLOB.
> $image_type holding the extension, later fed to mime_type.
>
> As the original question was about random background image, how do you intend
> to cater for the size difference (imagesize vs. browser background size),
> centered/streched/repeated.
>
> *****
>
> Walter
>
>
> ---------------------------------------
> Sent from YesSoftware forum
> http://forums.codecharge.com/
>

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

MS Access to Web

Convert MS Access to Web.
Join thousands of Web developers who build Web applications with minimal coding.

CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.