0

How would I go about making these items appear in a random order every time??

            <?php
            for($i = 1; $i <= 5; $i++) {
            $nameN = "name{$i}";
            $$nameN = get_post_meta(get_the_ID(), "ch_client_name{$i}", TRUE);
            // or $name[$i], if you can
            ?>

           <li data-id="<?php the_ID(); ?>" class="<?php echo $clientterms; ?> portfolio-item" data-type="<?php echo $clientterms; ?>">
               <?php echo $$nameN; ?> 
           </li>

           <?php
           } ?>
1
  • Variable variables? Why? There is nearly no place for those in PHP, seriously. Why not just use normal arrays - it's less hacky overall... Commented Feb 25, 2012 at 17:36

2 Answers 2

1
<?php
$numbers = array(1,2,3,4,5);
for ($i = 1; $i <= 5; $i++) {
  $r = rand(0, count($numbers) - 1);
  $nth = $numbers[$r];
  unset ($numbers[$r]); 
  array_unshift ($numbers, array_shift ($numbers)); 
  //Other part of the code

should work. It basically picks a random not-shown item, and removes it from array. $nth will store which is the real number of item.

Sign up to request clarification or add additional context in comments.

Comments

0

Why do you add each client to different meta value ? You can add all clients to one meta value - ch_client_name - it will be array. This will simplify your structure.

If you change your structure, I suggest firstly get meta values as array, then use shuffle and iterate through array.

$clientNames = get_post_meta(get_the_ID(), "ch_client_name");
shuffle($clientNames);

foreach ($clientNames as $clientName){....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.