siamiam Posted February 11, 2010 Share Posted February 11, 2010 So I'm unsuccessfully trying to output something like this: <table> <tr> <td><a href="http://www.somewebsite0.com"><img src="images/imagename0.jpg" ></a></td> <td><a href="http://www.somewebsite1.com"><img src="images/imagename1.jpg" ></a></td> <td><a href="http://www.somewebsite2.com"><img src="images/imagename2.jpg" ></a></td> </tr> <tr> <td><a href="http://www.somewebsite3.com"><img src="images/imagename3.jpg" ></a></td> <td><a href="http://www.somewebsite4.com"><img src="images/imagename4.jpg" ></a></td> <td><a href="http://www.somewebsite5.com"><img src="images/imagename5.jpg" ></a></td> </tr> </table> Here is the code I have so far: $content = array( "http://www.somewebsite0.com|imagename0.jpg", "http://www.somewebsite1.com|imagename1.jpg", "http://www.somewebsite2.com|imagename2.jpg", "http://www.somewebsite3.com|imagename3.jpg", "http://www.somewebsite4.com|imagename4.jpg", "http://www.somewebsite5.com|imagename5.jpg" ); shuffle($content); echo '<table><tr>'; foreach ($content as $value) { $printad = explode('|', $value); echo '<td><a href="' . $printadl[0] . '"><img src="images/' . $printad[1] . '" ></a></td>'; echo '<td><a href="' . $printad[0] . '"><img src="images/' . $printad[1] . '" ></a></td>'; echo '<td><a href="' . $printad[0] . '"><img src="images/' . $printad[1] . '" ></a></td>'; } echo '</tr></table>'; Which is giving me something like this: <table> <tr> <td><a href="http://www.somewebsite0.com"><img src="images/imagename0.jpg" ></a></td> <td><a href="http://www.somewebsite0.com"><img src="images/imagename0.jpg" ></a></td> <td><a href="http://www.somewebsite0.com"><img src="images/imagename0.jpg" ></a></td> </tr> <tr> <td><a href="http://www.somewebsite4.com"><img src="images/imagename4.jpg" ></a></td> <td><a href="http://www.somewebsite4.com"><img src="images/imagename4.jpg" ></a></td> <td><a href="http://www.somewebsite4.com"><img src="images/imagename4.jpg" ></a></td> </tr> </table> I'm not sure what my next step is or what exactly I'm doing wrong .... any help would be greatly appreciated. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/191827-shuffling-an-array-and-outputting-shuffled-content-across-3-col-table/ Share on other sites More sharing options...
jl5501 Posted February 11, 2010 Share Posted February 11, 2010 your foreach loop should look more like this $i = 1; foreach ($content as $value) { $printad = explode('|', $value); echo '<td><a href="' . $printadl[0] . '"><img src="images/' . $printad[1] . '" ></a></td>'; if(!$i % 3) echo '</tr><tr>'; $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/191827-shuffling-an-array-and-outputting-shuffled-content-across-3-col-table/#findComment-1011063 Share on other sites More sharing options...
teamatomic Posted February 12, 2010 Share Posted February 12, 2010 that wont output the </tr><tr> try this instead if($i % 3 == 0) echo '</tr><tr>'; HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/191827-shuffling-an-array-and-outputting-shuffled-content-across-3-col-table/#findComment-1011149 Share on other sites More sharing options...
jl5501 Posted February 12, 2010 Share Posted February 12, 2010 that is the same as mine except might be better as if(!($i % 3)) Quote Link to comment https://forums.phpfreaks.com/topic/191827-shuffling-an-array-and-outputting-shuffled-content-across-3-col-table/#findComment-1011228 Share on other sites More sharing options...
Zane Posted February 12, 2010 Share Posted February 12, 2010 you'd be better off with a for loop $content = array( "http://www.somewebsite0.com|imagename0.jpg", "http://www.somewebsite1.com|imagename1.jpg", "http://www.somewebsite2.com|imagename2.jpg", "http://www.somewebsite3.com|imagename3.jpg", "http://www.somewebsite4.com|imagename4.jpg", "http://www.somewebsite5.com|imagename5.jpg" ); shuffle($content); echo '</pre> <table>'; for($i=0; $i{ $printad = explode('|', $content[0]); if($i % 3) echo ""; // This is the key that will give you three columns echo ''; // You only need the above line once } echo '</table> Quote Link to comment https://forums.phpfreaks.com/topic/191827-shuffling-an-array-and-outputting-shuffled-content-across-3-col-table/#findComment-1011232 Share on other sites More sharing options...
teamatomic Posted February 12, 2010 Share Posted February 12, 2010 yea, either would work. well, as long as yours was modified to give more than the first line of the array. $printad = explode('|', $content[0]); = $printad = explode('|', $content[$i]); HTH Teamatomic edit Oh BTW...a modulus statement like this ($i % 3) is completely broken and does not work. Quote Link to comment https://forums.phpfreaks.com/topic/191827-shuffling-an-array-and-outputting-shuffled-content-across-3-col-table/#findComment-1011235 Share on other sites More sharing options...
Zane Posted February 12, 2010 Share Posted February 12, 2010 Oh BTW...a modulus statement like this ($i % 3) is completely broken and does not work. Good catch.. I meant to have it as if($i % 3 == 0).. .. fortunately though, that has already been pointed out earlier. Quote Link to comment https://forums.phpfreaks.com/topic/191827-shuffling-an-array-and-outputting-shuffled-content-across-3-col-table/#findComment-1011255 Share on other sites More sharing options...
teamatomic Posted February 12, 2010 Share Posted February 12, 2010 That will do it. Your does however have a possible use. What your does is remove all multiple of 3 so a for gets you returned something like 1,2,4,5,7,8,10.... Gotta be a mathematical use for that somewhere. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/191827-shuffling-an-array-and-outputting-shuffled-content-across-3-col-table/#findComment-1011269 Share on other sites More sharing options...
roopurt18 Posted February 12, 2010 Share Posted February 12, 2010 Nobody took into account that the array might not be evenly divisible by three. In which case all of the code provided will dump invalid markup. IMG tags are self-closing, which nobody seemed to mention either. Also, nobody noticed the OP's typo: $printad = explode('|', $value); echo '<td><a href="' . $printadl[0] . '"><img src="images/' . $printad[1] . '" ></a></td>'; // Typos os $printad vs. $printadl Here is my version, I've added extra white space so I could eyeball that things were closed correctly. Feel free to delete it. <?php $content = array( "http://www.somewebsite0.com|imagename0.jpg" , "http://www.somewebsite1.com|imagename1.jpg" , "http://www.somewebsite2.com|imagename2.jpg" , "http://www.somewebsite3.com|imagename3.jpg" , "http://www.somewebsite4.com|imagename4.jpg" , "http://www.somewebsite5.com|imagename5.jpg" , "http://www.somewebsite6.com|imagename6.jpg" , "http://www.somewebsite7.com|imagename7.jpg" #, "http://www.somewebsite8.com|imagename8.jpg" #, "http://www.somewebsite9.com|imagename9.jpg" ); shuffle($content); echo "<table>\n <tbody>\n"; $max = count( $content ); for( $i = 0; $i < $max; $i += 1 ) { list( $site, $img ) = explode( '|', $content[$i] ); if( $i % 3 === 0 ) { if( $i > 0 ) { echo " </tr>\n"; } echo " <tr>\n"; } echo " <td>\n <a href=\"{$site}\">\n <img src=\"images/{$img}\" />\n </a>\n </td>\n"; } while( $i % 3 !== 0 ) { echo " <td> </td>\n"; $i += 1; } if( $i % 3 === 0 ) { echo " </tr>\n"; } echo " <tbody>\n<table>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/191827-shuffling-an-array-and-outputting-shuffled-content-across-3-col-table/#findComment-1011463 Share on other sites More sharing options...
siamiam Posted February 13, 2010 Author Share Posted February 13, 2010 Wow! This really helps me out. Thanks to everyone for taking the time to help, I really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/191827-shuffling-an-array-and-outputting-shuffled-content-across-3-col-table/#findComment-1011633 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.