simple PHP help **newbie**

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
Adamck
Forum Newbie
Posts: 9
Joined: Sun Apr 02, 2006 9:00 am

simple PHP help **newbie**

Post 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,
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

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

Post by feyd »

Take a look through the Useful Posts thread.
Post Reply