Last post by author to embed externally

Discussion corner for Developers of Serendipity.
Post Reply
aschlemmer
Regular
Posts: 70
Joined: Fri Feb 23, 2007 6:13 pm
Location: Freiburg/Germany
Contact:

Last post by author to embed externally

Post by aschlemmer »

Hi!

I'm looking for a smart way to display the last post by author from a s9y installation (domain a) on my personal homepage (domain b), same DB server for both.

It would also be nice to have a way to show the last comment by author in the same configuration.
Is there a solution with RSS or similar?

Thanks for any hints or code snippets :)
Achim
www.ms-reporter.de
Pharma-unabhängige Infos für Multiple Sklerose-Betroffene
schnellze.it
Einzigartiges Zusammenspiel aus Popfetcher und s9y
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Last post by author to embed externally

Post by garvinhicking »

Hi!

Two ways, the API way:

Code: Select all

<?php
chdir('/path/to/serendipity');
include 'serendipity_config.inc.php';
$serendipity['GET']['viewAuthor'] = 7; // Enter author id!
$entries = serendipity_fetchEntries(null, null, true); // Pass the parameter for limit=1, I'm too lazy to look it up - it's documented in include/functions_entries.inc.php in the function header
serendipity_printEntries($entries);
(More info: http://www.s9y.org/78.html#A7)

And the DB way:

Code: Select all

$sql = mysql_query("SELECT * FROM serendipity_entries WHERE authorid = 7 ORDER BY timestamp DESC LIMIT 1");
$entry = mysql_fetch_array($sql, MYSQL_ASSOC);
print_r($entry);
Printing comments would work in a similar way, you would need to query serendipity_comments for that.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
aschlemmer
Regular
Posts: 70
Joined: Fri Feb 23, 2007 6:13 pm
Location: Freiburg/Germany
Contact:

Re: Last post by author to embed externally

Post by aschlemmer »

Hi!

Thanks for your answer!
I tried this one:
garvinhicking wrote:the API way
with a blank test file containing;

Code: Select all

<?php
chdir('/path/to/s9y/');
include 'serendipity_config.inc.php';
$serendipity['GET']['viewAuthor'] = 3;
$entries = serendipity_fetchEntries(null, true,1);
serendipity_printEntries($entries);
chdir('/path/to/test-file/');
?>
and I get an error message:
Fatal error: Call to a member function assign() on a non-object in /path/to/s9y/include/functions_entries.inc.php on line 923

I'm lost with this message. What's going wrong here?

Regards,
Achim
www.ms-reporter.de
Pharma-unabhängige Infos für Multiple Sklerose-Betroffene
schnellze.it
Einzigartiges Zusammenspiel aus Popfetcher und s9y
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Last post by author to embed externally

Post by garvinhicking »

Hi!

Sorry; after the viewAuthor variable, insert another line "serendipity_smarty_init();".

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
aschlemmer
Regular
Posts: 70
Joined: Fri Feb 23, 2007 6:13 pm
Location: Freiburg/Germany
Contact:

Re: Last post by author to embed externally

Post by aschlemmer »

OK, thanks: I modified my code to

Code: Select all

<?php
chdir('/path/to/s9y/');
include 'serendipity_config.inc.php';
$serendipity['GET']['viewAuthor'] = 3;
serendipity_smarty_init();
$entries = serendipity_fetchEntries(null, true,1);
serendipity_printEntries($entries);
chdir('/path/to/test-file/');
?>
The output is nothing -- a .php file containing only this results in an empty page.

Additionally, if I put it together with only one character (or other html), I get an error:
Warning: Cannot modify header information - headers already sent by (output started at /path/to/test-file/new.php:1) in /path/to/s9y/include/functions_config.inc.php on line 700

What's going on?

Lost in code,
Achim
www.ms-reporter.de
Pharma-unabhängige Infos für Multiple Sklerose-Betroffene
schnellze.it
Einzigartiges Zusammenspiel aus Popfetcher und s9y
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: Last post by author to embed externally

Post by Timbalu »

Try with

Code: Select all

<html>
    <head>
        <title>test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	</head>
	<body>
<?php
chdir('./serendipity/');
include 'serendipity_config.inc.php';
$serendipity['GET']['viewAuthor'] = 1;
$entries = serendipity_fetchEntries(null, true,1);
$author_post = serendipity_printEntries($entries, 0,false,'','return',false,false,false);
echo '<pre>';print_r($author_post);echo '</pre>';
chdir('../');
?>
     </body>
</html>
If you really want to use Smarty you will need a template file like the docu said:

Code: Select all

<?php
// 1: Switch to the Serendipity path. We need to use chdir so that the s9y framework can use its relative calls.
chdir('/home/www/public_html/blog/');

// 2: Start the Serendipity API
include 'serendipity_config.inc.php';

// 3: Start Smarty templating
serendipity_smarty_init();

// 4: Get the latest entries
$entries = serendipity_fetchEntries(null, true,1);

// 5: Put all the variables into Smarty
serendipity_printEntries($entries);

// 6: Get the template file
$tpl = serendipity_getTemplateFile('entries.tpl', 'serendipityPath');

// 7: Format and output the entries
$serendipity['smarty']->display($tpl);

// 8: Go back to where you came from
chdir('/home/www/public_html/');

?>
.... but this is quite complicated to just display some title and a truncated body somewhere else, so I would go the second way via SELECT.
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian
aschlemmer
Regular
Posts: 70
Joined: Fri Feb 23, 2007 6:13 pm
Location: Freiburg/Germany
Contact:

Re: Last post by author to embed externally

Post by aschlemmer »

Both ways are working now ... thank you very much for your support!
www.ms-reporter.de
Pharma-unabhängige Infos für Multiple Sklerose-Betroffene
schnellze.it
Einzigartiges Zusammenspiel aus Popfetcher und s9y
aschlemmer
Regular
Posts: 70
Joined: Fri Feb 23, 2007 6:13 pm
Location: Freiburg/Germany
Contact:

Re: Last post by author to embed externally

Post by aschlemmer »

Hi!

that's me again ... with an additional question regarding character encodings. While I fetch the most recent entry with

Code: Select all

mysql_query("SELECT * FROM serendipity_entries WHERE authorid = 3 ORDER BY timestamp DESC LIMIT 1");
and print it with

Code: Select all

<?php echo( $entry["title"] ); ?>
the output doesn't show mutated vowels (ä, ö, ü, ß)

Example: Drei sch�ne Websites mit sinnvollem CSS3

That's not beautiful, I'm sure there's a switch to deal with that issue?
Thanks in advance and regards,
Achim
www.ms-reporter.de
Pharma-unabhängige Infos für Multiple Sklerose-Betroffene
schnellze.it
Einzigartiges Zusammenspiel aus Popfetcher und s9y
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: Last post by author to embed externally

Post by Timbalu »

Tell the browser to read the output file as UTF-8.
Look at my first examples <meta...> head part.

If this isn't possible, while the output page is using latin elsewhere, you should convert your database read with either

Code: Select all

mysql_query("SET NAMES utf8");
before the SELECT, or use PHP

Code: Select all

iconv("UTF-8", "CP1252", $data)
to convert from UTF-8 to ISO-8859-15 (which is better than utf8_decode($data))
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian
aschlemmer
Regular
Posts: 70
Joined: Fri Feb 23, 2007 6:13 pm
Location: Freiburg/Germany
Contact:

Re: Last post by author to embed externally

Post by aschlemmer »

Timbalu wrote:Tell the browser to read the output file as UTF-8.
Look at my first examples <meta...> head part.
This has been set.
Timbalu wrote:If this isn't possible, while the output page is using latin elsewhere, you should convert your database read with either

Code: Select all

mysql_query("SET NAMES utf8");
before the SELECT
To summarize:

Code: Select all

<?php
mysql_query("SET NAMES 'UTF8'"); 
$sql = mysql_query("SELECT * FROM serendipity_entries WHERE authorid = 3 ORDER BY timestamp DESC LIMIT 1");
$entry = mysql_fetch_array($sql, MYSQL_ASSOC); ?>
<p><?php echo( date( "d. M. Y", $entry["timestamp"] ) ); ?></p>
<a href="http://schnellze.it/?p=<?php echo( $entry["id"] ); ?>"><?php echo( $entry["title"] ); ?>
works perfectly. Thanks for the UTF-8 help!

I'm asking myself why I'm still getting exactly one result when setting the

Code: Select all

LIMIT 2
in the sql query? I'm afraid that it would be better for me to learn some php/mysql instead of asking such basics here :oops:

Regards,
Achim
www.ms-reporter.de
Pharma-unabhängige Infos für Multiple Sklerose-Betroffene
schnellze.it
Einzigartiges Zusammenspiel aus Popfetcher und s9y
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: Last post by author to embed externally

Post by Timbalu »

That looks like a mysql fetch issue, but possibly isn't, as further on you don't loop the array fetched by mysql. Try to write like this

Code: Select all

$entries = mysql_fetch_array($sql, MYSQL_ASSOC); 
foreach ($entries AS $entry) { ?>
<p><?php echo( date( "d. M. Y", $entry["timestamp"] ) ); ?></p>
<a href="http://schnellze.it/?p=<?php echo( $entry["id"] ); ?>"><?php echo( $entry["title"] ); ?><hr />
<?php } ?>
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian
Post Reply