Linking to Uploads Across Multiple Documents?

25 posts by 3 authors in: Forums > CMS Builder
Last Post: October 7, 2010   (RSS)

Hello All

I'm trying to get my head around an interesting idea. I don't know if I can make it work but (as with all things in CMSB) I'm sure there's a simple way to do it.

In short, how can I post a direct link to an attachment which was uploaded to another document?

Here's the theory...

Imagine a site carved up into different categories. For the purpose of this idea the two most important categories are News and Downloads.

All of the downloads (pdf files) are uploaded to articles in the Download category.

In the News category I want to post articles alerting readers to the availability of each new pdf as it becomes available. Then, beneath each alert, I want to post the link to the download itself. (as opposed to a link to the article to which the download is attached.)

Ideally I'd like to use a multi-select list which lists all of the available uploads. I could then click on each one I want to provide a link to.

I'm sure it can be done. I know you can use gizmos like beta_lookup to show data from other items - but as we're dealing with uploads I suspect things might be a bit more complicated. That's why I'm asking here first before I head off on my own!

Any help would be most welcome.

:0)

Perch

Re: [Perchpole] Linking to Uploads Across Multiple Documents?

By Jason - September 28, 2010

Hi Perch,

Interesting idea.

What you could do is have your list field link directly to the uploads table. When creating your list field, for "List Options" select "Get Options from MySQL Query).
Then use this query:
SELECT num, urlPath
FROM `<?php echo $TABLE_PREFIX ?>uploads`

You can change the second field if you like, but urlPath would probably be the most descriptive. So, this would mean that you'd be storing a list of record numbers from the uploads table that you could then retrieve. An added benefit is that you'd be able to use the ":labels" pseudo field to get the urlPaths of your select uploads without a second query.

Hope this gets you started. Let me know if you run into any other issues.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Linking to Uploads Across Multiple Documents?

Hi, Jason -

Thanks for your note - and the advice. I really think I'm making progress...

Unfortunately I've just realised I cannot call "filename" as this column is not part of the "uploads" table.

I can't see a way around this. What would you suggest?

:0/

Perch

Re: [Perchpole] Linking to Uploads Across Multiple Documents?

Right -

I've followed my nose and discovered I could use REPLACE in the MySql statement to strip out the URLpath - leaving just the filename.

SELECT num, REPLACE(urlPath,'/cms/uploads/','' )urlPath
FROM `<?php echo $TABLE_PREFIX ?>uploads`


It's clunky but it appears to work!

Is this the "right" way?

:0)

Perch

Re: [Perchpole] Linking to Uploads Across Multiple Documents?

By Chris - September 28, 2010

Hi PerchPole,

Personally, I'd have a separate section for Downloads, even if there was only one Upload Field for that section. Then, your News section could have a List Field to select which Download you want shown. Then, it's a simple matter of doing a getRecords() call to get the Download record for your News record. (beta_lookup would work too, but there's no need for that.)

If both ways work equally well, whichever way is easier for you is the right way. ;)

I hoep this helps! Please let me know if you have any questions.
All the best,
Chris

Re: [chris] Linking to Uploads Across Multiple Documents?

Hi, Chris -

Thanks for your input. I'm very pleased with the way Jason's suggestion has worked out. However, I'd be grateful if you would explain your own idea in more detail as it sounds like the same approach I tried first - but couldn't make it work! (on a List page)

:0)

Perchpole

Re: [Perchpole] Linking to Uploads Across Multiple Documents?

By Chris - September 29, 2010

Hi Perchpole,

Oh, on a list page you'd need to either use a join, beta_lookup, or a bunch of PHP and a getRecords call (which would basically mimic what beta_lookup would do.) I was thinking this would be for a detail page. :)
All the best,
Chris

Re: [chris] Linking to Uploads Across Multiple Documents?

Hi, Chris -

In this particular instance I want to run it off a List page (a page of news story snippets complete with links to downloads).

Should I just go and research JOIN projects on the forum or would you like to give me a practical example of how you would do it (plz!)...

:0)

Perch

Re: [Perchpole] Linking to Uploads Across Multiple Documents?

By Chris - September 29, 2010

Hi Perch,

Er, I mis-spoke. Since you want uploads, simple joining won't help.

Here's an example of how to do this without beta_lookup:

<?php
list($news) = getRecords(array(
'tableName' => 'news',
));

$attachmentNums = array_filter(array_unique(array_pluck($news, 'attachment')));

if ($attachmentNums) {
list($attachments) = getRecords(array(
'tableName' => 'attachments',
'where' => 'num IN (' . implode(',', $attachmentNums) . ')',
));
}
else {
$attachments = array();
}
$attachmentsByNum = array_combine(array_pluck($attachments, 'num'), $attachments);
?>

<ul>
<?php foreach($news as $record): ?>
<li>
<b><?php echo $record['title'] ?></b><br />

<?php if ($record['attachment']): ?>
<?php $attachment = $attachmentsByNum[$record['attachment']] ?>
<?php foreach($attachment['uploads'] as $upload): ?>
<a href="<?php echo $upload['urlPath'] ?>"><?php echo $upload['filename'] ?></a><br/>
<?php endforeach ?>
<?php endif ?>
</li>
<?php endforeach ?>
</ul>

All the best,
Chris