Solved

Pulling random but unique value from array in php

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

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jQuery force form POST 7 85
PHP alternative to file_get_contents('php://input') 4 141
Showing multiple maps using PHP and Google Maps 12 38
Multi line FPDF footer: 3 22
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

680 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