displaying a limit number of rows from a text field

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

displaying a limit number of rows from a text field

Post 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
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Text Field or Text Area :?: and where do you want to preview it.
Please post full details when asking something
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

ok

Post 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?
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Code: Select all

$lines = explode("\n",$textarea_contents);
echo "Preview : " . $linesї0] . $linesї1] . $linesї2];
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

or

Post 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;
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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?"
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I guess you've never looked at the thread. :roll:
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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() :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

how can you do a word count with regEx?

Post by pelegk2 »

thnaks
Peleg
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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
Post Reply