Forum

Thread tagged as: Question, Members

Hi Drew,

Nothing is output on the page. If I comment out the if statement, downloads are displayed:

<?php 
    //if (perch_member_has_tag($result[0]['tag'])) {
        echo $result['html'];
    //}
?>

I'm guessing that the issue is with the if statement in some way - could it be anything to do with the following in the debug output

[message] => Undefined index: tag
Drew McLellan

Drew McLellan 2638 points
Perch Support

Do you have a field with the ID tag in your template?

Hi Drew, Yep, here is the template:

<perch:repeater id="downloads" label="Downloads">

    <perch:content id="tag" type="text" label="Member tag" suppress="true" />
    <a href="<perch:content id="download" type="file" label="Download" order="1" />"><perch:content type="text" id="desc" label="Title" order="1" required="true" title="true" /></a>

</perch:repeater>
Drew McLellan

Drew McLellan 2638 points
Perch Support

That's in a repeater, so it'll be downloads.tag.

So like this?

<?php
    if (perch_member_has_tag($result[0]['downloads.tag'])) {
        echo $result['html'];
    }
?>

Doesn't seem to work either?

Drew McLellan

Drew McLellan 2638 points
Perch Support

What does this output?

print_r($result[0]);

Here's the output...

Array ( 
    [_id] => 17 
    [downloads] => Array ( 
        [0] => Array ( 
            [download] => Array ( 
                [assetID] => 76 
                [title] => Test 
                [_default] => /admin/resources/ssh-instructions.rtf 
                [bucket] => default 
                [path] => ssh-instructions.rtf 
                [size] => 4630 
                [mime] => text/rtf 
            ) 
            [desc] => Andy's file 
            [_title] => Andy's file 
            [tag] => andy 
        ) 
        [1] => Array ( 
            [download] => Array ( 
                [assetID] => 76 
                [title] => Test 
                [_default] => /admin/resources/ssh-instructions.rtf 
                [bucket] => default 
                [path] => ssh-instructions.rtf 
                [size] => 4630 
                [mime] => text/rtf 
            ) 
            [desc] => David's file 
            [_title] => David's file 
            [tag] => david 
        ) 
    ) 
    [_page] => /members/index.php 
    [_pageID] => 2 
    [_sortvalue] => 1000 
)
Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, so your tags are in $result[0]['downloads'][x]['tag'] with x being a zero indexed counter.

Do you want to check all of those tags, or just the first?

Hi Drew,

I need to check all of the tags against the current logged in members tag and display results that match.

So here, if logged in as Andy Knight who has the tag "andy" I would just like to see results where the file uploaded has the tag "andy" and not the file with the tag 'david' or any other tags.

Does this answer the question?

Drew McLellan

Drew McLellan 2638 points
Perch Support

    $has_tag = false;
    if (isset($result[0]['downloads']) && count($result[0]['downloads'])) {
        foreach($result[0]['downloads'] as $download) {
            if (isset($download['tag']) && $download['tag']!='') {
                $tag = trim($download['tag']);
                if (perch_member_has_tag($tag)) {
                    $has_tag = true;
                    break;
                }
            }
        }
    }

    if ($has_tag) {
        echo $result['html'];
    }

Hi Drew,

Thanks for this. This code displays both of the files for both of the users.

Logged in as Andy Knight (tag=andy) displays the file with the tag 'andy' and the file with the tag 'david' and equally when logged in as David Sinnet (tag=david) displays the file with the tag 'andy' and the file with the tag 'david'.

Can you see why its is not working as expected?

Drew McLellan

Drew McLellan 2638 points
Perch Support

The example you showed me had both tags.

Hi Drew,

The example has two 'files'. The actual file is the same e.g. "ssh-instructions.rtf" but I uploaded them via the repeater, each with a different tag just to test the filtering.

The idea is to upload various files via the repeater and tag them with the same tag as a member so that that member only can view the file.

It's the files within the repeater I need to display/filter not the whole repeater.

Sorry for the confusion.

Drew McLellan

Drew McLellan 2638 points
Perch Support

if (isset($result[0]['downloads']) && count($result[0]['downloads'])) {
    foreach($result[0]['downloads'] as $download) {
        if (isset($download['tag']) && $download['tag']!='') {
            $tag = trim($download['tag']);
            if (perch_member_has_tag($tag)) {
                echo '<a href="'.$download['download']['_default'].'>';
                    echo PerchUtil::html($download['desc']);
                echo '</a>';
            }
        }
    }
}

Drew, you're a star! Works perfectly.

Thanks for your input on this thread - very much appreciated.

Thank you, Andy