Jump to content

[SOLVED] using arrays


almightyegg

Recommended Posts

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???

Link to comment
https://forums.phpfreaks.com/topic/63189-solved-using-arrays/
Share on other sites

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...

Link to comment
https://forums.phpfreaks.com/topic/63189-solved-using-arrays/#findComment-314937
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/63189-solved-using-arrays/#findComment-314973
Share on other sites

<? 
$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'>";
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/63189-solved-using-arrays/#findComment-315001
Share on other sites

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]'>";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/63189-solved-using-arrays/#findComment-315027
Share on other sites

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 :)

Link to comment
https://forums.phpfreaks.com/topic/63189-solved-using-arrays/#findComment-315030
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.