Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Pulling random but unique value from array in php

Posted on 2011-03-01
4
Medium Priority
?
144 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 111

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.
Suggested Courses

916 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