Hi i am trying to add a for each inside a while loop this is how I want to work the while loop retrieve the above from the database <li><a href="#tabs-<?php echo get_the_ID(); ?>" onclick="jwplayer('mediaplayer_vid_1').stop();jwplayer('mediaplayer_vid_2').stop();jwplayer('mediaplayer_vid_3').stop();jwplayer('mediaplayer_vid_4').stop();"><?php echo get_the_post_thumbnail( $post_id, 'story-thumbnail'); ?></a></li> PHP: so it display the above various time But I am trying to achive the above plus a the foreach inside the onclick because i want to for each post to increment the number inside the onclick mediaplayer_vid_1, mediaplayer_vid_2, mediaplayer_vid_3 so i have the foreach loop that increment by 1 <?php foreach ($posts as $post) $post_count++; ?> <?php echo "jwplayer('mediaplayer_vid_3').stop()". $post_count; ?> PHP: I want that to be inside the onclick I have tried like this but does not work anyone can help me out <li><a href="#tabs-<?php echo get_the_ID(); ?>" onclick="<?php foreach ($posts as $post) $post_count++; echo "jwplayer('mediaplayer_vid_ '.$post_count.').stop()"; ?>><?php echo get_the_post_thumbnail( $post_id, 'story-thumbnail'); ?></a></li> PHP: Thanks in advance
Your code makes absolutely no sense, no offense. Why are you trying to play 4 videos at once? Please explain what you need to achieve, because I suspect you are going about it wrong.
Hi sorry what i meant is how can i add the foreach lopp inside the onclick like this? at moment this is how it is you see the onclick (mediaplayer_vid_1) the number I want to be incremented with for each loop so it would go (mediaplayer_vid_1) ((mediaplayer_vid_2) ((mediaplayer_vid_3) and so on <li><a href="#" onclick="jwplayer('mediaplayer_vid_1').stop();"><?php echo get_the_post_thumbnail( $post_id, 'story-thumbnail'); ?></a></li> PHP: onclick=" <?php foreach ($posts as $post) $post_count++; ?> <?php echo $post_count; ?>" PHP:
STOP opening and closing PHP willy-nilly like that -- 99% of your convoluted coding seems to stem from this... this is PARTICULARLY true when you're putting ?><?php for no good reason... IF I'm reading what you want to do properly... the foreach involves some overhead, pulling COUNT would be a better option. ( assumes we're already <?php ) echo '<li><a href="#" onclick="'; $count=count($posts); while ($count>0) { echo 'jwplayer(\'mediaplayer_vid_',$count,'\').stop();'; $count--; } echo ' return false;">',get_the_post_thumbnail( $post_id, 'story-thumbnail'),'</a></li>'; Code (markup): Would actually put them in reverse order 4 to 1, but I imagine that really doesn't matter for you. I'd also do the return false just so that hash doesn't actually trip... Though to be frank, 'onclick' is outdated and you're putting in code people with scripting disabled won't be able to use -- really these types of things should be applied client-side IMHO.