Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
How can I return multiple values in a plugin function?
I wanted to make a plugin that will show me precisely when people have last logged in into textpattern, rather than just knowing the month/year as shown on the admin/users tab. This is what I have so far:
function jdw_show_logins() {
$jdw_some_text = "last login on";
$jdw_rs = safe_rows("last_access, RealName", "txp_users", "RealName LIKE '%';");
rsort($jdw_rs);
echo ("<table><th>Real Name</th><th>$jdw_some_text</th>");
foreach ($jdw_rs as $jdw_value) {
echo ("<tr>");
echo ("<td>".$jdw_value['RealName']."</td><td> ". $jdw_value['last_access']."</td>");
echo ("</tr>");
}
echo ("</table>");
}
This works sort of okay, but the echo statements place the output outside of the document flow.
I know I’m supposed to return the values, but I understand that I can return only one value. I’m unsure how to get the output I need. Can I return it as an array somehow?
Cheerio,
Jan
TXPDream – A Textpattern Tag Library for Adobe Dreamweaver. (updated for 4.2.0) | jdw_if_ajax – Only serve certain parts of a page when requested with AJAX
Offline
Re: How can I return multiple values in a plugin function?
Call the function as a tag, then the function return value will replace the tag. Instead of echo
, collect the output into a variable and then return it. Something like this:
Oops, sorry, wasn’t paying close attention — didn’t realize this is for admin-side display. In that case you want to look into pluggable_ui
callbacks to choose when the table gets echo’d. See here. Where do you want the output to appear?
Last edited by jsoo (2009-09-08 02:29:43)
Code is topiary
Offline
Re: How can I return multiple values in a plugin function?
Thanks Jsoo. Actually, this plugin is mostly for learning purposes. It makes most sense to have it eventually on the admin side, but I was calling it from an article, so public side for now.
TXPDream – A Textpattern Tag Library for Adobe Dreamweaver. (updated for 4.2.0) | jdw_if_ajax – Only serve certain parts of a page when requested with AJAX
Offline
Re: How can I return multiple values in a plugin function?
Use return. No echo inside echo. It’s too echo :) For example:
function jdw_show_logins() {
$rs =
safe_rows(
'last_access,RealName',
'txp_users',
'1=1 order by last_access asc'
)
;
$out = array();
foreach($rs as $row)
$out[] =
'<tr><td>'.$row['RealName'].'</td><td>'.$row['last_access'].'</td></tr>';
return
'<table><th>Real Name</th><th>Last login on</th>'.
implode('',$out).
'</table>'
;
}
Last edited by Gocom (2009-09-08 04:31:06)
Offline
Re: How can I return multiple values in a plugin function?
Naice! Thanks, Jukka.
I knew i needed to return it, I just had no idea how to go about it. (still pretty much a php noob). Let’s see where I get stuck tomorrow when I try to get it to the admin side. ;-)
TXPDream – A Textpattern Tag Library for Adobe Dreamweaver. (updated for 4.2.0) | jdw_if_ajax – Only serve certain parts of a page when requested with AJAX
Offline
Re: How can I return multiple values in a plugin function?
Just another version of the same thing (and longer). If you’re getting all of the last logins (even if it might be one), you’ll probably want to return an array like this: {{'userA', '<date>'}, {'userB', '<date>'}...}
and feed it into a presentation-function. It’s more flexible to separate your presentation, so you can manipulate your data more easily.
So for your logic:
/**
* Returns an array of login information.
*
* @param string $since EX: '30 days'
* @return array {{usr, date}, ...}
*/
function jdw_getLogins($since)
{
$rs = safe_rows('RealName, last_access',
"last_access <= date_sub(current_date,
interval {$since})
order by last_access asc");
// no logins since $since
if (!$rs)
return 0;
foreach ($rs as $row)
$out[] = array($row['RealName'], $row['last_access']);
return $out;
}
I added a last_access
check, like if you wanted to see the users that had logged in within the past 30 days. If you don’t need this, then use Jukka’s 1=1
(the $where
parameter isn’t optional in TXP’s DB functions, hence the need to “trick” it with 1=1
).
Then for formatting:
/**
* Prints a table of login information.
*/
function jdw_printLogins()
{
$logins = jdw_getLogins('30 days');
if (!$logins)
return 'No logins <msg>';
$out[] = <<<EOF
<table>
<th>Name</th>
<th>Last login</th>
EOF;
foreach ($logins as $l)
{
// you don't need these vars, but I don't know if you want to display
// names in UPPERCASE or format the date differently
$name = $l[0];
$date = $l[1];
$out[] = <<<EOF
<tr>
<td>{$name}</td>
<td>{$date}</td>
</tr>
EOF;
}
$out[] = '</table>';
return implode('', $out);
}
The <<<EOF
is a heredoc – they’re pretty handy for dealing with multi-line strings.
I should mention that I haven’t tested this – no TXP install ATM.
Offline
Re: How can I return multiple values in a plugin function?
jm wrote:
Just another version of the same thing (and longer).
Jon-Michael officially took it to the next level :-)
jm wrote:
I should mention that I haven’t tested this – no TXP install ATM.
You don’t need daily fix? Omgzgod. I blame WordPress!
I should after mention. I didn’t test mine either. In reality it’s just 100% malware ;)
Last edited by Gocom (2009-09-08 07:01:46)
Offline
Re: How can I return multiple values in a plugin function?
Gocom wrote:
You don’t need daily fix? Omgzgod. I blame WordPress!
LOL. I just did a clean install of OS X 10.6 last week and haven’t installed TXP yet. I’ll be setting it up tomorrow with the rest of my devel environment, since my plugins haven’t had an update in awhile.
Offline
Re: How can I return multiple values in a plugin function?
Holy cooked turnips! Thanks a lot, jm. Much appreciated. This will definitely push my abilities from one-trick pony to two -trick pony once I get my head round it.
Gocom wrote:
I should after mention. I didn’t test mine either. In reality it’s just 100% malware ;)
Aaah… I was wondering what that swiss bank account # was doing in the code
TXPDream – A Textpattern Tag Library for Adobe Dreamweaver. (updated for 4.2.0) | jdw_if_ajax – Only serve certain parts of a page when requested with AJAX
Offline
Re: How can I return multiple values in a plugin function?
Is there a place on the wiki we could post examples like these?
Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker
Offline
Re: How can I return multiple values in a plugin function?
Yeah, I think the wifi can use some beefing up on stuff like that – I’ll post this under tutorials on the wiki later today and link it from extending textpattern as well later
Last edited by JanDW (2009-09-08 16:27:55)
TXPDream – A Textpattern Tag Library for Adobe Dreamweaver. (updated for 4.2.0) | jdw_if_ajax – Only serve certain parts of a page when requested with AJAX
Offline
Re: How can I return multiple values in a plugin function?
An initial question:
from gocoms code:
$rs =
safe_rows(
'last_access,RealName',
'txp_users',
'1=1 order by last_access asc'
)
What does 1=1 mean exactly? I guess it means select all?
I’ll need some more time to understand jon-michael’s functions ;-)
TXPDream – A Textpattern Tag Library for Adobe Dreamweaver. (updated for 4.2.0) | jdw_if_ajax – Only serve certain parts of a page when requested with AJAX
Offline