digitex Posted April 20, 2023 Share Posted April 20, 2023 Afternoon folks. I'm having an issue I can't figure out. I have a file field that supports pdf doc and docx and png for uploads and I have the following code in the template: $dls = $page->downloadable; echo "<ul class=\"dl-list\">"; foreach($dls as $dl) { $fs = $dl->filesize; $tfs = (int)($fs / 1024); if($dl->ext() == "pdf") { $icon = "pdf"; } else if($dl->ext() == "doc" or "docx") { $icon = "word"; } else { $icon = "png"; } echo "<li class=\"{$icon}\"><a href=\"{$dl->url}\" target=\"_blank\">{$dl->description}</a><span> | {$tfs}k</span></li>\n"; } echo "</ul>"; I'm using CSS to give each extension type it's own icon and it recognizes pdf and doc/docx but the png is getting the Word icon rather than the png icon. Am I missing something obvious? Any suggestions appreciated. Thanks. Link to comment Share on other sites More sharing options...
Robin S Posted April 21, 2023 Share Posted April 21, 2023 5 hours ago, digitex said: } else if($dl->ext() == "doc" or "docx") { You'll need to write this line as... } else if($dl->ext() == "doc" or $dl->ext() == "docx") { ...or else it will always evaluate as true regardless of the extension. A good candidate for rewriting as a switch statement too. 2 1 Link to comment Share on other sites More sharing options...
digitex Posted April 21, 2023 Author Share Posted April 21, 2023 Haaaa! I did miss something obvious. Safe bet with me. I was under the impression I could short form that, not sure where I got that idea but it didn't throw an error so..... Thanks Robin. If more file types are added to the list then a switch is absolutely what I'll use. 1 Link to comment Share on other sites More sharing options...
gornycreative Posted April 29, 2023 Share Posted April 29, 2023 A humorous stackexchange question page that has collected many answers over the years on stacking ternary operators is here: https://stackoverflow.com/questions/5235632/stacking-multiple-ternary-operators-in-php I agree this may be a good candidate for switch, but it's funny to see how many ways this cat got skinned over the years. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now