lspiehler Posted November 3, 2010 Share Posted November 3, 2010 I figure I'd use array_chunk to divide my array by 5's, but how can I get each set of five to be enclosed in div tags. My array is a list of images in a directory which I need inserted into a format like the example code on this page. http://flowplayer.org/tools/demos/scrollable/index.html. Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/217693-seperate-array-by-5s-into-individual-div-containers/ Share on other sites More sharing options...
Andy-H Posted November 3, 2010 Share Posted November 3, 2010 $array = array( '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'); $array = array_chunk($array, 5); foreach($array as $arr) { echo '<div>' . "\r\n" . implode("\r\n", $arr) . "\r\n" . '</div>' . "\r\n\r\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/217693-seperate-array-by-5s-into-individual-div-containers/#findComment-1130017 Share on other sites More sharing options...
lspiehler Posted November 3, 2010 Author Share Posted November 3, 2010 Thanks for the super fast reply, but this code echoes "<div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div><div>7</div><div>8</div><div>9</div><div>10</div>". In my case I would need it to separate like so. "<div>12345</div><div>678910</div>Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/217693-seperate-array-by-5s-into-individual-div-containers/#findComment-1130023 Share on other sites More sharing options...
Andy-H Posted November 3, 2010 Share Posted November 3, 2010 Sorry, I realised that and edited my post before you posted the reply. Quote Link to comment https://forums.phpfreaks.com/topic/217693-seperate-array-by-5s-into-individual-div-containers/#findComment-1130026 Share on other sites More sharing options...
lspiehler Posted November 3, 2010 Author Share Posted November 3, 2010 Awesome! It looks like I need more help though. I'm really new to this. After reading about the implode function, it looks like the implode function can only work with a variable and I need to be able to echo lots of html there. Let me see if I can be very specific. My current code without the div stuff i want to do is: foreach ($myfile as $v) { echo "<a href=\"http://mysite.com/index.php?cat=family&video={$v}\" onmouseover=\"{$v}.src={$v}loadImage1.src;\" onmouseout=\"{$v}.src={$v}staticImage1.src;\"> <img name=\"{$v}\" src=\"http://mysite.com/thumbs/{$v}/_002.jpg\" border=0></a>\n"; } As you can see there is lots of additional html and the same variable is called several times in each block of html. The extra javascript causes a mouseover event to change the jpg to a gif to play a sort of video preview. The html currently shows up like this: <a href="http://mysite.com/index.php?cat=family&video=1" onmouseover="1.src=1loadImage1.src;" onmouseout="1.src=1staticImage1.src;"> <img name="1" src="http://mysite.com/thumbs/1/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=2" onmouseover="2.src=2loadImage1.src;" onmouseout="2.src=2staticImage1.src;"> <img name="2" src="http://mysite.com/thumbs/2/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=3" onmouseover="3.src=3loadImage1.src;" onmouseout="3.src=3staticImage1.src;"> <img name="3" src="http://mysite.com/thumbs/3/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=4" onmouseover="4.src=4loadImage1.src;" onmouseout="4.src=4staticImage1.src;"> <img name="4" src="http://mysite.com/thumbs/4/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=5" onmouseover="5.src=5loadImage1.src;" onmouseout="5.src=5staticImage1.src;"> <img name="5" src="http://mysite.com/thumbs/5/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=6" onmouseover="6.src=6loadImage1.src;" onmouseout="6.src=6staticImage1.src;"> <img name="6" src="http://mysite.com/thumbs/6/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=7" onmouseover="7.src=7loadImage1.src;" onmouseout="7.src=7staticImage1.src;"> <img name="7" src="http://mysite.com/thumbs/7/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=8" onmouseover="8.src=8loadImage1.src;" onmouseout="8.src=8staticImage1.src;"> <img name="8" src="http://mysite.com/thumbs/8/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=9" onmouseover="9.src=9loadImage1.src;" onmouseout="9.src=9staticImage1.src;"> <img name="9" src="http://mysite.com/thumbs/9/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=10" onmouseover="10.src=10loadImage1.src;" onmouseout="10.src=10staticImage1.src;"> <img name="10" src="http://mysite.com/thumbs/10/_002.jpg" border=0></a> But I need each 5 blocks of html to have divs around them like this: <div> <a href="http://mysite.com/index.php?cat=family&video=1" onmouseover="1.src=1loadImage1.src;" onmouseout="1.src=1staticImage1.src;"> <img name="1" src="http://mysite.com/thumbs/1/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=2" onmouseover="2.src=2loadImage1.src;" onmouseout="2.src=2staticImage1.src;"> <img name="2" src="http://mysite.com/thumbs/2/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=3" onmouseover="3.src=3loadImage1.src;" onmouseout="3.src=3staticImage1.src;"> <img name="3" src="http://mysite.com/thumbs/3/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=4" onmouseover="4.src=4loadImage1.src;" onmouseout="4.src=4staticImage1.src;"> <img name="4" src="http://mysite.com/thumbs/4/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=5" onmouseover="5.src=5loadImage1.src;" onmouseout="5.src=5staticImage1.src;"> <img name="5" src="http://mysite.com/thumbs/5/_002.jpg" border=0></a> </div> <div> <a href="http://mysite.com/index.php?cat=family&video=6" onmouseover="6.src=6loadImage1.src;" onmouseout="6.src=6staticImage1.src;"> <img name="6" src="http://mysite.com/thumbs/6/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=7" onmouseover="7.src=7loadImage1.src;" onmouseout="7.src=7staticImage1.src;"> <img name="7" src="http://mysite.com/thumbs/7/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=8" onmouseover="8.src=8loadImage1.src;" onmouseout="8.src=8staticImage1.src;"> <img name="8" src="http://mysite.com/thumbs/8/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=9" onmouseover="9.src=9loadImage1.src;" onmouseout="9.src=9staticImage1.src;"> <img name="9" src="http://mysite.com/thumbs/9/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=10" onmouseover="10.src=10loadImage1.src;" onmouseout="10.src=10staticImage1.src;"> <img name="10" src="http://mysite.com/thumbs/10/_002.jpg" border=0></a> </div> Do you know how I can accomplish this? Thanks a lot!!!! Quote Link to comment https://forums.phpfreaks.com/topic/217693-seperate-array-by-5s-into-individual-div-containers/#findComment-1130035 Share on other sites More sharing options...
Andy-H Posted November 3, 2010 Share Posted November 3, 2010 $myfile = array_chunk($myfile, 5); foreach ($myfile as $v) { echo "\t" . '<div>' . "\r\n"; foreach($v as $val) { printf("\t\t" . '<a href="http://mysite.com/index.php?cat=family&video=%1$d" onmouseover="%1$d.src=%1$dloadImage1.src;" onmouseout="%1$d.src=%1$dstaticImage1.src;"> <img name="%1$d" src="http://mysite.com/thumbs/%1$d/_002.jpg" border=0></a>' . "\r\n", $val); } echo "\t" . '</div>' . "\r\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/217693-seperate-array-by-5s-into-individual-div-containers/#findComment-1130039 Share on other sites More sharing options...
lspiehler Posted November 3, 2010 Author Share Posted November 3, 2010 The html is formatting great, but where you replaced the variable with "%1$d" the html only shows "0" instead of the values in the array. Here's the source: <div> <a href="http://mysite.com/index.php?cat=family&video=0" onmouseover="0.src=0loadImage1.src;" onmouseout="0.src=0staticImage1.src;"> <img name="0" src="http://mysite.com/thumbs/0/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=0" onmouseover="0.src=0loadImage1.src;" onmouseout="0.src=0staticImage1.src;"> <img name="0" src="http://mysite.com/thumbs/0/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=0" onmouseover="0.src=0loadImage1.src;" onmouseout="0.src=0staticImage1.src;"> <img name="0" src="http://mysite.com/thumbs/0/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=0" onmouseover="0.src=0loadImage1.src;" onmouseout="0.src=0staticImage1.src;"> <img name="0" src="http://mysite.com/thumbs/0/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=0" onmouseover="0.src=0loadImage1.src;" onmouseout="0.src=0staticImage1.src;"> <img name="0" src="http://mysite.com/thumbs/0/_002.jpg" border=0></a> </div> <div> <a href="http://mysite.com/index.php?cat=family&video=0" onmouseover="0.src=0loadImage1.src;" onmouseout="0.src=0staticImage1.src;"> <img name="0" src="http://mysite.com/thumbs/0/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=0" onmouseover="0.src=0loadImage1.src;" onmouseout="0.src=0staticImage1.src;"> <img name="0" src="http://mysite.com/thumbs/0/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=0" onmouseover="0.src=0loadImage1.src;" onmouseout="0.src=0staticImage1.src;"> <img name="0" src="http://mysite.com/thumbs/0/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=0" onmouseover="0.src=0loadImage1.src;" onmouseout="0.src=0staticImage1.src;"> <img name="0" src="http://mysite.com/thumbs/0/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=0" onmouseover="0.src=0loadImage1.src;" onmouseout="0.src=0staticImage1.src;"> <img name="0" src="http://mysite.com/thumbs/0/_002.jpg" border=0></a> </div> <div> <a href="http://mysite.com/index.php?cat=family&video=0" onmouseover="0.src=0loadImage1.src;" onmouseout="0.src=0staticImage1.src;"> <img name="0" src="http://mysite.com/thumbs/0/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=0" onmouseover="0.src=0loadImage1.src;" onmouseout="0.src=0staticImage1.src;"> <img name="0" src="http://mysite.com/thumbs/0/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=0" onmouseover="0.src=0loadImage1.src;" onmouseout="0.src=0staticImage1.src;"> <img name="0" src="http://mysite.com/thumbs/0/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=0" onmouseover="0.src=0loadImage1.src;" onmouseout="0.src=0staticImage1.src;"> <img name="0" src="http://mysite.com/thumbs/0/_002.jpg" border=0></a> <a href="http://mysite.com/index.php?cat=family&video=0" onmouseover="0.src=0loadImage1.src;" onmouseout="0.src=0staticImage1.src;"> <img name="0" src="http://mysite.com/thumbs/0/_002.jpg" border=0></a> </div> Quote Link to comment https://forums.phpfreaks.com/topic/217693-seperate-array-by-5s-into-individual-div-containers/#findComment-1130044 Share on other sites More sharing options...
Andy-H Posted November 3, 2010 Share Posted November 3, 2010 The array values are integers as you displayed in your example, aren't they? //EDIT If not, please show the contents of the array using: echo '<pre>' . print_r($myfile, true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/217693-seperate-array-by-5s-into-individual-div-containers/#findComment-1130047 Share on other sites More sharing options...
lspiehler Posted November 3, 2010 Author Share Posted November 3, 2010 Sorry, no. My array is file names. Some of the file names are long, and after using them several times the code got very long and I thought it would make a better example if I just used some short numbers. I didn't realize it would be a problem. Do you have any more tricks up your sleeve? Thanks again for your amazingly fast responses! Quote Link to comment https://forums.phpfreaks.com/topic/217693-seperate-array-by-5s-into-individual-div-containers/#findComment-1130048 Share on other sites More sharing options...
Andy-H Posted November 3, 2010 Share Posted November 3, 2010 $myfile = array_chunk($myfile, 5); foreach ($myfile as $v) { echo "\t" . '<div>' . "\r\n"; foreach($v as $val) { printf("\t\t" . '<a href="http://mysite.com/index.php?cat=family&video=%1$s" onmouseover="%1$s.src=%1$sloadImage1.src;" onmouseout="%1$s.src=%1$sstaticImage1.src;"> <img name="%1$s" src="http://mysite.com/thumbs/%1$s/_002.jpg" border=0></a>' . "\r\n", $val); } echo "\t" . '</div>' . "\r\n"; } That should work. And you're welcome. Quote Link to comment https://forums.phpfreaks.com/topic/217693-seperate-array-by-5s-into-individual-div-containers/#findComment-1130052 Share on other sites More sharing options...
lspiehler Posted November 3, 2010 Author Share Posted November 3, 2010 Just what I needed. You and this site are awesome! Now I've got to go study all this stuff to see how you made it work. Hopefully after enough playing around, I can help contribute to this highly skilled community. Thank you so much. Quote Link to comment https://forums.phpfreaks.com/topic/217693-seperate-array-by-5s-into-individual-div-containers/#findComment-1130058 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.