Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 17th Aug 2012, 03:47 PM   #1
HyperActive Warrior
 
footballbob06's Avatar
 
Join Date: 2009
Location: Arizona
Posts: 184
Thanks: 12
Thanked 25 Times in 25 Posts
Default
Get a random image to show the same on two image sections
Share on: 
fb share twitter share gplus share more share

Ok So I have a slider on my site and then under that there are 3 boxes. If a post has a featured image it will then show that picture on the slider and in the box if its the first post. Well when there is not post it shows a random picture from 8 different pictures. The problem I have is that the random picture that correlates to the first post is not the same in the slider and the box. Below is the code. How would I fix this?

Code:
if (have_posts()) : while (have_posts()) : the_post();

		if($img_position == 'boxgrid'){
			$thumb = get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
			$pattern= "/(?<=src=['|\"])[^'|\"]*?(?=['|\"])/i";
			preg_match($pattern, $thumb, $thePath); 
         $random_number = mt_rand(1, 8);
			if(!isset($thePath[0])){
			$thePath[0] = get_template_directory_uri().'/images/slideshow/noftrdimg-'.$random_number.'.jpg';
			}
			$tmp .= '<div class="boxgrid captionfull" style="background: transparent url('.$thePath[0].') repeat scroll 0 0; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; " title="'. get_the_title().'">';
			$tmp .= '<div class="cover boxcaption">';
			$tmp .= '<h3 style="padding-left:8px;"><a href="'. get_permalink().'" title="'. get_the_title().'">'. get_the_title().'</a></h3>';
			$tmp .= '<p>'.substr(get_the_excerpt(), 0, 100).'</p>';
			$tmp .= '</div>';		
			$tmp .= '</div>';
So that is the little boxes then here is for the slider. They are both in the same php file


Code:
while (have_posts()) : the_post();
		
			$url = get_permalink();
			$theme_fields = get_post_custom_values('my_url');
			if(isset($theme_fields[0])){
			 	$url = $theme_fields[0];
			}
			   
			$tmp .='<div id="fragment-'.$id.'-'.$i.'" class="ui-tabs-panel">'. chr(13);
		      $random_number = mt_rand(1, 8);
		    if($width != '' || $height != ''){
		    	if (get_the_post_thumbnail( $post->ID, array($width,$height),""  ) == '') { $ftrdimg = '<img src="'.get_template_directory_uri().'/images/slideshow/noftrdimg-'.$random_number.'.jpg" />'; } else { $ftrdimg = get_the_post_thumbnail( $post->ID, array($width,$height),"class={$reflect}" ); }
		    } else {
		    	if (get_the_post_thumbnail( $post->ID, array(756,250),""  ) == '') { $ftrdimg = '<img src="'.get_template_directory_uri().'/images/slideshow/noftrdimg.jpg"/>'; } else { $ftrdimg = get_the_post_thumbnail( $post->ID, array(756,250),"class={$reflect}"  ); }
		    }
		    
			$tmp .='	<a class="reflect" href="'.$url.'">'.$ftrdimg.'</a>'. chr(13);
Both you can see are using random number but they don't sync. So one will have picture 3 and the other picture 5, but I want them to both have say picture 6 for the each instance.
Thanks!
footballbob06 is offline  
Unread 18th Aug 2012, 04:56 AM   #2
VIP Warrior
War Room Member
 
Join Date: 2006
Location: , , USA.
Posts: 1,795
Thanks: 70
Thanked 287 Times in 259 Posts
Default
Re: Get a random image to show the same on two image sections
Share on: 
fb share twitter share gplus share more share

Are you new to programming?
Save the random numbers generated in an array and reuse it in the second part.
KirkMcD is offline  
Unread 18th Aug 2012, 07:27 AM   #3
HyperActive Warrior
 
footballbob06's Avatar
 
Join Date: 2009
Location: Arizona
Posts: 184
Thanks: 12
Thanked 25 Times in 25 Posts
Default
Re: Get a random image to show the same on two image sections
Share on: 
fb share twitter share gplus share more share

Yes quite new. Ok I will look into how to do that. Thanks!
footballbob06 is offline  
Unread 18th Aug 2012, 12:37 PM   #4
HyperActive Warrior
 
footballbob06's Avatar
 
Join Date: 2009
Location: Arizona
Posts: 184
Thanks: 12
Thanked 25 Times in 25 Posts
Default
Re: Get a random image to show the same on two image sections
Share on: 
fb share twitter share gplus share more share

I haven't found anything about how to do this online. Do you have anywhere you could point me?
footballbob06 is offline  
Unread 18th Aug 2012, 03:37 PM   #5
Software Product Creator
War Room Member
 
SteveSRS's Avatar
 
Join Date: 2012
Location: NL => Thailand
Posts: 452
Thanks: 42
Thanked 164 Times in 93 Posts
Blog Entries: 5
Default
Re: Get a random image to show the same on two image sections
Share on: 
fb share twitter share gplus share more share

$reuse = array();

....
$reuse[] = $randnr;

or you can use associative arrays.. see php.net for more explination
SteveSRS is offline  
The Following User Says Thank You to SteveSRS For This Useful Post:
Unread 18th Aug 2012, 08:55 PM   #6
HyperActive Warrior
 
footballbob06's Avatar
 
Join Date: 2009
Location: Arizona
Posts: 184
Thanks: 12
Thanked 25 Times in 25 Posts
Default
Re: Get a random image to show the same on two image sections
Share on: 
fb share twitter share gplus share more share

I think I understand how this works. Basically the random number is put into an array. Then the array is named reuse. Then further down the random number will actually be reuse which is the array which is the original number. Am I correct? I have been putting this code all over in my php file with no luck. Any other help you can give me would be fantastic.
footballbob06 is offline  
Closed Thread


Bookmarks

Tags
image, random, sections, show


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 05:32 PM.