Page 1 of 1
displaying a limit number of rows from a text field
Posted: Thu Feb 17, 2005 7:59 am
by pelegk2
i have a text field which hold's a text written by user
and i wantto do a preview while will show for each user only the first 3 row's.
how can i do that?
thnaks in advance
peleg
Posted: Thu Feb 17, 2005 8:05 am
by n00b Saibot
Text Field or Text Area

and where do you want to preview it.
Please post full details when asking something
ok
Posted: Thu Feb 17, 2005 8:11 am
by pelegk2
it'ss Text Area
and i wantto show for example a list of articles and a preview of 3 rows from each 1 of them in a page
how do ido that?
Posted: Thu Feb 17, 2005 8:17 am
by n00b Saibot
Code: Select all
$lines = explode("\n",$textarea_contents);
echo "Preview : " . $linesї0] . $linesї1] . $linesї2];
or
Posted: Thu Feb 17, 2005 9:30 am
by s.dot
You could use the substr() function to call the first 100 characters or so from the entries.
It'd look like this.
Code: Select all
$preview = substr($entry, 0, 100);
echo $preview;
Posted: Thu Feb 17, 2005 9:36 am
by Maugrim_The_Reaper
Second solution is probably preferable since it avoids long lines.
Textareas usually wrap text - but that's just a visual aid. Exploding on the basis of newlines could just as easily return three paragraphs of text as three lines...
Posted: Thu Feb 17, 2005 9:41 am
by feyd
preferred is more likely to be on words, not characters. Look through the Useful Posts thread for links to getting data by word count.
Posted: Thu Feb 17, 2005 9:49 am
by s.dot
I think calling a certain amount of characters would be more effective, as it would imply that there is more text available and this is only a preview.
Code: Select all
$preview = substr($entry, 0 , 100);
echo "$preview...";
with the word being cut off and "..." being displayed it would imply there is more text to be read.
If you just display a certain number of words the user might think, "well, hrmm, that's it?"
Posted: Thu Feb 17, 2005 9:50 am
by feyd
I guess you've never looked at the thread.

Posted: Thu Feb 17, 2005 10:00 am
by Maugrim_The_Reaper
If you only need 100 characters - why go through regex to fiddle with word counts, spaces, non-spaced lines, etc... You can't go wrong with a simple substr()

Posted: Thu Feb 17, 2005 10:20 am
by feyd
a simple substr() disregards complete words. It looks quite strange to cut a word in half. So depending on what you want to do, yes it can go very wrong.
how can you do a word count with regEx?
Posted: Thu Feb 17, 2005 1:26 pm
by pelegk2
thnaks
Peleg
Posted: Thu Feb 17, 2005 2:37 pm
by timvw
this is a nice query trick for mysql i once found
Code: Select all
SELECT SUBSTRING_INDEX(content,' ',20) as description
FROM message