1

I have scoured google with this question and read through many articles, tutorials, PHP guides and other peoples questions. But not a single solution has worked for me so far. I know it must be something simple.

This is my code:

<?php
$random=array_rand("http://www.mentor-distribution.com/media/gmaster_header.mp4", "http://www.mentor-distribution.com/media/hs_freedom_chair.mp4");
?>


<html>
<body>
<iframe width="320" height="187" frameborder="0" src="<?php echo htmlspecialchars($random); ?>"></iframe>
</body>
</html>

But this is how it appears on inspect element: Chrome's inspect element output

Any help will be greatly appreciated.

1

1 Answer 1

1

Your array_rand call is wrong. Use:

<?php
$array = [
    "http://www.mentor-distribution.com/media/gmaster_header.mp4",
    "http://www.mentor-distribution.com/media/hs_freedom_chair.mp4"
];
$randomIndex = array_rand($array);
$random = $array[$randomIndex];
?>

The documentation (http://php.net/array_rand) states that the first argument of array_rand should be an array. You provided a string.

When in doubt, be paranoid. Check if every variable has the value you expect it to have, using var_dump for instance. In this case you would have discovered that $random wasn't set to a valid url.

One final note is that I would refrain from using <iframe>s as video players. Look into the HTML5 <video> element, as explained on http://www.w3schools.com/html/html5_video.asp

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the near instant reply. the iframe is now loading but the iframe src is now displaying as either a 0 or a 1 when loaded and not one of the url's. which means it returns a 404 error inside the iframe.
You are right. array_rand returns a random index. I'll edit my answer accordingly.
thanks for the advice, i would love to use <video> tags but i can use the as something on our Magento store blocks them, the store was setup before i began working here so i dont know why it happens.
If my answer helped you solve the problem, please mark it as accepted for future reference :)

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.