donboe — 2011-05-19T08:00:25-04:00 — #1
I use SUBSTRING_INDEX quite a lot on Eccommerce sites where for a listing of products I would like to extract a certain number of words from the actual product description:
SUBSTRING_INDEX('description', ' ' , 20) AS shortDesc
In most case this works like a charm! But there are those moments that the actual product description is devided in paragraphs. If in those cases the number of words in the first paragraph doesn't match the number of words in the SUBSTRING_INDEX, words from the second paragraph are added until it reach the number of words but with this the space between the paragraphs is included in my output. Is there any way to avoid this?
Thank you in advance
guido2004 — 2011-05-19T09:03:13-04:00 — #2
What do you want to do? Eliminate the space between paragraphs? Or show only the first paragraph?
donboe — 2011-05-19T09:56:23-04:00 — #3
Hi Guido, although my first reaction to your question was, lets elliminate the space between paragraphs, I think I would prefer the second option (show only the first paragraph). But if you don't mind can you show me the solution for the elimination for the space between the paragraphs as well?
Thank you in advance,
r937 — 2011-05-19T16:04:58-04:00 — #4
how exactly would you detect where one paragraph ends and the next one begins?
also, are you sure you should be doing this with sql in the first place?
where's your html coming from?
ibazz — 2011-05-19T20:29:31-04:00 — #5
what about getting the whole description from the db and showing in your output, just the first paragraph? Then you could display a 'More>>' link so the user could choose to read the full description and to facilitate this, you could use a JS solution, to avid reload of the page.
donboe — 2011-05-20T11:08:33-04:00 — #6
@ Rudy: I have no idea either with the paragraphs, but since Guido asked explicit what I would like to do I presumed, both ways were possible! But if not what would be the best approach. The text is entered by the site owner in a customized controlroom. It is just one text area where with enter a new parapraph is created.
@IBazz I can't use your approach, the way the listing is organized, but thank you for the idea
r937 — 2011-05-20T11:21:49-04:00 — #7
It is just one text area where with enter a new parapraph is created.
then you can do this --
SELECT SUBSTRING_INDEX(content,CHAR(13,10),1) AS first_paragraph