Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-09-08 01:09:57

JanDW
Plugin Author
From: Providence, RI, USA
Registered: 2008-07-18
Posts: 327
Website

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

#2 2009-09-08 01:57:07

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

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

#3 2009-09-08 03:21:15

JanDW
Plugin Author
From: Providence, RI, USA
Registered: 2008-07-18
Posts: 327
Website

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

#4 2009-09-08 04:29:43

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

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

#5 2009-09-08 04:42:15

JanDW
Plugin Author
From: Providence, RI, USA
Registered: 2008-07-18
Posts: 327
Website

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

#6 2009-09-08 06:35:15

jm
Plugin Author
From: Missoula, MT
Registered: 2005-11-27
Posts: 1,746
Website

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

#7 2009-09-08 07:01:14

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

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

#8 2009-09-08 07:11:58

jm
Plugin Author
From: Missoula, MT
Registered: 2005-11-27
Posts: 1,746
Website

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

#9 2009-09-08 14:48:24

JanDW
Plugin Author
From: Providence, RI, USA
Registered: 2008-07-18
Posts: 327
Website

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

#10 2009-09-08 16:00:56

MattD
Plugin Author
From: Monterey, California
Registered: 2008-03-21
Posts: 1,254
Website

Re: How can I return multiple values in a plugin function?

Is there a place on the wiki we could post examples like these?


My Plugins

Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker

Offline

#11 2009-09-08 16:05:47

JanDW
Plugin Author
From: Providence, RI, USA
Registered: 2008-07-18
Posts: 327
Website

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

#12 2009-09-09 03:55:55

JanDW
Plugin Author
From: Providence, RI, USA
Registered: 2008-07-18
Posts: 327
Website

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

Board footer

Powered by FluxBB