Page 1 of 1

simple PHP help **newbie**

Posted: Sun Apr 02, 2006 9:02 am
by Adamck
Hi,
Ive writen a short php...

Code: Select all

newsText=
<?php
$db = mysql_connect("localhost", "***********", "******") or die(mysql_error());
mysql_select_db("adamck_*******") or die(mysql_error());
$que = "SELECT * FROM phpbb_notes";
$res = mysql_query($que);

if (!$res)
  die(mysql_query());
  
while ($row = mysql_fetch_assoc($res)) {
	echo stripslashes($row["text"]);
}

?>
This then outputs (below) to show the vars "newsText=" and some text from mySQL.

Code: Select all

newsText= Latest news Apr 06 Latest new will be in this section of the website and will be displayed dynamically! loads of text here loads of text here loads of text here loads of text here loads of text here loads of text here loads of text here loads of text here loads of text here loads of text here loads of text here
Thats all great BUT....
I need another php script to shorten it down so that it only retrieves about 6 words then adds "....." after it. for a snippet of news that is displayed in another area.
So basically i need to end up with...

Code: Select all

newsText=Latest news Apr 06 Latest new....
Any help is much appreciated!
Ad,

Posted: Sun Apr 02, 2006 9:42 am
by RobertGonzalez
You can do two things...

1. Use the LEFT() MySQL function and tell the DB to only get the first few characters... or
2. Use PHP functions strstr(), str_replace() and strlen() to count the string and replace a certain amount of charachters with '...'.

Posted: Sun Apr 02, 2006 10:53 am
by feyd
Take a look through the Useful Posts thread.