other view on existing block
Permalink 1 user found helpful
On a site i'm using the guestbook blok. What i'm aiming for is creating a short view with a listing of the last 3 comments on the home page. Coul'd any one help me?
Tried with Block::getByID etc. but no success.
Tried with Block::getByID etc. but no success.
I'm interested in this too, did you have any luck?
You need to create an override of the view, or an alternate view (also as an override). Seehttp://www.concrete5.org/documentation/how-tos/developers/change-th...
If I create an overiding view.php and save it as a custom template, that's going to create a new guestbook instance isn't it? What we were after is just a view of some entries from an existing block.
I found an interesersting post herehttp://www.webdesignblog.org.uk/2011/02/concrete-5-pull-random-gues... in which he creates pulls a single random entry from an existing guestbook.
I can see the principal at work here but I'm no programmer, I don't know how to do this for say a pull of three recent or even 3 random. I can see it must need a conditional loop or something to build the output...
I found an interesersting post herehttp://www.webdesignblog.org.uk/2011/02/concrete-5-pull-random-gues... in which he creates pulls a single random entry from an existing guestbook.
<?php $db = Loader::db(); $rs=$db->Execute('SELECT user_name, commentText FROM btGuestBookEntries where approved = 1 ORDER BY RAND() LIMIT 1'); $row = $rs->FetchRow(); $username=$row['user_name']; $commentText=$row['commentText']; echo "<em>"".$commentText.""</em><br />$username"; ?>
I can see the principal at work here but I'm no programmer, I don't know how to do this for say a pull of three recent or even 3 random. I can see it must need a conditional loop or something to build the output...
Here's something odd. This code above was not pulling in user_name. I stuffed around with it for ages then looked in the database for btGuestBookEntries, both the user_name and user_email fields are empty. But on the guestbook block display the comments show a posted by line and the name of that user. So is C5 not actually pushing the name/email through to the database and getting the name from somewhere else for the block?
I have a solution for you. I could appreciate the logic for achieving this based on the sample I found for pulling 1 random entry directly from the database. Having no idea how to wrtite PHP I just hunted through a few reference manuals and created my first bit of code.
Now I had intended to inlcude the user's name but as per my other post it does not appear into be going into the database in the first place. If it was then the code below works nicely.
But seeing that the username data is missing in the C5 database (for me at least), I removed the user name stuff from the following code. I've given the output a heading and the comments in a blockquote. But you could format that how you like.
To change the number of entries, make the LIMIT 3 your prefered quantity, and make num <=3 a matching value.
You can place this in any of templates to get it on all pages.
Or to make this even easier, I found a free PHP block on the marketplace.
http://www.concrete5.org/marketplace/addons/simple-php-block/...
It's based on the HTML block. Install that and you can add the PHP code in any editable area. BUT remove the php opening and closing tags from my code snippet.
Now I had intended to inlcude the user's name but as per my other post it does not appear into be going into the database in the first place. If it was then the code below works nicely.
<?php echo '<h3>Last 3 comments</h3>'; $db = Loader::db(); $rs=$db->Execute('SELECT * FROM btGuestBookEntries where approved = 1 ORDER BY entryID DESC LIMIT 3'); $num = 1; while ( $num <=3 ) { $row = $rs->FetchRow(); $username=$row['user_name']; $commentText=$row['commentText']; echo "<blockquote>$commentText<cite>$username</cite></blockquote>"; $num++; } ?>
But seeing that the username data is missing in the C5 database (for me at least), I removed the user name stuff from the following code. I've given the output a heading and the comments in a blockquote. But you could format that how you like.
To change the number of entries, make the LIMIT 3 your prefered quantity, and make num <=3 a matching value.
<?php echo '<h3>Last 3 comments</h3>'; $db = Loader::db(); $rs=$db->Execute('SELECT * FROM btGuestBookEntries where approved = 1 ORDER BY entryID DESC LIMIT 3'); $num = 1; while ( $num <=3 ) { $row = $rs->FetchRow(); $commentText=$row['commentText']; echo "<blockquote>$commentText</blockquote>"; $num++; } ?>
You can place this in any of templates to get it on all pages.
Or to make this even easier, I found a free PHP block on the marketplace.
http://www.concrete5.org/marketplace/addons/simple-php-block/...
It's based on the HTML block. Install that and you can add the PHP code in any editable area. BUT remove the php opening and closing tags from my code snippet.