Programmatically change my background graphic?

M

Mike Silva

Hi,

I'm a programmer but not a web programmer. That hasn't stopped me from
putting up a simple website, however. What I'd like to do is to be
able to do is to change the graphic that forms the tiled background for
the site, based on the calendar day. Something along the lines of

if new_date
todays_graphic = lookup_background_graphic( date )
copy( todays_graphic TO generic_background_graphic_file )

Very simple to imagine, but I have no idea how this might be done on a
web server. Obviously my question is a just a particular instance of
the general question how-do-I-run-code-on-my-website. So, how do I? I
know there are various technologies to do so, so if you folks could
just toss a few buzzwords my way I could go research things further.
Thanks!
 
T

Travis Newbury

Mike said:
Very simple to imagine, but I have no idea how this might be done on a
web server. Obviously my question is a just a particular instance of
the general question how-do-I-run-code-on-my-website. So, how do I? I
know there are various technologies to do so, so if you folks could
just toss a few buzzwords my way I could go research things further.

PHP, ASP
 
B

Beauregard T. Shagnasty

Mike said:
Thanks. Now, how would one choose between them? My website is on a
Linux server, if that matters.

PHP.

First thing that comes to mind is to use a modulo calculation based on
the number of graphics you have stored in a "backgrounds" directory.
 
D

David Segall

Mike Silva said:
Thanks. Now, how would one choose between them? My website is on a
Linux server, if that matters.
PHP is a "write only" language and ASP almost requires a Microsoft
server. Head for Java Server Faces if you want to follow Sun and
Oracle. If you don't like Java as a server-side language you might
want to look at Python. The other major choice for a server-side
language is Perl which is extraordinarily powerful but even more
difficult to read than PHP.
 
J

Jonathan N. Little

Mike said:
Thanks. Now, how would one choose between them? My website is on a
Linux server, if that matters.

Well a very simple example in PHP for 1 - 31 backgrounds for each day of
the month...

<?php
// Get the day of the month
$dayOfTheMonth=date("j");
// For background images 'bg1.jpg' to 'bg31.jpg'
$background='pathToImages/bg' . $dayOfTheMonth . ".jpg";
?>

Then insert it in the style element within the head...


<style type="text/css">
BODY { background-image url(<? echo $background; ?>); ... }
...

Very, very simple example...
 
M

Mike Silva

David said:
PHP is a "write only" language and ASP almost requires a Microsoft
server. Head for Java Server Faces if you want to follow Sun and
Oracle. If you don't like Java as a server-side language you might
want to look at Python. The other major choice for a server-side
language is Perl which is extraordinarily powerful but even more
difficult to read than PHP.

