Linking to Uploads Across Multiple Documents?

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

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

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

"Mis-spoke"

Didn't know you'd had a career in politics?!!!

:oP

Perch