almightyegg Posted August 3, 2007 Share Posted August 3, 2007 I've no used arrays before and I was wondering if I could store image types in an array and then see if a user has a special profile image uploaded? eg. $arr = array(imagetype1 => jpg, imagetype2 => jpeg, imagetype3 => gif); /etc... $file = '/home/lordofth/public_html/images/players/m'.$player[id].'.$arr'; if(file_exists($file)){ echo "<img src='http://www.lordoftheabyss.com/images/players/m$player[id].jpg'>"; }else{ echo "<img src='http://www.lordoftheabyss.com/images/players/$player[image]'>"; } I'm pretty sure this example won't work as I don't know how to call up to check all the array values at once...how can I do this successfully??? Quote Link to comment Share on other sites More sharing options...
mpharo Posted August 3, 2007 Share Posted August 3, 2007 that code there wont work because $arr does not have an index... you need to specify an array index for the extension you want... you would then need to search if the file exists and then use that array index.. if (file_exists('/home/lordofth/public_html/images/players/m'.$player[id].'.$arr[imagetype1]')){ $file = '/home/lordofth/public_html/images/players/m'.$player[id].'.$arr[imagetype1]'; } Else If (file_exists('/home/lordofth/public_html/images/players/m'.$player[id].'.$arr[imagetype2]')){ $file = '/home/lordofth/public_html/images/players/m'.$player[id].'.$arr[imagetype2]'; } Else If (file_exists('/home/lordofth/public_html/images/players/m'.$player[id].'.$arr[imagetype3]')){ $file = '/home/lordofth/public_html/images/players/m'.$player[id].'.$arr[imagetype3]'; } this is untested but you can get the jist of it... Quote Link to comment Share on other sites More sharing options...
almightyegg Posted August 3, 2007 Author Share Posted August 3, 2007 would it be easier with a while loop?? Quote Link to comment Share on other sites More sharing options...
mpharo Posted August 3, 2007 Share Posted August 3, 2007 it might be easier to do a loop if you are using numeric index's....you would set it up to loop until a file exists then use that filename. Quote Link to comment Share on other sites More sharing options...
almightyegg Posted August 3, 2007 Author Share Posted August 3, 2007 one last question, can you have multipul values for one array value eg $arr[imagetype1] = jpg, jpeg, JPG, JPEG ??? Quote Link to comment Share on other sites More sharing options...
mpharo Posted August 3, 2007 Share Posted August 3, 2007 yes but you would then need to explode each index in the array and then implode them into a seperate variable...which is much more work... Quote Link to comment Share on other sites More sharing options...
almightyegg Posted August 3, 2007 Author Share Posted August 3, 2007 Hmm, my code isn't working....shows no errors, but it isn't finding my file. which is saved to the right directory, and as far as I can see is no different to what is specified.... it is called m1.PNG (1 is my ID) <? $type = array( type1 => jpg, type2 => JPG, type3 => jpeg, type4 => JPEG, type5 => gif, type6 => GIF, type7 => png, type8 => PNG ); if(file_exists('/home/lordofth/public_html/images/players/m'.$player[id].'.$type[type1]') == TRUE){ $file = '/home/lordofth/public_html/images/players/m'.$player[id].'.$type[type1]'; }elseif(file_exists('/home/lordofth/public_html/images/players/m'.$player[id].'.$type[type2]') == TRUE){ $file = '/home/lordofth/public_html/images/players/m'.$player[id].'.$type[type2]'; }elseif(file_exists('/home/lordofth/public_html/images/players/m'.$player[id].'.$type[type3]') == TRUE){ $file = '/home/lordofth/public_html/images/players/m'.$player[id].'.$type[type3]'; }elseif(file_exists('/home/lordofth/public_html/images/players/m'.$player[id].'.$type[type4]') == TRUE){ $file = '/home/lordofth/public_html/images/players/m'.$player[id].'.$type[type4]'; }elseif(file_exists('/home/lordofth/public_html/images/players/m'.$player[id].'.$type[type5]') == TRUE){ $file = '/home/lordofth/public_html/images/players/m'.$player[id].'.$type[type5]'; }elseif(file_exists('/home/lordofth/public_html/images/players/m'.$player[id].'.$type[type6]') == TRUE){ $file = '/home/lordofth/public_html/images/players/m'.$player[id].'.$type[type6]'; }elseif(file_exists('/home/lordofth/public_html/images/players/m'.$player[id].'.$type[type7]') == TRUE){ $file = '/home/lordofth/public_html/images/players/m'.$player[id].'.$type[type7]'; }elseif(file_exists('/home/lordofth/public_html/images/players/m'.$player[id].'.$type[type8]') == TRUE){ $file = '/home/lordofth/public_html/images/players/m'.$player[id].'.$type[type8]'; }else{ $file = 0; } if($file == 0){ echo "<img src='http://www.lordoftheabyss.com/images/players/$player[image]'>"; }else{ echo "<img src='$file'>"; } ?> I added == TRUE because it didn't work without it and I thought it may need it but, obviously it's not that which was stopping it from working Quote Link to comment Share on other sites More sharing options...
mpharo Posted August 3, 2007 Share Posted August 3, 2007 <? $type = array( type1 => jpg, type2 => JPG, type3 => jpeg, type4 => JPEG, type5 => gif, type6 => GIF, type7 => png, type8 => PNG ); if(file_exists("/home/lordofth/public_html/images/players/m.$player[id].$type[type1]")){ $file = "/home/lordofth/public_html/images/players/m.$player[id].$type[type1]"; }elseif(file_exists("/home/lordofth/public_html/images/players/m.$player[id].$type[type2]")){ $file = "/home/lordofth/public_html/images/players/m.$player[id].$type[type2]"; }elseif(file_exists("/home/lordofth/public_html/images/players/m.$player[id].$type[type3]")){ $file = "/home/lordofth/public_html/images/players/m.$player[id].$type[type3]"; }elseif(file_exists("/home/lordofth/public_html/images/players/m.$player[id].$type[type4]")){ $file = "/home/lordofth/public_html/images/players/m.$player[id].$type[type4]"; }elseif(file_exists("/home/lordofth/public_html/images/players/m.$player[id].$type[type5]")){ $file = "/home/lordofth/public_html/images/players/m.$player[id].$type[type5]"; }elseif(file_exists("/home/lordofth/public_html/images/players/m.$player[id].$type[type6]")){ $file = "/home/lordofth/public_html/images/players/m.$player[id].$type[type6]"; }elseif(file_exists("/home/lordofth/public_html/images/players/m.$player[id].$type[type7]")){ $file = "/home/lordofth/public_html/images/players/m.$player[id].$type[type7]"; }elseif(file_exists("/home/lordofth/public_html/images/players/m.$player[id].$type[type8]")){ $file = "/home/lordofth/public_html/images/players/m.$player[id].$type[type8]"; }else{ $file = 0; } if($file == 0){ echo "<img src='http://www.lordoftheabyss.com/images/players/$player[image]'>"; }else{ echo "<img src='$file'>"; } ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted August 3, 2007 Share Posted August 3, 2007 use a loop <?php $type = array( type1 => jpg, type2 => JPG, type3 => jpeg, type4 => JPEG, type5 => gif, type6 => GIF, type7 => png, type8 => PNG ); $exists = false; foreach ($type as $ft) { if(file_exists("/home/lordofth/public_html/images/players/m.$player[id].$ft")) { $file = "/home/lordofth/public_html/images/players/m.$player[id].$ft"; $exists = true; break; } } if($exists){ echo "<img src='$file'>"; }else{ echo "<img src='http://www.lordoftheabyss.com/images/players/$player[image]'>"; } ?> Quote Link to comment Share on other sites More sharing options...
almightyegg Posted August 3, 2007 Author Share Posted August 3, 2007 I finally got it working...it took me all this time but it just needed a few changes... <? $type = array( type1 => jpg, type2 => JPG, type3 => jpeg, type4 => JPEG, type5 => gif, type6 => GIF, type7 => png, type8 => PNG ); $file = '/home/lordofth/public_html/images/players/m'; $ft1 = file_exists(''.$file.''.$player[id].'.'.$type[type1].''); $ft2 = file_exists(''.$file.''.$player[id].'.'.$type[type2].''); $ft3 = file_exists(''.$file.''.$player[id].'.'.$type[type3].''); $ft4 = file_exists(''.$file.''.$player[id].'.'.$type[type4].''); $ft5 = file_exists(''.$file.''.$player[id].'.'.$type[type5].''); $ft6 = file_exists(''.$file.''.$player[id].'.'.$type[type6].''); $ft7 = file_exists(''.$file.''.$player[id].'.'.$type[type7].''); $ft8 = file_exists(''.$file.''.$player[id].'.'.$type[type8].''); if($ft1 == '1'){ $file = "http://www.lordoftheabyss.com/images/players/m$player[id].$type[type1]"; }elseif($ft2 == '1'){ $file = "http://www.lordoftheabyss.com/images/players/m$player[id].$type[type2]"; }elseif($ft3 == '1'){ $file = "http://www.lordoftheabyss.com/images/players/m$player[id].$type[type3]"; }elseif($ft4 == '1'){ $file = "http://www.lordoftheabyss.com/images/players/m$player[id].$type[type4]"; }elseif($ft5 == '1'){ $file = "http://www.lordoftheabyss.com/images/players/m$player[id].$type[type5]"; }elseif($ft6 == '1'){ $file = "http://www.lordoftheabyss.com/images/players/m$player[id].$type[type6]"; }elseif($ft7 == '1'){ $file = "http://www.lordoftheabyss.com/images/players/m$player[id].$type[type7]"; }elseif($ft8 == '1'){ $file = "http://www.lordoftheabyss.com/images/players/m$player[id].$type[type8]"; } if(!$file){ echo "<br><img src='http://www.lordoftheabyss.com/images/players/$player[image]'>"; }else{ echo "<br><img src='$file'>"; } Thanks for the help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.