Click to See Complete Forum and Search --> : convert simple javascript code to PHP


BigMoosie
03-29-2005, 12:50 AM
Hi, I'm just starting with PHP and wish to have a random image on a page. I have tried searching these forums but have been quite confused. I have written what I wish to create in JavaScript so you can see what kind of effect I'm after:

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>

<body>
<Script>
var picPath=new Array("pic1.jpg", "pic2.jpg", "pic3.jpg")
var picWidth=new Array(120, 220, 175);
var picHeight=new Array(65, 98, 160);

var n=Math.floor(Math.random()*picPath.length);

document.write('<img src='+picPath[n]+' width='+picWidth[n]+' height='+picHeight[n]+'>');

</script>
</body>

</html>


Can somebody please rewrite this in PHP, it would be a huge help and would allow me to understand probably the most important features (for me) of PHP.

Thankyou for you time.

-Benjamin

ShrineDesigns
03-29-2005, 01:08 AM
<?php
$images = array
(
'image.jpg',
'image2.jpg',
'image3.jpg'
// etc ...
);
$i = mt_rand(0, count($images) - 1);
$image = $images[$i];
list(, , , $wh) = getimagesize($image);
?>
<img src="<?php echo $image; ?>"<?php echo $wh; ?> alt="" />

BigMoosie
03-29-2005, 01:10 AM
thankyou very very much ShrineDesigns thats perfect!

ShrineDesigns
03-29-2005, 01:11 AM
your welcome
it was no problem

BigMoosie
03-29-2005, 01:14 AM
hmmm... i uploaded that image here: Online Test (http://www.google.rollingtank.com/test.php) yet it displays the words: 403 Forbidden. The file is CHMOD: 664, is this the problem. Once again i know this probably seems very basic but i very much appreciate it.

ShrineDesigns
03-29-2005, 01:32 AM
hmm...

weird, on my site my php files are chmod'd to 644, and they work just fine, does your host support php?

is this the url to your site: http://www.rollingtank.com/
when i go here: http://www.google.rollingtank.com/ i get some weird google page?!?!?

BigMoosie
03-29-2005, 01:36 AM
hmmm... probably not, actually now that i think about it, this was the reason that stopped me learning this language like a year ago. It is a pity, i would really like to utilize PHP's capabilities,

i appologize for wasting your time

ShrineDesigns
03-29-2005, 01:41 AM
you can setup a server on your home computer for testing php scripts, i as well as many others have done this

it's a convienent way to test your scripts without having to pay for host and use up bandwidth re uploading your scripts for every change you make

BigMoosie
03-29-2005, 04:06 AM
that sounds really good, how can I go about making my computer a server?

ShrineDesigns
03-29-2005, 04:35 AM
download:
apache http server: http://httpd.apache.org/
php: http://www.php.net/ (DON'T download the installer)
mysql: http://www.mysql.com/ (optional, if you want database support, choose mysql 4.0.x not 4.1.x)

install apache (if you use the installer set the domain name or ip to localhost or 127.0.0.1), then extract php, open and follow the install.txt

run apache, (if no errors popped up) open a browser window and type in http://localhost or http://127.0.0.1 and you should see the good ol' apache logo

BigMoosie
03-29-2005, 04:48 AM
Thanks alot for those links, I think I will install MySQL just to see its capabilities.

Are these technologies sufficient to create a shopping cart that does not use cookies? If so that would be really great.

I'm glad I have found this out because I have never know a way of me actually testing PHP files, so once again I am very grateful.

- Benjamin

BigMoosie
03-29-2005, 04:52 AM
hmm...

weird, on my site my php files are chmod'd to 644, and they work just fine, does your host support php?

is this the url to your site: http://www.rollingtank.com/
when i go here: http://www.google.rollingtank.com/ i get some weird google page?!?!?

sorry, somehow i did not see your post, that is my brothers website and the google thing is just a little thing im mucking around with, it has been quite popular, had 6,000 hits overnight (900 of which are individual people)!!!

the google thing is actually what i first asked this for, that page has a random image javascript which i was hoping to replace with php...

BigMoosie
03-29-2005, 05:08 AM
With this "Apache", I think I have downloaded the source code. Is that what I was suposed to do? I have unzipped it but am unsure as to what to do next...

ShrineDesigns
03-29-2005, 02:58 PM
for windows: get the MSI installer (*.msi)
for linux: get the rpm or .gz

BigMoosie
04-01-2005, 02:02 AM
my friend told me that Apache 1.3.33 is better cos the new one is all bugged up, is this true?

solomon
04-01-2005, 01:24 PM
Depending on how far down the line of installing you are... I found phptriad (http://sourceforge.net/projects/phptriad) very handy. It's Apache, php and MySQL all in one easy to install package.

ShrineDesigns
04-01-2005, 02:06 PM
i prefer apache 1.3, i haven't used apache 2 yet, and php recommends using apache 1.3