Jump to content

[SOLVED] Randomizing Display of Variables


xProteuSx

Recommended Posts

Say I have 4 variables:

 

$_1;

$_2;

$_3;

$_4;

 

I want to display them as a list, so, for example:

 

echo '<ol>';

echo '<li>' . $_1; . '</li>';

echo '<li>' . $_2; . '</li>';

echo '<li>' . $_3; . '</li>';

echo '<li>' . $_4; . '</li>';

echo '</ol>';

 

Now, what would I have to do to display the list in a random order??  For example:

 

echo '<ol>';

echo '<li>' . $_3; . '</li>';

echo '<li>' . $_1; . '</li>';

echo '<li>' . $_4; . '</li>';

echo '<li>' . $_2; . '</li>';

echo '</ol>';

 

And next time maybe:

 

echo '<ol>';

echo '<li>' . $_4; . '</li>';

echo '<li>' . $_3; . '</li>';

echo '<li>' . $_1; . '</li>';

echo '<li>' . $_2; . '</li>';

echo '</ol>';

 

My brain can't figure this one out.  I can do this if I re-enter the values into a DB, then extrapolate them, but I know that I am complicating this way too much.  There must be a boolean style of logic that can get this done, I just don't know what it is.

Link to comment
Share on other sites

Put them into an array, shuffle the array to randomize it, then generate the HTML:

<?php
$vars = array($_1,$_2,$_3,$_4);
shuffle($vars);
$tmp = array();
$tmp[] = '<ol>';
foreach ($vars as $var)
   $tmp[] = '<li>' . $var . '</li>';
$tmp[] = '</ol>';
echo implode("\n",$tmp)."\n";
?>

 

Ken

Link to comment
Share on other sites

Ken, thanks for the reply.  I am quite unfamiliar with arrays.  I understand the basic principles, but I don't work with them often, hence I do not know many of the commands and operations specific to them.  I do not understand any of the following code:

 

Why do you have to set this variable?  what does it do?

$tmp = array();

 

What does this foreach(? as ?) do?  I have never seen this.

foreach ($vars as $var)

 

Why implode the $tmp variable?  Maybe this has to do with the first question in the post ...

echo implode("\n",$tmp)."\n";

 

Anyway, I'm looking this up to see if I can understand and learn something here.  If you an post a reply that would be great.

Link to comment
Share on other sites

I use the $tmp array to hold the generated HTML and echo it all at once using the implode() function which puts newline characters between each line.

 

The foreach statement goes through an array one element at a time.

 

It would be best for you to read the on line PHP manual on these and other functions. You can start at the page about arrays in general and explore the whole manual.

 

Arrays are very powerful and can be used to save yourself a lot of time and effort.  Take the time to learn all about using them.

 

Ken

Link to comment
Share on other sites

Thanks again.  I do understand the importance of arrays.  Through my little website project I am hoping to get a decent understanding of most of the common functions.  I have not come up against any arrays as of yet, but I have just gotten to that part.  I think I will be working on arrays for the next few days, so I will better understand all the functions soon.  I guess the part that really stumped me is the fact that you transformed the array into a single string, then output it as such.  I don't understand the benefits of this, but it sure is one way to go about getting this done.

 

As for now, I did implement your code, and have modified it to suit my script.  It is working as needed.  Thanks.

Link to comment
Share on other sites

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.