0

I would like to convert this javascript (which works correctly) into a PHP function, so that all this code does not have to appear in the source code of the webpage.

<script type="text/javascript">

    var images = [],
    index = 0;

    images[0] = "<a href = 'http://www.random_target1.com' target='_blank'><img src='//www.random_target1.com/random_banner.jpg' width='120' /></a>";

    images[1] = "<a href = 'http://www.random_target2.com' target='_blank'><img src='//www.random_target2.com/random_banner.jpg' width='120' /></a>";

    images[2] = "<a href = 'http://www.random_target3.com' target='_blank'><img src='//www.random_target3.com/random_banner.jpg' width='120' /></a>";

    index = Math.floor(Math.random() * images.length);
    document.write(images[index]);

</script>

I tried this PHP function in the PHP file, and thought I could call its result, from the HTML file, so only determined value would appear in HTML source code. This code broke the page, though.

public function getRandom()
{
    $images = array();
    $index = 0;

    $images[0] = "<a href = 'http://www.random_target1.com' target='_blank'><img src='//www.random_target1.com/random_banner.jpg' width='120' /></a>";

    $images[1] = "<a href = 'http://www.random_target2.com' target='_blank'><img src='//www.random_target2.com/random_banner.jpg' width='120' /></a>";

    $images[2] = "<a href = 'http://www.random_target3.com' target='_blank'><img src='//www.random_target3.com/random_banner.jpg' width='120' /></a>";

    $index = Math.floor(Math.random() * $images.length);
    return $images[$index];
}   
3
  • 2
    have you tried anything by yourself? Commented Jan 9, 2012 at 9:36
  • Whether you have this as JavaScript or PHP, it will end up in the webpage hence being part of source code. Based on your requirements, you would need to redesign your App if you want to prevent this code to appear on client page webpage. Commented Jan 9, 2012 at 9:38
  • Math.floor() and Math.random() aren't PHP functions, they're exclusively JavaScript functions. You should read the errors that PHP throws. Commented Jan 9, 2012 at 10:05

2 Answers 2

1
$images = array(
"<a href = 'http://www.random_target1.com' target='_blank'><img src='//www.random_target1.com/random_banner.jpg' width='120' /></a>",
 "<a href = 'http://www.random_target1.com' target='_blank'><img src='//www.random_target1.com/random_banner.jpg' width='120' /></a>",
 "<a href = 'http://www.random_target1.com' target='_blank'><img src='//www.random_target1.com/random_banner.jpg' width='120' /></a>"
);
$index = rand(0, count($images)-1);

echo $images[$index];
Sign up to request clarification or add additional context in comments.

Comments

1

Note I use mt_rand instead of rand. It's generally considered a better random number function.

<?php 
$images = array();

$images[0] = "<a href = 'http://www.random_target1.com' target='_blank'><img src='//www.random_target1.com/random_banner.jpg' width='120' /></a>";

$images[1] = "<a href = 'http://www.random_target2.com' target='_blank'><img src='//www.random_target2.com/random_banner.jpg' width='120' /></a>";

$images[2] = "<a href = 'http://www.random_target3.com' target='_blank'><img src='//www.random_target3.com/random_banner.jpg' width='120' /></a>";

$randIndex = mt_rand(0,sizeof($images)-1);

echo $images[$randIndex];
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.