you can try this:
create a snippet ’addScore’ with:
<?php
$sum = $modx->getPlaceholder('scores_sum');
$sum = !empty($sum) ? $sum+$score : $score;
$modx->setPlaceholder('scores_sum',$sum);
in your getResources - tpl place:
[[!addScore? &score=`[[+yourScoreTv]]`]]
and where you want to output the sum place:
Are you displaying the total views of all pages created by the person viewing the page?
Hi Bruno,
I tried that setup but no Joy. Will investigate further though and see if I can get to work.
Bob, I am displaying the total video views of the videos created by certain users.
Same like a YouTube channel, you see total video views in the channel.
Let me know your thoughts.
Im thinking it would be good if I could just pull all of the document IDs created by a certain users and populate this snippet with it.
[[!AddScores? &ids=`12,15,33,45`]]
Stuart
I think you want something like this (the snippet tag will contain the IDs of the users):
<?php
/* AddScores snippet */
$tvId = 1; // put the id of the TV here
$output = '';
$userIds = explode(',', $ids);
$tv = $modx->getObject('modTemplateVar', $tvId);
foreach ($userIds as $userId) {
$pages = array();
$userObj = $modx->getObject('modUser', $userId);
if ($userObj) {
$pages = $userObj->getMany('CreatedResources');
$score = 0;
if (!empty($pages)) {
foreach($pages as $page) {
$score += (int) $tv->getValue($page->get('id'));
}
$output .= '<p>User: ' . $userObj->get('username') . ' . . . Views: ' . $score . '</p>';
}
}
}
return $output;
I’m sure there is a more efficient (but more complicated) method, since this pulls a lot of information you don’t need, but it should work.
Hey Bob,
Thanks for this.
It worked perfectly!
I added this line so it determines the usser from their channel page.
$ids = $modx->documentObject[’createdby’];
Starting to study more PHP and hopefully will be writing snippets myself one day soon!
Could you post your code?
I suspected as much. You’re only getting the pages for one user, not a collection of users, so the code I wrote is more complex than it needs to be. Try this:
<?php
/* AddScores snippet */
$tvId = 8; // put the id of the TV here
$output = '';
$UserObj = $modx->resource->getOne('CreatedBy');
$tv = $modx->getObject('modTemplateVar', $tvId);
$pages = $userObj->getMany('CreatedResources');
$score = 0;
foreach($pages as $page) {
$score += (int) $tv->getValue($page->get('id'));
}
$output .= ' ' . $score . '';
return $output;
No loop is necessary except the one to add up the data from the pages and I left out the sanity checks because we can assume that the current page has a creator and that that user has created at least one page (the current one).