Forum Moderators: coopster

Message Too Old, No Replies

Limiting the amount of text displayed from a mysql query

         

edward301

10:00 pm on Jun 29, 2005 (gmt 0)

10+ Year Member



I have a query that will output over 100 words on average I would like to limit the text displayed to 30 words is this possible with either php or mysql?


<?
$qr1 = mysql_query("SELECT * FROM MYTABLE WHERE id=$id AND status=1 ORDER BY sum DESC LIMIT 50");
while( $row = mysql_fetch_object($qr1) ){
?>

sned

10:30 pm on Jun 29, 2005 (gmt 0)

10+ Year Member



Do you mean that the words are stored in text form in your database, so as you loop through the results you only want to print the first 30 words of each result?

If that's the case, you could do something like:


while($row = mysql_fetch_object($qr1)){
$text = $row->textvar;
// assuming the words are separated by spaces
$text_array = explode(' ', $text);
for($x = 0; $x < 30; $x++){
echo $text_array[$x];
}
}

But perhaps you mean something entirely different ...

-sned

edward301

10:49 pm on Jun 29, 2005 (gmt 0)

10+ Year Member



Thanks Sned that works almost perfectly but the text that is displayed has no spaces.

onetwothreefourfivesixseveneightnineteneleventwelvethirteenfourteenfifteensixteenseventeeneighteennineteentwenty

Instead of
one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty

sned

10:56 pm on Jun 29, 2005 (gmt 0)

10+ Year Member



Ok, instead of

echo $text_array[$x];

Try this:

echo $text_array[$x], ' ';

Or whatever formatting you choose to add.

-sned

edward301

11:10 pm on Jun 29, 2005 (gmt 0)

10+ Year Member



Thanks sned you've been a great help it works perfectly now.