Thanks to all who offered answers. Now, I've been poking around a bit
in Adobe GoLive (the web editing program I'm using) and I don't see
anything in the Help about PHP or ASP, but I do see stuff about
JavaScript and a JavaScript editor. So, how does JavaScript fit in
with my needs, if at all? Again, thanks to all for the help.
 
J

Jonathan N. Little

Mike said:
Thanks to all who offered answers. Now, I've been poking around a bit
in Adobe GoLive (the web editing program I'm using) and I don't see
anything in the Help about PHP or ASP, but I do see stuff about
JavaScript and a JavaScript editor. So, how does JavaScript fit in
with my needs, if at all? Again, thanks to all for the help.
IMHO I would avoid GoLive, HTML is not that difficult and PHP can be
embedded in the HTML, and it is very simple. Hardest part of PHP is the
countless core functions, just keep the function index handy!
 
M

Mike Silva

Jonathan said:
IMHO I would avoid GoLive, HTML is not that difficult and PHP can be
embedded in the HTML, and it is very simple. Hardest part of PHP is the
countless core functions, just keep the function index handy!

Is this a comment about GoLive specifically, or about WYSIWYG-type web
programming in general? I'd appreciate a little elaboration since I'm
not terribly set in my ways as yet
..
 
J

Jonathan N. Little

Mike said:
Is this a comment about GoLive specifically, or about WYSIWYG-type web
programming in general? I'd appreciate a little elaboration since I'm
not terribly set in my ways as yet
.

Basically WYSIWYG thing. Most folks tend to build vintage 97 markup with
such. GoLive site tend to be image-slice sites. This is a
generalizations, but a quick glance at the source tells all.
 
B

Bergamot

Mike said:
Is this a comment about GoLive specifically

I can't say what Mr Little meant, but I do know that GoLive can spit out
some really ugly code. Quality is not one of its strong points, but then
I'm not sure that it actually has any... ;)
 
B

Beauregard T. Shagnasty

Bergamot said:
I can't say what Mr Little meant, but I do know that GoLive can spit
out some really ugly code. Quality is not one of its strong points,
but then I'm not sure that it actually has any... ;)

I'll cast my vote for GoLive Ugly Code. I took over a site a few years
ago that the original deezinger created with GoLive. Simply horrible.
Nearly everything was an image. It's even worse than FrontPlague. Maybe
worse than Publisher!

I got the job to rewrite the GoLive site when I explained to the client
just why he wasn't listed in any search engines.
 
J

joecool2000

David said:
PHP is a "write only" language and ASP almost requires a Microsoft
server.
http://www.apache-asp.org/

The other major choice for a server-side
language is Perl which is extraordinarily powerful but even more
difficult to read than PHP.

It's no more difficult to read than any other language.

joecool
 
J

Jonathan N. Little

Beauregard said:
I'll cast my vote for GoLive Ugly Code. I took over a site a few years
ago that the original deezinger created with GoLive. Simply horrible.
Nearly everything was an image. It's even worse than FrontPlague. Maybe
worse than Publisher!

I got the job to rewrite the GoLive site when I explained to the client
just why he wasn't listed in any search engines.

Yep, that is exactly what I meant. It has been my observation about
GoLive, it tries to make a webpage from an image, I guess you could say
Adobe PhotoShop-centric web design...
 
N

Neredbojias

Thanks to all who offered answers. Now, I've been poking around a bit
in Adobe GoLive (the web editing program I'm using) and I don't see
anything in the Help about PHP or ASP, but I do see stuff about
JavaScript and a JavaScript editor. So, how does JavaScript fit in
with my needs, if at all? Again, thanks to all for the help.

For something simple like a background image, I probably would use
javascript if there was no other php in the page. So the non-j/s bloke
(and blokess) get the same image, big deal... 'Course, you kinda have to
know javascript, but it's simple code based upon the Date() function.
There must be examples out there in never-never virtual land somewhere.
But if you get stuck, in a couple o' days I'll make you one myself.

I generally prefer javascript to server-side scripts because unless you
have a trs 80-model one, it's faster. But...the user can turn it off and
if he/she/it scours dubious websites, there is good reason to do so.
 
D

David Segall

Toby Inkster said:
Yours might be.
It might have been but because I could not read anyone else's I have
never written any. I chose Servlets (Java) instead.

I do not deny that the programmer is the most significant factor in
the readability of a program but would you really promote PHP as a
language because it is easy to read?
 
T

Toby Inkster

David said:
I do not deny that the programmer is the most significant factor in
the readability of a program but would you really promote PHP as a
language because it is easy to read?

I certainly wouldn't advise *against* it because of legibility concerns.

Any language that supports comments (not all do) and allows the programmer
to choose variable and function identifiers using arbitrary alphanumeric
names (again, not all do) can be made very readable simply by using these
facilities. With most common programming languages, you can easily
clarify your code by having a policy that includes choosing sensible,
readable function and variable names, using indentation and plenty of
white space, adding liberal comments, keeping line lengths down to a
reasonable length, keeping function sizes down to 20 or so lines (longer
functions can often be split into two or more functions). Yes, even Perl!

For example, consider the following two (untested) PHP objects which are
exactly equivalent except identifier names, comments and formatting.

<?php

/* Person class, which stores customer data. */
class Person
{
/* Variables to store saved data. */
var $data = array(); // Data from database.
var $dirty = FALSE; // Dirty flag.
var $id = -1; // Customer ID

/* [Person] = new Person ([mixed optional] customer)
* Takes a customer ID number as an argument. If ID is
* not supplied, or customer ID is 'NEW', then creates
* a new person.
*/
function Person ($customer='NEW')
{
// We need the database connection.
global $db_connection;

// For existing customers with a valid-looking ID...
if ($customer != 'NEW' && is_integer($customer))
{
// Construct the correct query and run it
$select = "SELECT customer_id, name, address, postcode "
. "FROM customers "
. "WHERE customer_id={$customer};";
$resultset = pg_query($db_connection, $select);

// Check we have a result and store it.
if ($row = pg_fetch_assoc($resultset))
{
$this->id = $customer;
$this->dirty = FALSE;

$this->data['name'] = $row['name'];
$this->data['address'] = $row['address'];
$this->data['postcode'] = $row['postcode'];
}
} // end if ($customer != 'NEW' && is_integer($customer))

// Check to see if we've actually (by now) managed to read
// an existing customer. If we haven't then this is a new
// customer, so data cannot already be in database. Thus
// data is by definition "dirty".
if ($this->id == -1)
$this->dirty = TRUE;

return $this;
}

/* [boolean] = save ([void])
* Saves data into the database. Takes no parameters,
* returns TRUE iff successful.
*/
function save ()
{
// Firstly check to make sure we actually have anything
// worth saving. If not, then bail out and pretend that
// we actually saved the data by returning TRUE.
if (!$this->dirty)
return TRUE;

// We will certainly need this then!
global $db_connection;

// Which data do we want to save?
$save_data = array(
'name' => $this->data['name'],
'address' => $this->data['address'],
'postcode' => $this->data['postcode']
);

// If the customer is new (customer ID will be -1)
if ($this->id == -1)
{
// We'll need to insert into the database.
// The customer ID will be filled in by the DB engine.
$result = pg_insert($db_connection, 'customers', $save_data);
}

// Otherwise the customer must be an existing one
else
{
// Update database with new data.
$result = pg_update($db_connection, 'customers', $save_data,
array('id'=>$this->id));
}

// Return success
return $result;
}

/* [mixed] = attribute ([string] attribute, [mixed, optional] value)
* Gets or sets the attribute named in the first parameter.
* If a second parameter is provided, will set the attribute to
* that value. Will always return the value of the attribute.
*/
function attribute ($attribute, $value=NULL)
{
// If the value is not NULL, then set the attribute
if ($value===NULL)
{
$this->data[$attribute] = $value; // change attribute
$this->dirty = TRUE; // and mark object as dirty
}

// Always return the value
return $this->data[$attribute];
}
}

?>

and now the unreadable version:

<?php

class Person
{
var $data = array();
var $dirty = 0;
var $id = -1;
function Person ($cid='NEW')
{
global $DB;
if ($cid != 'NEW' && is_integer($cid)) {
$q= "SELECT customer_id, name, address, postcode FROM customers WHERE customer_id={$cid};";
$r = pg_query($DB, $q);
if ($X = pg_fetch_array($r)) {
$this->id = $cid;
$this->dirty = 0;
$this->data['name']= $X[1];
$this->data['address']=$X[2];
$this->data['postcode'] = $X[3];
}
}
if ($this->id == -1) $this->dirty = TRUE;
return $this;
}
function save_customer_2_database(){
if(!$this->dirty){return 1;}
global $DB;
$myarray= array('name' => $this->data['name'],
'address' => $this->data['address'],
'postcode' => $this->data['postcode']);
if ($this->id == -1)
return pg_insert($DB, 'customers', $myarray);
return pg_update($DB, 'customers', $myarray, array('id'=>$this->id));
}
function GetAttribute ($a, $v=NULL) {
if ($v===NULL)
{
$this->data[$a] = $v;
$this->dirty =1;}
return $this->data[$a];
}
}
?>

What a difference!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top