/    Sign up×
Community /Pin to ProfileBookmark

convert simple javascript code to PHP

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:

[CODE]<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>
[/CODE]

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

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsMar 29.2005 — [code=php]<?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="" />[/code]
Copy linkTweet thisAlerts:
@BigMoosieauthorMar 29.2005 — thankyou very very much ShrineDesigns thats perfect!
Copy linkTweet thisAlerts:
@ShrineDesignsMar 29.2005 — your welcome

it was no problem
Copy linkTweet thisAlerts:
@BigMoosieauthorMar 29.2005 — hmmm... i uploaded that image here: [URL=http://www.google.rollingtank.com/test.php]Online Test[/URL] 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.
Copy linkTweet thisAlerts:
@ShrineDesignsMar 29.2005 — 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?!?!?
Copy linkTweet thisAlerts:
@BigMoosieauthorMar 29.2005 — 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
Copy linkTweet thisAlerts:
@ShrineDesignsMar 29.2005 — 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
Copy linkTweet thisAlerts:
@BigMoosieauthorMar 29.2005 — that sounds really good, how can I go about making my computer a server?
Copy linkTweet thisAlerts:
@ShrineDesignsMar 29.2005 — 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
Copy linkTweet thisAlerts:
@BigMoosieauthorMar 29.2005 — 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
  • Copy linkTweet thisAlerts:
    @BigMoosieauthorMar 29.2005 — 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?!?!?[/QUOTE]


    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...
    Copy linkTweet thisAlerts:
    @BigMoosieauthorMar 29.2005 — 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...
    Copy linkTweet thisAlerts:
    @ShrineDesignsMar 29.2005 — for windows: get the MSI installer (*.msi)

    for linux: get the rpm or .gz
    Copy linkTweet thisAlerts:
    @BigMoosieauthorApr 01.2005 — my friend told me that Apache 1.3.33 is better cos the new one is all bugged up, is this true?
    Copy linkTweet thisAlerts:
    @solomonApr 01.2005 — Depending on how far down the line of installing you are... I found [URL=http://sourceforge.net/projects/phptriad]phptriad[/URL] very handy. It's Apache, php and MySQL all in one easy to install package.
    Copy linkTweet thisAlerts:
    @ShrineDesignsApr 01.2005 — i prefer apache 1.3, i haven't used apache 2 yet, and php recommends using apache 1.3
    ×

    Success!

    Help @BigMoosie spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...