Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Pulling random but unique value from array in php

Posted on 2011-03-01
4
Medium Priority
?
143 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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

730 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