Page 1 of 1

Last post by author to embed externally

Posted: Wed Apr 25, 2012 1:58 pm
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

Re: Last post by author to embed externally

Posted: Wed Apr 25, 2012 6:29 pm
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

Re: Last post by author to embed externally

Posted: Thu Apr 26, 2012 9:39 am
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

Re: Last post by author to embed externally

Posted: Thu Apr 26, 2012 10:51 am
by garvinhicking
Hi!

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

Regards,
Garvin

Re: Last post by author to embed externally

Posted: Thu Apr 26, 2012 4:11 pm
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

Re: Last post by author to embed externally

Posted: Thu Apr 26, 2012 4:28 pm
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.

Re: Last post by author to embed externally

Posted: Thu Apr 26, 2012 7:45 pm
by aschlemmer
Both ways are working now ... thank you very much for your support!

Re: Last post by author to embed externally

Posted: Mon May 07, 2012 4:39 pm
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

Re: Last post by author to embed externally

Posted: Tue May 08, 2012 8:20 am
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))

Re: Last post by author to embed externally

Posted: Tue May 08, 2012 9:05 am
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

Re: Last post by author to embed externally

Posted: Tue May 08, 2012 9:16 am
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 } ?>