Click to See Complete Forum and Search --> : Quick PHP/MySQL question


MentatTux
02-21-2001, 03:20 PM
How would I either only display the first few paragraphs from a db entry, or just select them? As in, the first 400-500 chars(if there's that many)

Thanks

Salmon
02-21-2001, 04:17 PM
Two ways:

with PHP . . .


// the number of characters to limit to
$chrLimit = 450;


print( substr($results["paragraph"], 0, $chrLimit) );


or let the database do it . . .
"SELECT SUBSTRING(paragraph, 1, 450) FROM myTable"

The latter method is probably better.

[ 21 February 2001: Message edited by: Salmon ]

[ 21 February 2001: Message edited by: Salmon ]

david
02-22-2001, 03:38 PM
Thanks!

david
02-22-2001, 03:40 PM
Oh yeah! is there any way to check if there actually ARE 450 chars there first? So thqt I don't give a link to "the rest of the story" when there is none?

Salmon
02-23-2001, 12:04 AM
Oh yeah! is there any way to check if there actually ARE 450 chars there first? So thqt I don't give a link to "the rest of the story" when there is none?

The way I would do it is to use the MySQL function SUBSTRING that I mentioned above to actually get the info from the database.

Then I would use PHP to test the length of the string like this . . .



// assuming a good result from stuff the was mentioned previously . . .

// if the result length isn't less than your allowed maximum, then there's more to read
if ( !(strlen($results["paragraph"]) < $chrLimit) ) {

// print your "Read More" link

}