Jump to content

Shuffling an Array and Outputting shuffled content across 3 Col. Table


Recommended Posts

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!

 

 

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++;
    
}

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>

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.

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.

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

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

?>

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.