Linking to Uploads Across Multiple Documents?
25 posts by 3 authors in: Forums > CMS Builder
Last Post: October 7, 2010 (RSS)
By Perchpole - October 6, 2010
Again, very nearly. I'm just getting an "Invalid argument supplied for foreach()" error for the foreach loop...
<?php foreach($attachment['uploads'] as $upload): ?>
I think the problem is that $attachment returns a single upload record...
Array
(
[num] => 37
[order] => 3
[createdTime] => 2008-06-27 01:16:50
[tableName] => tag_articles
[fieldName] => attachments
[recordNum] => 1
[preSaveTempId] =>
[filePath] => /mypath/assets/Scottish_2008.pdf
[urlPath] => /assets/Scottish_2008.pdf
etc..........
:o+
Perch
Re: [Perchpole] Linking to Uploads Across Multiple Documents?
By Perchpole - October 6, 2010
<ul>
<?php foreach($news as $record): ?>
<li>
<b><?php echo $record['title'] ?></b><br />
<?php if ($record['attachment']): ?>
Attachments:
<ul>
<?php foreach(explode("\t", trim($record['attachment'])) as $attachmentNum): ?>
<?php $attachment = $attachmentsByNum[$attachmentNum] ?>
<?php foreach($attachment['uploads'] as $upload): ?>
<a href="<?php echo $upload['urlPath'] ?>"><?php echo $upload['filename'] ?></a><br/>
<?php endforeach ?>
<?php endforeach ?>
</ul>
<?php endif ?>
</li>
<?php endforeach ?>
</ul>
In fact if you just take out the code in red it works! You need to replace each instance of "$upload" with "$attachment" but it seems to do the trick.
Thanks chaps. Your help was very much appreciated.
:0)
Perch
PS. There is no ['filename'] for uploads only the pseudo [_filename] - but I'm not sure how to define it....
Re: [Perchpole] Linking to Uploads Across Multiple Documents?
By Chris - October 6, 2010
Oh, I thought you were going with my suggestion of having an "Attachments" section which would own the uploads. It seems you're directly selecting uploads. I'm glad you figured out how to fix things to work with your approach. :)
If you're getting uploads directly from the uploads table, there are a few fields which won't be defined for you. You can see which "pseudo-fields" are added (and how they're added) if you look in cmsAdmin/lib/upload_functions.php at the getUploadRecords() function.
Specificially, to generate the 'filename' pseudo-field, try this:
<?php echo pathinfo($attachment['filePath'], PATHINFO_BASENAME) ?>
Does that help? Please let me know if you have any questions.
Chris
By Perchpole - October 7, 2010
Thank you, both, very much for this.
Just one last question on this: Currently, in the section editor, I'm using a standard list arrangement to pull the uploads together, using num for values and urlPath for the label.
Would it be possible to construct an advanced MySQL query using PATHINFO_BASENAME? This should result in a list of filenames - not url paths - which would be far easier to read.
:0)
Perch
Re: [Perchpole] Linking to Uploads Across Multiple Documents?
By Jason - October 7, 2010
You can get the file name from the urlPath like this assuming the folder the images are in is called uploads:
SELECT num, SUBSTRING(urlPath FROM (LOCATE('/uploads/',urlPath)+9))
FROM `<?php echo $TABLE_PREFIX ?>uploads`
Hope this helps.
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/