ow-design Posted December 22, 2008 Share Posted December 22, 2008 Hi everybody i am a little stuck at the moment, I am creating a site that needs to display on the home page a shortened title of a news story that a user can click to get full story. I have the shortening code: <?php function ShortenText($text) { // Change to the number of characters you want to display $chars = 80; $text = $text."database row would go here"; $text = substr($text,0,$chars); $text = substr($text,0,strrpos($text,' ')); $text = $text."..."; return $text; } ?><p></p> <?php echo ShortenText($text); ?> But i need this code to bring up the newest 3 rows of a database table that holds the news stories and display the shortened content. Any ideas? Thank You Quote Link to comment https://forums.phpfreaks.com/topic/138049-shorten-and-dispaly-text-from-database-row/ Share on other sites More sharing options...
wildteen88 Posted December 22, 2008 Share Posted December 22, 2008 To get the latestest 3 rows from the database you'll apply LIMIT 3 to your query, eg function ShortenText($text, $chars=80) { $text = substr($text,0,$chars); $text = substr($text,0,strrpos($text,' ')); $text .= '...'; return $text; } // get the latest 3 news feeds $sql = 'SELECT news_title_field from news_table ORDER by news_id_field DESC LIMIT 3'; $result = mysql_query($sql); // display news while($row = mysql_num_assoc($result)) { // display shortened news title, returns the first 30 chars. echo shortText($row['news_title_field'], 30); } NOTE: I made $chars an optional parameter for your function. Quote Link to comment https://forums.phpfreaks.com/topic/138049-shorten-and-dispaly-text-from-database-row/#findComment-721535 Share on other sites More sharing options...
ow-design Posted December 22, 2008 Author Share Posted December 22, 2008 <?php $con = mysql_connect("localhost","user","pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } ?> <?php function ShortenText($text, $chars=80) { $text = substr($text,0,$chars); $text = substr($text,0,strrpos($text,' ')); $text .= '...'; return $text; } // get the latest 3 news feeds $sql = 'SELECT title from table ORDER by id LIMIT 3'; $result = mysql_query($sql); // display news while($row = mysql_num_rows($result)) { // display shortened title, returns the first 30 chars. echo shortText($row['title'], 80); } ?> however the following is displayed now Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/deswo/public_html/clients/baseyouthproject/shorten_a_text_string1.php on line 27 line 27 is while($row = mysql_num_rows($result)) please help Thank You Quote Link to comment https://forums.phpfreaks.com/topic/138049-shorten-and-dispaly-text-from-database-row/#findComment-721542 Share on other sites More sharing options...
premiso Posted December 22, 2008 Share Posted December 22, 2008 Did you change the column and table name to be what they should be? If you do not have a table named "table" and a column inside table named "title" that will not work. Quote Link to comment https://forums.phpfreaks.com/topic/138049-shorten-and-dispaly-text-from-database-row/#findComment-721545 Share on other sites More sharing options...
ow-design Posted December 22, 2008 Author Share Posted December 22, 2008 the word table isnt in there?? and yes the column i am pulling up is called title Quote Link to comment https://forums.phpfreaks.com/topic/138049-shorten-and-dispaly-text-from-database-row/#findComment-721546 Share on other sites More sharing options...
wildteen88 Posted December 22, 2008 Share Posted December 22, 2008 Sorry, line 27 should of been while($row = mysql_fetch_assoc($result)) EDIT: you query should be // get the latest 3 news feeds $sql = 'SELECT title from table ORDER by id DESC LIMIT 3'; Quote Link to comment https://forums.phpfreaks.com/topic/138049-shorten-and-dispaly-text-from-database-row/#findComment-721562 Share on other sites More sharing options...
ow-design Posted December 22, 2008 Author Share Posted December 22, 2008 No it is still giving the message Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/deswo/public_html/clients/baseyouthproject/shorten_a_text_string1.php on line 27 Any ideas?? Sorry. Quote Link to comment https://forums.phpfreaks.com/topic/138049-shorten-and-dispaly-text-from-database-row/#findComment-721581 Share on other sites More sharing options...
ow-design Posted December 22, 2008 Author Share Posted December 22, 2008 any ideas Quote Link to comment https://forums.phpfreaks.com/topic/138049-shorten-and-dispaly-text-from-database-row/#findComment-721661 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.