l3rodey Posted July 17, 2013 Share Posted July 17, 2013 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 Quote Link to comment Share on other sites More sharing options...
fastsol Posted July 17, 2013 Share Posted July 17, 2013 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted July 17, 2013 Share Posted July 17, 2013 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; } 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.