Forum Moderators: open

Message Too Old, No Replies

Help needed for images and links to change on page refresh

help needed with code

         

Sweetlywrapped

4:29 pm on Jan 11, 2011 (gmt 0)

10+ Year Member



Hi people,

I've recently began designing a site using dreamweaver, but despite my efforts I have hit a wall with a certain attribute that I would like applied to the site.
I won't pretend to know a lot or even a moderate amount about Javascript, so please bear with me. I am reading and learning as quickly as possible.

On two or three pages I have a centre column filled with content. On either side of the content I have 3-4 product images that when clicked take you to a page detailing the specific image you click on.

I am trying to find code that would allow those images to change when the page is refreshed and the links to change along with them.

I am presuming this can be done using Javascript, but please let me know if I am wrong.

Any advice would be taken in kind.


Cheers.

Fotiman

4:49 pm on Jan 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



First, welcome to WebmasterWorld! :)

You probably wouldn't want to do that with JavaScript. You would be better off doing this with a server side language like PHP. If you try to do this with JavaScript, it means users with JavaScript disabled (and search engines) won't be able to see those links/images.

Sweetlywrapped

8:57 pm on Jan 17, 2011 (gmt 0)

10+ Year Member



Sorry for the late response.

Thanks for the advice. Any chance you could point me in the right direction of where i could find the PHP code to do this?
I would love to say I can code it myself using PHP script but I've only been teaching myself this web design and coding for a few weeks. still getting my head round JS at the minute.

Fotiman

9:26 pm on Jan 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



First, I would look into PHP Arrays [php.net], as you'll need to store your links in some collection that you can access randomly.

Then you'll want to look into the rand method [us3.php.net] to get an number that falls within your array. That will act as the index to retrieve one of the records.

<?php
// create an array of links
$arr = array(0 => array('url' => 'http://example.com', 'text' => 'Example Com'),
1 => array('url' => 'http://example.net', 'text' => 'Example Net'));
// pick a link at ramdom
$n = rand(0, count($arr) - 1);
$a = $arr[$n];
// print the link
echo "<a href='" . $a['url'] . "'>" . $a['text'] . "</a>";
?>

I'm not sure if that is exactly right, but should give you the basic idea.