ricky spires Posted November 19, 2012 Share Posted November 19, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/270903-array-not-displaying-correctly/ Share on other sites More sharing options...
Barand Posted November 19, 2012 Share Posted November 19, 2012 Define "doesn't work". What is happening that shouldn't? What isn't happening that should? Quote Link to comment https://forums.phpfreaks.com/topic/270903-array-not-displaying-correctly/#findComment-1393530 Share on other sites More sharing options...
AyKay47 Posted November 19, 2012 Share Posted November 19, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/270903-array-not-displaying-correctly/#findComment-1393558 Share on other sites More sharing options...
deoiub Posted November 19, 2012 Share Posted November 19, 2012 (edited) 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 November 19, 2012 by deoiub Quote Link to comment https://forums.phpfreaks.com/topic/270903-array-not-displaying-correctly/#findComment-1393566 Share on other sites More sharing options...
ricky spires Posted November 19, 2012 Author Share Posted November 19, 2012 hmmm. thanks for your comments. i think i need to have a second look at the code. ill keep you posted . thanks Quote Link to comment https://forums.phpfreaks.com/topic/270903-array-not-displaying-correctly/#findComment-1393583 Share on other sites More sharing options...
ricky spires Posted November 19, 2012 Author Share Posted November 19, 2012 ok... scrap that. this is all i need <?php $i=1; while($i<=5) { $no = (rand(1,5)); echo "The number is " . $no . "<br />"; $i++; } ?> that gives me 5 results showing a random number thanks Quote Link to comment https://forums.phpfreaks.com/topic/270903-array-not-displaying-correctly/#findComment-1393610 Share on other sites More sharing options...
ricky spires Posted November 19, 2012 Author Share Posted November 19, 2012 damm. now every now and again a div disapears like its looking for 0 or 5 ? Quote Link to comment https://forums.phpfreaks.com/topic/270903-array-not-displaying-correctly/#findComment-1393611 Share on other sites More sharing options...
AyKay47 Posted November 19, 2012 Share Posted November 19, 2012 Post the updated code. Quote Link to comment https://forums.phpfreaks.com/topic/270903-array-not-displaying-correctly/#findComment-1393635 Share on other sites More sharing options...
ricky spires Posted November 20, 2012 Author Share Posted November 20, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/270903-array-not-displaying-correctly/#findComment-1393722 Share on other sites More sharing options...
AyKay47 Posted November 20, 2012 Share Posted November 20, 2012 What does a view source show on the blank div? Quote Link to comment https://forums.phpfreaks.com/topic/270903-array-not-displaying-correctly/#findComment-1393771 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.