Skip to content

Instantly share code, notes, and snippets.

@codearachnid
Created October 17, 2012 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codearachnid/3907498 to your computer and use it in GitHub Desktop.
Save codearachnid/3907498 to your computer and use it in GitHub Desktop.
Loop and display unique random array rows
<?php
$special_var = '';
foreach($rows as $row){
$page_links = $row[ 'callout_block_pages' ];
if( is_array( $page_links ) && in_array( $page_url, $page_links ) ) {
$special_var = '<article class="'. $row[ 'callout_background_color' ] .'"><a href="'. $row[ 'callout_link' ] .'" rel="bookmark">'. $row[ 'callout_title' ]. '</a></article>';
}
}
if(!empty($special_var)) {
echo $special_var;
} else {
if(count($rows) > 1 ){
$picked = array();
// loop while we pick unique randoms
while ( count($picked) < 3 {
$random = array_rand($rows);
// check if random row is in $picked
if( !in_array($random, $picked )) {
$picked[] = $random;
echo '<article class="'. $rows[$random]['callout_background_color'] .'">';
// our url
echo '<a href="'. $rows[$random]['callout_link'] .'" rel="bookmark">';
// our title
echo $rows[$random]['callout_title'];
echo '</a>';
echo '</article>';
}
}
} else {
echo '<article class="'. $rows[0]['callout_background_color'] .'">';
// our url
echo '<a href="'. $rows[0]['callout_link'] .'" rel="bookmark">';
// our title
echo $rows[0]['callout_title'];
echo '</a>';
echo '</article>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment