randomize divs (load all randomly)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Donkies11
Forum Newbie
Posts: 3
Joined: Mon Aug 09, 2010 6:41 pm

randomize divs (load all randomly)

Post by Donkies11 »

I've got a project that was just "dumped" on my lap...

85 video snippets are loading to a page for viewing. The person loaded them into a table and I am expected to mix them up every day for a few weeks.

I am thinking that if I build out the page as divs for each video element (losing the table), there is probably a code that will load them all randomly.

Maybe if I have each div element as a separate file in a specific directory and numbered such as div1.php, div2.php? Then something could search that directory, create an array and then randomly load each div upon page load...

Anyone able to direct me to a resource that will help?

(I do not consider myself a programmer - just knowledgeable enough to get into trouble)
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: randomize divs (load all randomly)

Post by JakeJ »

So all 85 are on the same page?

No need to switch to divs. Just use the php shuffle() function to randomize the array.

In your table, echo out $vid1, $vid2, $vid3 in order. But shuffle your array and then loop through it to assign the images to the $vidX variables.

Should be pretty easy. In fact, this method will mean users see a different order of snippets with every page load.
Donkies11
Forum Newbie
Posts: 3
Joined: Mon Aug 09, 2010 6:41 pm

Re: randomize divs (load all randomly)

Post by Donkies11 »

yes, all 85 are on the same page...

right now there are 10 rows like so:

<tr>
<td width="100"><img src="video_filler.jpg" width="100" height="75" /></td>
<td width="100"><img src="video_filler.jpg" alt="" width="100" height="75" /></td>
<td width="69"><img src="video_filler.jpg" alt="" width="100" height="75" /></td>
<td width="104"><img src="video_filler.jpg" alt="" width="100" height="75" /></td>
<td width="16"><img src="video_filler.jpg" alt="" width="100" height="75" /></td>
<td width="16"><img src="video_filler.jpg" alt="" width="100" height="75" /></td>
<td width="16"><img src="video_filler.jpg" alt="" width="100" height="75" /></td>
<td width="16"><img src="video_filler.jpg" alt="" width="100" height="75" /></td>
<td width="16"><img src="video_filler.jpg" alt="" width="100" height="75" /></td>
<td width="27"><img src="video_filler.jpg" alt="" width="100" height="75" /></td>
</tr>
<tr>
<td><div align="center"><span class="style3">Contestant Name</span></div></td>
<td><div align="center"><span class="style3">Contestant Name</span></div></td>
<td><div align="center"><span class="style3">Contestant Name</span></div></td>
<td><div align="center"><span class="style3">Contestant Name</span></div></td>
<td><div align="center"><span class="style3">Contestant Name</span></div></td>
<td><div align="center"><span class="style3">Contestant Name</span></div></td>
<td><div align="center"><span class="style3">Contestant Name</span></div></td>
<td><div align="center"><span class="style3">Contestant Name</span></div></td>
<td><div align="center"><span class="style3">Contestant Name</span></div></td>
<td><div align="center"><span class="style3">Contestant Name</span></div></td>
</tr>


It makes more sense to me that each video embed code and contestant name be in a single div... Or, I could create an xml file with each one, yes?

Can you direct me to something that will help me learn to randomize all the videos and write them to the page?

TIA,
Greg
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: randomize divs (load all randomly)

Post by JakeJ »

Code: Select all

//this
<td width="100"><img src="video_filler.jpg" alt="" width="100" height="75" /></td>

//becomes this
$vid1 = "name_of_one_file.jpg";
echo "<td width='100'><img src='$vid1' alt='' width='100' height='75' /></td>"

Do you know how to put all of your file names in to an array? If you're loading them from mysql, then they already are. In fact, you can order them randomly coming out of mysql by using "ORDER BY rand()" at the end of your sql statement.

once you have your file names, loop through the array and assign them to the variable on the fly.
Donkies11
Forum Newbie
Posts: 3
Joined: Mon Aug 09, 2010 6:41 pm

Re: randomize divs (load all randomly)

Post by Donkies11 »

i don't have a db to work with - i am going to create an xml file to call in for the array...

you might have given me enough to get started with...

thank you,
Greg
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: randomize divs (load all randomly)

Post by JakeJ »

an xml file should work just fine. You can even load it up with an ajax request and make it faster. But use jQuery and the .load() method. It's easier than trying to figure out the ajax stuff yourself. jQuery handles all of that for you.
Post Reply