Click to See Complete Forum and Search --> : My first ever PHP code!
I've been to W3schools and have taught myself some PHP.
I needed a random background for a table.
I copied bits of code from here and there.
Erm... I was hoping someone could comment on my code:
<?php
$backgrounds[0] = "background1.jpg";
$backgrounds[1] = "background2.jpg";
$backgrounds[2] = "background3.jpg";
$backgrounds[3] = "background4.jpg";
$backgrounds[4] = "background5.jpg";
$backgrounds[5] = "background6.jpg";
$backgrounds[6] = "background7.jpg";
$numberOfBackgrounds = count($backgrounds) - 1;
$randomNumber = rand(0, $numberOfBackgrounds);
echo "<table width=\"750\" height=\"550\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" background=\"files/images/backgrounds/" . $backgrounds[$randomNumber] ."\">";
?>
And it works! Yee ha! :)
Was this the best way to code what I wanted?
Any feedback would be appreciated.
Thanks.
OM
NightShift58
01-11-2007, 06:29 PM
I would added another line at the beginning to make sure I didn't received a warning message:$backgrounds = array();Someone might pop in and tell you that this is better:$backgrounds = array("background1.jpg", "background2.jpg", "background3.jpg", "background4.jpg", "background5.jpg", "background6.jpg", "background7.jpg");But the format you chose makes for better reading and PHP couldn't really care.
It's just another way to code it and since PHP doesn't care, you should always opt for the option that makes it easier to read, maintain and debug your scripts.
NogDog
01-11-2007, 07:08 PM
Another array format option (sort of the best of both ways):
$backgrounds = array(
"background1.jpg",
"background2.jpg",
"background3.jpg",
"background4.jpg",
"background5.jpg",
"background6.jpg",
"background7.jpg"
);
kewl.
thanks for all the help.
if any of u guys need any help with programming php, give me a shout.
(joke.) :)
NightShift58
01-11-2007, 07:30 PM
...sort of the best of both ways...I should've put money on that one... :)
I disagree but, I admit, it's somewhat subjective. But please look at the mistakes made in this forum. A good number of the errors have their origin in Copy&Paste. And these are just the reported ones...
That which should be the preferred form, in this case $backgrounds[0] = "background1.jpg";, lends itself to copy, duplicating and deleting without too many problems. The lesser form does not have a comma in the final element, the final parenthese is on a separate line, etc.
It's not really a big issue but defensive driving never hurt anyone and just as one tries to insulate one's self from logic error, the same should apply to potential "mechanical" error - for lack of a better word.
And besides, in COBOL, that's the way it's done... But you knew that might be coming... :)
NogDog
01-11-2007, 09:09 PM
My number one preferred method for creating such a data structure would be to store the data elements in a database, then build the array as needed via a query. Then if I need to add items to the list or delete them, I just do it in the database and don't have to worry about breaking my code when I edit it. :)
MrCoder
01-11-2007, 09:10 PM
Use suffle() if you want to be diffrent :P
<?php
$backgrounds = array( "background1.jpg",
"background2.jpg",
"background3.jpg",
"background4.jpg",
"background5.jpg",
"background6.jpg",
"background7.jpg");
suffle($backgrounds);
?>
<table width="750" height="550" border="0" align="center" cellpadding="0" cellspacing="0" background="files/images/backgrounds/<?php echo $backgrounds[0]; ?>">
NightShift58
01-11-2007, 10:32 PM
My number one preferred method...You give me no choice but to agree with you...
NogDog
01-12-2007, 01:04 AM
You give me no choice but to agree with you...
Does it hurt? :p
NightShift58
01-12-2007, 01:06 AM
Feelin' no pain... could be the Costa Rican brew...
My number one preferred method for creating such a data structure would be to store the data elements in a database, then build the array as needed via a query. Then if I need to add items to the list or delete them, I just do it in the database and don't have to worry about breaking my code when I edit it. :)
erm... i haven't ventured into working with databases yet.
i needed to create the random thing quickly... and stopped reading at w3schools.
if it won't take more than 60 secs of code to write... would you be tempted to give me the code that i would use for accessing through a db?
erm... acutally... that opens a can of worms for me:
- setting up the db? how? through my control panel? or done through php?
- adding to the database? how? i take it i'd need a gui script that allowed me to upload to the db?
- then you have security? i'm sure it's all standard... but how do i hide the username and password for the db?
hmmm, sound like a big job to me...?
Use suffle() if you want to be diffrent :P
<?php
$backgrounds = array( "background1.jpg",
"background2.jpg",
"background3.jpg",
"background4.jpg",
"background5.jpg",
"background6.jpg",
"background7.jpg");
suffle($backgrounds);
?>
<table width="750" height="550" border="0" align="center" cellpadding="0" cellspacing="0" background="files/images/backgrounds/<?php echo $backgrounds[0]; ?>">
i think that's my fav one. :)
i wasn't sure/didn't realise you could have <?php echo $backgrounds[0]; ?> in the middle of the code.
thanks.
NightShift58
01-12-2007, 03:43 AM
Use suffle() if you want to be diffrent :P
But to really, really be different... use shuffle() instead...:)
MrCoder
01-12-2007, 04:55 AM
My H key is broke!!
NightShift58
01-12-2007, 05:02 AM
I'll shuffle one over to you for Xmas...