Jump to content

Recommended Posts

hello,

 

could someone please help me with this array.

 

what i what is 4 images displayed on the page which change randomly.

 

each image has is own audio on hover and plays video on click (no included in this code).

 

 

 

<?php
function random_ads($no){
$number = array(
"1",
"2",
"3",
"4",
);
for($y=0;$y<=$no;$y++){
$rand_no = array_rand($number, 2);
$no=$number[$rand_no[1]];
$random_vid.='
<div id="videoHolder" class="vid'.$no.'">
<audio id="audio'.$no.'" preload="auto">
<source src="assets/audio/audio'.$no.'.mp3" type="audio/mpeg" ></source>
<source src="assets/audio/audio'.$no.'.ogg" type="audio/ogg" ></source>
</audio>
<img src="assets/images/pic'.$no.'.jpg" alt="picture'.$no.'">
</div>';
}
return $random_vid;
}
?>
<div id="heart">
<?php
echo random_ads(4); // 2 = number of ads displayed
?>
</div>

 

i think im putting the wrong number in the brackets but it doesnt seem to work ?

 

thanks

ricky

Link to comment
https://forums.phpfreaks.com/topic/270903-array-not-displaying-correctly/
Share on other sites

As barand has stated, we need more information pertaining to your issue in order to give you an adequate answer.

However I do notice some things right off the bat:

 

You are using the concatenation operator .= on $random_vid which does not yet exist. This will trigger an E_USER_NOTICE error.

Why do you pick 2 random numbers when you are only using one?

In additional to AyKay47's comments, you are also reassigning the value of $no inside of for loop.

 

for($y=0;$y<=$no;$y++){
...
$no=$number($rand_no[1]];
...
...
}

 

if your first random number is 1, your for loop 'while' condition is false on the second pass and the code drops out of the for loop.

 

Your loop will randomly execute between 1 and 5 times (as you start with 0), and on each loop randomly display one of the images, potentially the same image each time.

 

- D

Edited by deoiub

this is the code now:

 

<?php
$i=1;
while($i<=4)
 {
 $no = (rand(1,4));

 echo'<div id="videoHolder" class="vid'.$no.'">
 <img src="assets/images/pic'.$no.'.jpg" alt="picture'.$no.'">'.$no.'
 </div>';
 $i++;
 }
?>

 

i get a blank div every now and then

 

thanks

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.