Created
October 17, 2012 19:13
-
-
Save codearachnid/3907498 to your computer and use it in GitHub Desktop.
Loop and display unique random array rows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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