Owen
|
Posted: 01/30/2003, 5:06 PM |
|
PHP with Templates under CCS2
I wish to limit the amount of characters returned into a label (ie:30) after a lookup so that I can produce "snippetts" of a news story....
Ie: if a sentence (or story) is 1000 characters (like a normal story) I want only to produce the first 30, to end up with something like this:
"The latest news on the sugar field is that the..."
Then I will do a link to the full story.
I am sure that this is easy but how? Is it a LIMIT call in the SQL or will I need to play with php to chop the db output?
I am working in CCS2 under Php with templates.
|
|
|
Zee
|
Posted: 04/01/2003, 5:52 PM |
|
Bump
I want to know too.... Thanks
|
|
|
Edd
|
Posted: 04/01/2003, 9:58 PM |
|
You can do it in your SQL.
Change the Data Source's Properties from "Table" to "SQL" so it looks something like this
Select * from Titles where Titles.Author = '{author}'
Then change the SQL to reflect the columns you want
Select ID, Name, Left(Story,30) as story from Titles where Titles.Author = '{author}'
The "Left(Story,30)" as story will return the 1st 30 characters.
Depending upon your database you can even concatinate the "..." by entering
Select ID, Name, (Left(Story,30) + '...') as story from Titles where Titles.Author = '{author}'
Then in the presentation layer change the story column to a hyperlink.
This could also be done in the before show event but you should avoid retrieving large blochs of data to a web server as it can really slow it down.
Sorry this is very quick but you should get the gist.
Hope this helps
Edd
|
|
|
Zee
|
Posted: 04/02/2003, 6:03 AM |
|
Many thanks Edd for your help. I still can't get it to work for me though. I am using latest CCS2 with PHP4 with templates. My grid just has ID, Date_posted, Title, Content. I just want to limit the content, I don't see where the {author} bit fits in. This is what I have.
Select news_id,news_title,date_posted, (Left(news_content,30) + '...') as news_content from news where news.news_content = '{news_content}'
ORDER BY date_posted
It gets through the test but shows no records when run.
Cheers!
|
|
|
Zee
|
Posted: 04/02/2003, 6:35 AM |
|
Oh I forgot ... How does the before show event method go in CCS2? I had it working in CC portal example but things have changed.
I would like the format to be like:
Today there was a great big ...More
Thanks.
|
|
|
Edd
|
Posted: 04/02/2003, 9:47 PM |
|
Zee,
You are probably using MySQL (my example was MS SQL)- therfore the SQL syntax is different - try:
--------------
Select news_id,news_title,date_posted, CONCAT(LEFT(news_content,30),'...More') as news_content from news where news.news_content = '{news_content}'
ORDER BY date_posted
--------------
CONCAT is the MySQL equivalent to concatenate strings.
Re the beforeShow event have a look in the Help for CCS2.0 it gives a good explanation.
Hope this helps.
Edd
|
|
|
Zee
|
Posted: 04/03/2003, 5:26 AM |
|
Thanks again Edd. Have it working now
I had trouble with my WHERE statement - where news.news_content = '{news_content}' - It shows no records when run. So I just created a hyperlink called {more} linked to the news.php page on the news grid next to my news_content. Anyways!
Cheers!
|
|
|
Steven
|
Posted: 04/03/2003, 7:39 AM |
|
I did not author this, its a text which from somewhere i have snipped it as usefull code, I keep a dir of odd text files for things i find usefull, and this was sitting in it, i think this could be used to limit your label/field length, also it will break at a space, not in the middle of a word, so its very usefull for news snippets posts etc..
hope you find it usefull, thanks go to whoever the original author was ?
Steven Dowd
=====
The important thing here is to ensure that you get a snippet not truncated
in the middle of a word. You could use function similar to this one (add it
to your Common.php or wherever you prefer):
// -------------------------------------------------
function smart_truncate($str2cut, $trig_val=30){
$newstr="";
if(strlen($str2cut) <= $trig_val)
return $str2cut;
$newlen=strrpos(substr($str2cut,0,$trig_val)," ");
if($newlen>0)
$newstr=substr($str2cut,0,$newlen);
else
$newstr=substr($str2cut,0,$trig_val);
return $newstr."...";
}
// -------------------------------------------------
Set the $str2cut parameter to the story you want to get a snip from, and
optionally set $trig_val to the maximum length of the resulting string
(defaults to 30 chars.)
To use:
$full_story=CCDLookUp("content_field", "news_table", "id_field=xxx", $db);
$snip_story=smart_truncate($full_story);
=============
|
|
|
|