Jump to content

Link a user to a page which has files


onjegolders
 Share

Recommended Posts

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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/>";
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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
 Share

×
×
  • Create New...