Jump to content

Recommended Posts

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.


$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";
   }

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.

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!!!!

$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";
}

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>

 

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!

$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. ;)

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.

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.