Jump to content

Convert huge if to functions


l3rodey

Recommended Posts

Hi Everyone,

 

I need help converting this if / ifelse to a function, (I am new), 

 

if($file_extension == 'unknown'){$doc_extension = 'unknown'; $file_type = 'Unknown';}
elseif(($fe=='docx')||($fe=='doc')||($fe=='docm')||($fe=='dotx')||($fe=='dotm')||($fe=='pages')||($fe=='wps')){$doc_extension='docx';$file_type='Word Document';}
elseif(($fe=='pdf')||($fe=='pdp')){$doc_extension='pdf';$file_type='PDF';}
elseif(($fe=='xlsx')||($fe=='xlsm')||($fe=='xlsb')||($fe=='xltx')||($fe=='xltm')||($fe=='xls')||($fe=='xlt')||($fe=='xls')||($fe=='csv')){$doc_extension='xlsx';$file_type='Excel Document';}
elseif(($fe=='zip')||($fe=='zipx')||($fe=='tar')||($fe=='gz')||($fe=='z')||($fe=='cab')||($fe=='rar')||($fe=='bz2')||($fe=='lzh')||($fe=='7z')||($fe=='img')||($fe=='iso')){$doc_extension='zip';$file_type='Zip Folder';}
elseif(($fe=='jpg')||($fe=='jpeg')||($fe=='jpe')){$doc_extension='jpg';$file_type='JPEG Image';}
elseif(($fe=='png')||($fe=='pns')){$doc_extension='jpg';$file_type='PNG Image';}
elseif(($fe=='gif')){$doc_extension='jpg';$file_type='GIF Image';}
elseif(($fe=='tiff')||($fe=='tif')){$doc_extension='jpg';$file_type='TIFF/TIF Image';}
elseif(($fe=='psb')||($fe=='bmp')||($fe=='rle')||($fe=='dib')||($fe=='eps')||($fe=='iff')||($fe=='tdi')||($fe=='jpf')||($fe=='jpx')||($fe=='jp2')||($fe=='j2c')||($fe=='j2k')||($fe=='jpc')||($fe=='jps')||($fe=='mpo')||($fe=='pcx')||($fe=='raw')||($fe=='pxr')||($fe=='pbm')||($fe=='ppm')||($fe=='pnm')||($fe=='pfm')||($fe=='pam')){$doc_extension='jpg';$file_type='Image File';}
elseif(($fe=='mp3')){$doc_extension='mp3';$file_type='MP3';}
else { $doc_extension = 'unknown'; $file_type = 'Unknown'; }
 
This goes on for 280lines... But If I get these done I will be able to do the rest, as you can see I am checking what the document file extension is from a database, $file_extension and depending the results, it creates a variable $doc_extension which works like this : class="$doc_extension" and this is a style from .style.css and it also changes  $file_type which is printed on the screen, This works fine as a large if/ifelse but I only had the index page of my application done now I am doing directories and every time I make a change to it all of it and it's way long process.
 
so what I want to do is just put <?php functionname();?> or <?php require 'fdsfas.php'; ?> so I can just change it once. 
 
If someone could please help that would be mad.
 
Thanks,
Brodey
Link to comment
https://forums.phpfreaks.com/topic/280219-convert-huge-if-to-functions/
Share on other sites

Well it's fairly simple to put this in a function, but you will need to feed the function a parameter from the db of the file extension.

function functioname($fe)
{
if($fe == 'unknown'){$doc_extension = 'unknown'; $file_type = 'Unknown';}
elseif(($fe=='docx')||($fe=='doc')||($fe=='docm')||($fe=='dotx')||($fe=='dotm')||($fe=='pages')||($fe=='wps')){$doc_extension='docx';$file_type='Word Document';}
elseif(($fe=='pdf')||($fe=='pdp')){$doc_extension='pdf';$file_type='PDF';}
elseif(($fe=='xlsx')||($fe=='xlsm')||($fe=='xlsb')||($fe=='xltx')||($fe=='xltm')||($fe=='xls')||($fe=='xlt')||($fe=='xls')||($fe=='csv')){$doc_extension='xlsx';$file_type='Excel Document';}
elseif(($fe=='zip')||($fe=='zipx')||($fe=='tar')||($fe=='gz')||($fe=='z')||($fe=='cab')||($fe=='rar')||($fe=='bz2')||($fe=='lzh')||($fe=='7z')||($fe=='img')||($fe=='iso')){$doc_extension='zip';$file_type='Zip Folder';}
elseif(($fe=='jpg')||($fe=='jpeg')||($fe=='jpe')){$doc_extension='jpg';$file_type='JPEG Image';}
elseif(($fe=='png')||($fe=='pns')){$doc_extension='jpg';$file_type='PNG Image';}
elseif(($fe=='gif')){$doc_extension='jpg';$file_type='GIF Image';}
elseif(($fe=='tiff')||($fe=='tif')){$doc_extension='jpg';$file_type='TIFF/TIF Image';}
elseif(($fe=='psb')||($fe=='bmp')||($fe=='rle')||($fe=='dib')||($fe=='eps')||($fe=='iff')||($fe=='tdi')||($fe=='jpf')||($fe=='jpx')||($fe=='jp2')||($fe=='j2c')||($fe=='j2k')||($fe=='jpc')||($fe=='jps')||($fe=='mpo')||($fe=='pcx')||($fe=='raw')||($fe=='pxr')||($fe=='pbm')||($fe=='ppm')||($fe=='pnm')||($fe=='pfm')||($fe=='pam')){$doc_extension='jpg';$file_type='Image File';}
elseif(($fe=='mp3')){$doc_extension='mp3';$file_type='MP3';}
else { $doc_extension = 'unknown'; $file_type = 'Unknown'; }

$types = array('fe' => $fe, 'type' => $file_type);
return $types;
}

Then you would call and use it like this

$type = functionname($db['row']); // $db['row'] would be whatever you are using for info from the db.
print_r($type); // this is just to show you how it comes back from the function.  You would simply do checks against the specific array item.

I'd agree you should put it somewhere special, but the primary problem is how crazy the code is.

 

1. How about putting all that in a database? XML file? Both very maintainable, very readable, and very simple to look up against.

2. If it must stay in code a switch is much cleaner:

switch ($file_extension) {
	case "doc": case "docm": case "docx":
	case "dotm": case "dotx":
	case "pages":
	case "wps":
		$doc_extension = "docx";
		$file_type = "Word Document";
		break;

	case "pdf": case "pdp":
		$doc_extension = "pdf";
		$file_type = "PDF";
		break;

	// ...

	default:
		$doc_extension = "unknown";
		$file_type = "Unknown";
		break;
}

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.