How to make single link change automatically based on filetype

7 posts by 3 authors in: Forums > CMS Builder
Last Post: January 14, 2009   (RSS)

By eriqcook - January 9, 2009 - edited: January 10, 2009

Hi everyone, Donna mentioned to ask this question in the web forum so I'm posting it here. Need some quick help on what I think is a simple issue to tackle.

Please take a look at the folowing web page:

http://www.mtrip.org/v2/cmstest.php

In the 4th row of each entry (titled "File"), I want to be able to have a single link that changes automatically based on the type of file it is. In other words, if the entry is a basic article, I want the link to read "View Page", or if it's a file download (PDF, DOC, XLS, etc), the link would read "View/Download File" (instead of showing both "[url "http://www.interactivetools.com/forum/forum.cgi?url=http%3A%2F%2Fwww.interactivetools.com%2Fforum%2Fforum.cgi%3Furl%3Dhttp%253A%252F%252Fwww.mtrip.org%252Fv2%252Farticles%252FarticleDetail.php%253FLogo-Creative-Brief-2"]View Web Page[/#0066cc][/url]" AND "[url "http://www.interactivetools.com/forum/forum.cgi?url=http%3A%2F%2Fwww.interactivetools.com%2Fforum%2Fforum.cgi%3Furl%3Dhttp%253A%252F%252Fwww.mtrip.org%252Fv2%252FcmsAdmin%252Fuploads%252FLogoCreativeBrief.pdf"]View/Download[/#0066cc][/url]" at the same time). I don't like having to show both text links if only one works. Does that make sense?

I think what I want to do is something with an "IF" or "ELSE" statement, but I'm not sure how to code it. Can anyone here provide some example code to acheive the results I need? Here's the code I that's in place now:

<a href="<?php echo $record['_link'] ?>">View Web Page</a><br/>
<!-- STEP 2a: Display Uploads for field 'file' (Paste this anywhere inside STEP2 to display uploads) -->
<!-- Upload Fields: num, createdTime, tableName, fieldName, recordNum, preSaveTempId, filePath, filename, extension, thumbFilePath, isImage, hasThumbnail, urlPath, width, height, thumbUrlPath, thumbWidth, thumbHeight, info1, info2, info3, info4, info5 -->
<?php foreach ($record['file'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /><br/>
<?php else: ?>
<a href="<?php echo $upload['urlPath'] ?>">View/Download<br />
</a>
<?php endif ?>
<?php endforeach ?>

I really don't care about the the thumbnail references (since I'm not showing thumbnails) but tried removing them and screwed up everything. And I'm not sure how to code if/else statements to generate the results I want. I need help. Need to launch this site by Sunday (tomorrow).

Greatly appreciated.

Re: [ross] How to make single link change automatically based on filetype

Thank you for the quick reply. Yes that's exactly what I want to do. Not sure how to code exactly

Re: [eriqcook] How to make single link change automatically based on filetype

By Dave - January 13, 2009

Hi Eriq,

Try something like this:

<?php if ($upload['extension'] == 'gif'): ?>
<a href="...">View Image</a>
<?php elseif ($upload['extension'] == 'pdf'): ?>
<a href="...">View PDF</a>
<?php else: ?>
<a href="...">View All other files</a>
<?php endif ?>


Let me know if that works for you or if you run into any problems.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] How to make single link change automatically based on filetype


Hi Eriq,

Try something like this:

<?php if ($upload['extension'] == 'gif'): ?>
<a href="...">View Image</a>
<?php elseif ($upload['extension'] == 'pdf'): ?>
<a href="...">View PDF</a>
<?php else: ?>
<a href="...">View All other files</a>
<?php endif ?>


Let me know if that works for you or if you run into any problems.

Re: [eriqcook] How to make single link change automatically based on filetype

Strange. I typed in a long reply (using the "Quote" button) and it posted your quote with my response missing! [crazy]

Anyway, what I said was that I changed the code to match to the correct file type, but if you look at the following page:

http://www.mtrip.org/v2/cmstest.php

You'll see it only shows "View Page" (for "non file upload" articles). It's supposed to say "View PDF" if the filetype is a PDF, or "View Page" if there's no file uploaded and just a standard article page.

If you click on the "View Page" link however, it loads the article detail page and displays the file at the very bottom (IF the file exist). Not exactly how I wanted it to work though.

It may have something to do with how I have everything setup on the back side. I don't know. You may need to look at how I have things setup internally to see what's wrong here.

Thank you for your help...

Re: [eriqcook] How to make single link change automatically based on filetype

By Dave - January 14, 2009

Hi Eriq,

Can you attach the cmstest.php file to this post or email me CMS and FTP login details to dave@interactivetools.com (email, don't post login details to the forum) so I can take a closer look at the code?

Thanks!
Dave Edis - Senior Developer
interactivetools.com