Solved

Pulling random but unique value from array in php

Posted on 2011-03-01
4
129 Views
Last Modified: 2013-11-30
I am trying to intersperse my posts in a WordPress loop with non-post content. The non-post content has to be pulled in at random. The idea is that posts are represented as little square thumbnails and I want non-post color boxed interspersed with the post thumbnails. I don't think it really matters that this is a WordPress loop. It could be any loop. I set up my loop to count my records so that each record has a class of "box1, box2, box3, etc." I want to create another class that is basically the same as the count class but with the order random and unique (blank3, blank32, blank14, etc.). I know that there will be a total of 33 items in the loop. I  created an array called $blanks and shuffled them but I am having trouble finding a way to pull that into my class field in the loop.

This is the page: http://www.clampdesign.com/dev/itopartners/category/work

I want to add to the $classes variable but everything I've tried would result in duplicate random numbers between 1 and 33.
Here is what I have so far:


<?php
$count = 0;
while ($wp_query->have_posts()) : $wp_query->the_post(); 
$blanks = range(0,33);
shuffle($blanks);
	foreach ($blanks as $blank) {
		$teal[$blank] = 'teal'.$blank;
	}
		$count++;
		$classes = 'class="box'.$count.' '.$teal[$blank];
		if ($count % 11 == 0) $classes .= ' last';
		$classes .= '"';
		if ( has_post_thumbnail()) { ?>
		   <a <?php echo $classes; ?> href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
		   <?php the_post_thumbnail('thumbnail'); ?>
		   </a>
	 <?php } ?>
 <?php endwhile;  
?>

Open in new window

0
Comment
Question by:lyeffeth
4 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 35011331
Hmm...  What seems to be going wrong?  range() will produce no duplicates.  shuffle() will order randomly.
<?php // RAY_temp_lyeffeth.php
error_reporting(E_ALL);
echo "<pre>";

$blanks = range(0,33);
shuffle($blanks);

// SHOW THE RANDOM ARRAY
print_r($blanks);

// CREATE A NEW ARRAY OF STUFF
$teal = array();
foreach ($blanks as $key =>$blank)
{
    $teal[$blank] = 'teal'.$blank;
    unset($blanks[$key]);
}
print_r($teal);
print_r($blanks);

Open in new window

0
 

Accepted Solution

by:
lyeffeth earned 0 total points
ID: 35011446
Nevermind, I figured it out. Final code:
<?php
					$blankbox = range(1,15);
					if ( get_query_var('paged') ) {
								$paged = get_query_var('paged');
						} elseif ( get_query_var('page') ) {
								$paged = get_query_var('page');
						} else {
								$paged = 1;
						}
					global $post;
					$args = array( 'showposts' => 18, 'offset'=> 1, 'orderby' => rand, 'paged'=>$paged, 'cat'=>169 );
					$myposts = get_posts( $args );
					$workposts = array_merge($blankbox, $myposts);
					shuffle($workposts);
					$count = 0;
					foreach( $workposts as $post ) :	setup_postdata($post); 
							$count++;
							$classes = 'box'.$count;
							if ($count % 11 == 0) $classes .= ' last';
							if ( has_post_thumbnail()) { ?>
							   <div class="<?php echo $classes; ?>"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
							   <?php the_post_thumbnail('thumbnail'); ?>
							   </a></div>
						 <?php } else { ?>
						 		<div class="blankbox <?php echo $classes; ?>"></div>
						 <?php } ?>
					 <?php endforeach; ?>
			</div><!--#thumbnails-->

Open in new window

0
 
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 39686721
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now