Super tough PHP if/else statement add array help needed

I have the following PHP if/else statement that I need to change the order of the $image variable.  It currently displays <img value low to high when it needs to display the highest value for $c_ext first...can someone adjust this to show me how I would build an array to save the $c_ext found in the if statement and then write them out with the highest value first?


                                                      
<?
									$image = "<br>";
									$img_cnt = 1;
									$image .= "<a href=/feeds/sancap/rets_images/$MLNumber.jpg rel=\"enlargeimage\" rev=\"targetdiv:loadarea\"><img src=/feeds/sancap/rets_images/$MLNumber.jpg alt='' width='100' height='75' border='0' /></a>&nbsp;";
									for ($c=1;$c<30;$c++) {
										if ($c<10)
											$c_ext = "0".$c;
										else
											$c_ext = $c;

										if (file_get_contents("http://www.mysite.com/feeds/sancap/rets_images/{$MLNumber}_{$c_ext}.jpg"))
									$image .= "<a href=/feeds/sancap/rets_images/{$MLNumber}_{$c_ext}.jpg rel=\"enlargeimage\" rev=\"targetdiv:loadarea\"><img src=/feeds/sancap/rets_images/{$MLNumber}_{$c_ext}.jpg alt='' width='100' height='75' border='0' /></a>&nbsp;";
										else
											break;

										$img_cnt++;
										if ($img_cnt == 5) {
											$image .= "<br>";
											$img_cnt = 0;
										}

									}

									?>  

Open in new window

pda4meAsked:
Who is Participating?
 
Greg AlexanderConnect With a Mentor Lead DeveloperCommented:
Sorry.... use this one
<?
$url = "http://www.mysite.com/feeds/sancap/rets_images/";
for($c=1;$c<30;$c++){
        
        if($c<10){
                $c_ext = "0".$c;
        }else{
                $c_ext = $c;
        }
        
        if (file_get_contents($url."{$MLNumber}_{$c_ext}.jpg")){
                $image_array[$c_ext] = "{$MLNumber}_{$c_ext}.jpg";      
        }
        
}

ksort($image_array);

$image_array[] = "{$MLNumber}.jpg"; 

$img_cnt = 1;
$image = "<br>";
foreach($image_array as $key => $value){
        
        $image .= "<a href=/feeds/sancap/rets_images/".$value." rel=\"enlargeimage\" rev=\"targetdiv:loadarea\">";
        $image .= "<img src=/feeds/sancap/rets_images/".$value." alt='' width='100' height='75' border='0' />";
        $image .= "</a>&nbsp;";
        
$img_cnt++;
}

?>

Open in new window

0
 
Greg AlexanderLead DeveloperCommented:
How about something like this you can change the ksort to krsort depending on which way to sort the array
<?
$url = "http://www.mysite.com/feeds/sancap/rets_images/";
for($c=1;$c<30;$c++){
	
	if($c<10){
		$c_ext = "0".$c;
	}else{
		$c_ext = $c;
	}
	
	if (file_get_contents($url."{$MLNumber}_{$c_ext}.jpg")){
		$image_array[$c_ext] = "{$MLNumber}_{$c_ext}.jpg";	
	}
	
}

ksort($image_array);

$img_cnt = 1;
$image = "<br>";
$image .= "<a href=/feeds/sancap/rets_images/$MLNumber.jpg rel=\"enlargeimage\" rev=\"targetdiv:loadarea\"><img src=/feeds/sancap/rets_images/$MLNumber.jpg alt='' width='100' height='75' border='0' /></a>&nbsp;";
foreach($image_array as $key => $value){
	
	$image .= "<a href=/feeds/sancap/rets_images/".$value." rel=\"enlargeimage\" rev=\"targetdiv:loadarea\">";
	$image .= "<img src=/feeds/sancap/rets_images/".$value." alt='' width='100' height='75' border='0' />";
	$image .= "</a>&nbsp;";
	
$img_cnt++;
}

?>

Open in new window

0
 
pda4meAuthor Commented:
so close...there is one image that is first that needs to be last...the first image $MLNumber.jpg with no _$c_ext.jpg ends up as the first thumbnail...anyway to kick that one back to the end of the line?

Thanks!
0
 
Greg AlexanderLead DeveloperCommented:
Here, try this:
<?
$url = "http://www.mysite.com/feeds/sancap/rets_images/";
for($c=1;$c<30;$c++){
        
        if($c<10){
                $c_ext = "0".$c;
        }else{
                $c_ext = $c;
        }
        
        if (file_get_contents($url."{$MLNumber}_{$c_ext}.jpg")){
                $image_array[$c_ext] = "{$MLNumber}_{$c_ext}.jpg";      
        }
        
}

ksort($image_array);

$image_array[$c_ext] = "{$MLNumber}.jpg"; 

$img_cnt = 1;
$image = "<br>";
foreach($image_array as $key => $value){
        
        $image .= "<a href=/feeds/sancap/rets_images/".$value." rel=\"enlargeimage\" rev=\"targetdiv:loadarea\">";
        $image .= "<img src=/feeds/sancap/rets_images/".$value." alt='' width='100' height='75' border='0' />";
        $image .= "</a>&nbsp;";
        
$img_cnt++;
}

?>

Open in new window

0
 
pda4meAuthor Commented:
Awesome, thanks so much!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.