Solved

Pulling random but unique value from array in php

Posted on 2011-03-01
4
128 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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 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

10 Experts available now in Live!

Get 1:1 Help Now