Please help! Foreach problem

Discussion in 'PHP' started by imthebanana, Jul 13, 2011.

  1. #1
    ok so im new with the whole foreach thing, i use to just do a bunch of different lines (Taught myself to code) so if you could help me with this one

    foreach ($result[$key]['pic_big'] as $key => $value) {
    	if ($result[$key]['oauth_provider'] != '4dare') {
    		if (strlen($value) > 6){
    		$string = substr($value, 0, 6); 
    		}
    		if ($string == "users/") {
    		$pic[$key] = "http://www.trollfight.com/".$result[$key]['pic_big']."";
    		}else{
    		$pic[$key] = $result[$key]['pic_big'];
    		}
    	}else{
    	$pic[$key] = $result[$key]['pic_big'];
    	}
    	}
    
    Code (markup):
    Basically my mysql class returns values as $result3[lineno.][column] eg $result[1]['name'] and i need to test each line to see where to load the avatar from, eg either trollfight or local

    Thanks in advance

    alex
     
    imthebanana, Jul 13, 2011 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    You didn't mention what problems you have.

    but try this code

    
    
    foreach ($result as $key => $value) {
    	if ($result[$key]['oauth_provider'] != '4dare') {
    		if (strlen($value['pic_big']) > 6){
    		  $string = substr($value['pic_big'], 0, 6); 
    		}
    		if ($string == "users/") {
    		  $pic[$key] = "http://www.trollfight.com/".$result[$key]['pic_big']."";
    		}else{
    		  $pic[$key] = $result[$key]['pic_big'];
    		}
    	}else{
    	   $pic[$key] = $result[$key]['pic_big'];
    	}
    }
        
    
    PHP:
     
    stephan2307, Jul 13, 2011 IP
  3. imthebanana

    imthebanana Well-Known Member

    Messages:
    486
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    140
    #3
    foreach($result3 as $i => $v) {	
    	if ($result3[$i]['oauth_provider'] != '4dare') {
    		if (strlen($result3[$i]['pic_big']) > 6){
    		$string = substr($result3[$i]['pic_big'], 0, 6); 
    		}
    		$picno = 'pic'.$i.'';
    		if ($string == "users/") {
    		$pic[$i] = "http://www.trollfight.com/".$result3[$i]['pic_big']."";
    		}else{
    		$pic[$i] = $result3[$i]['pic_big'];
    		}
    	}else{
    	$pic[$i] = $result3[$i]['pic_big'];
    	}
    }
    Code (markup):
    I did it like 2 seconds before your reply mate! Cheers ;)
    Works perfectly!
     
    imthebanana, Jul 13, 2011 IP