Jump to content

Recommended Posts

Posted

Hey guys am having a bit of trouble linking users to downloadable files (which are being stored in pages).

Basically I have extended the user template to include a page field called "files_link" which links to the pages where the files are being displayed.

On the user's profile page (front-end) I want to display a little welcome message then the files that they are allowed to access.

This is my current code, can anyone help me figure it out?

<?php include("./header.inc"); ?>

<div id="profile_wrap">
<?php
// user is logged in
if ($user->isLoggedin()) { ?>

<h2>Welcome back <?php echo $user->first_name; ?></h2>

<?php $file_pages = $user->files_link;
if ($file_pages)
{
$files = $file_pages->files;
?>
<h6>Downloadable files</h6>
<ul>
<?php foreach ($files as $file) { ?>
<li>
<a href="<?php echo $file->url; ?>"><?php echo $file->description; ?></a>
</li>
<?php } ?>
</ul>

<?php } ?>

<?php } else { ?>

<h4>Sorry, you do not have access to this page, <a href="./login">please log in</a></h4>

<?php } ?>
</div>

<?php include("./footer.inc"); ?>

Thanks

Posted

I think you need to loop twice. First, through the related pages (it seems you're already doing this) and then through the files. But this depends on how you set-up your files fieldtype.

Posted

Thanks Arjen, it's all a bit confusing but I have:

a page fieldtype on the user profile called "file_links" which relates to pages (these pages store the files)

so these file pages have a fieldtype called "files".

So I'm looking to output the files within the pages that have been linked to the user, the idea being that the editor can upload different files and on the user profile can select which files that particular student has access to.

How to I get to output a field from within a page reference?

Thanks again for you help!

Posted

arjen hinted you the solution.

1 Loop: You have a page field with multiple pages (array) so you need to loop them.

2 Loop: Each page has files so loop it and output them.

$file_pages = $user->files_link;
if (count($file_pages)) {
 foreach($file_pages as $file_page) { // loop pages!
   $files = $file_page->files;
   foreach ($files as $file) { // loop files!
     echo "<a href='$file->url'>$file->description</a>";
   }
 }
} 
  • Like 2
Posted

Thanks Soma and Arjen,

That looks right but for some reason it's not outputting anything, I've checked and checked again but fieldnames seem correct.

If I print_r on $file_pages I get a whole page of code, I don't know if there's another way of checking the contents?

Cheers for your help

Posted

Thanks Soma and Arjen,

That looks right but for some reason it's not outputting anything, I've checked and checked again but fieldnames seem correct.

If I print_r on $file_pages I get a whole page of code, I don't know if there's another way of checking the contents?

Cheers for your help

"print_r" on page or pagearrays isn't really helpfu as it will show a lot you don't want to see. Usually you try to find out if there's any pages found and fo am echo here and there. just echo $file_pages should return a list with id(s) 1003|1022|1033.

Also I'm curious what you get with this?

echo $fields->get("files_link")->derefAsPage ."<br/>";
echo $fields->get("files")->maxFiles ."<br/>";
Posted

Hiya thanks again Soma,

If I echo $file_pages I get 1094 which I guess means it found a linked page which is good news

If I echo $files I get the name of a linked file

If I echo $file within the loop I get the name of the file but for some reason wrapping it in an a tag shows nothing

echo $fields->get("files_link")->derefAsPage ."<br/>";

echo $fields->get("files")->maxFiles ."<br/>";

the code above outputs 0.

Cheers

Posted

Could it be possible that you set the limit on of the files field is set to 1? I think in that case there isn't a loop, but a single file.

Posted

Hi Arjen,

No, it's set to 0 but each file "page" can contain 1 or several files in the Page Tree it looks like this:

Home

About

Files

-- Geography A-level files

-- Biology A-level files

-- Single information file

(Within all those subpages would be the files, but some subpages contain a collection of files, some will only contain a single file)

Posted

Check that output formatting is enabled for $user. Add $user->of(true); to the top of Soma's example and see if it makes any difference?

Posted

Thanks, Ryan finally figured out that having title was obsolete as at times there were multiple files to a page so I needed file->name. It seems like it was as simple as that. Thanks for all your help :)

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...