dazzclub Posted March 25, 2009 Share Posted March 25, 2009 Hi guys i trying to set a limit to how many characters i want to displayed for my news headline, but i think im using the wordwrap function wrongly. I'm applying it to the News row Here is my entire code; function getHeadlines() { global $dbc; $query = "SELECT NewsID, NewsDate, NewsTitle, News FROM news ORDER BY NewsDate"; $result = mysqli_query ($dbc, $query)or die(mysqli_error() . "<p>With query:<br>$query"); while ($row = mysqli_fetch_array($result)) { //need to format the date on this function //get NewsID echo '<strong class="news_header"><a href="news_article.php?news_article='.$row['NewsID'].' title="erer">'.$row['NewsTitle'].'</a></strong> <p class="date">'.$row['NewsDate'].'</p> <p class="intro">'.wordwrap($row['News'], '20').'</p> '; } } Any advice would be great Link to comment https://forums.phpfreaks.com/topic/150983-solved-using-the-wordwrap-function/ Share on other sites More sharing options...
trq Posted March 25, 2009 Share Posted March 25, 2009 but i think im using the wordwrap function wrongly. What makes you think that? What results are you expecting? What results are you getting? Link to comment https://forums.phpfreaks.com/topic/150983-solved-using-the-wordwrap-function/#findComment-793219 Share on other sites More sharing options...
trq Posted March 25, 2009 Share Posted March 25, 2009 Oh, and ps. the second argument to wordwrap is expected to be an int, not a string. Link to comment https://forums.phpfreaks.com/topic/150983-solved-using-the-wordwrap-function/#findComment-793221 Share on other sites More sharing options...
dazzclub Posted March 25, 2009 Author Share Posted March 25, 2009 Hi thorpe, The row News, is basicly the whole of the news article, so rather than having the whole article displayed i would like to shorted it to a paragraph. So i though using wordwrap i can reduce the amount of characters being displayed. so in my code above, i was hoping the wordwrap would only display 20 characters (just for testing purposes). The reason i think im using it wrong is when i remove the wordwrap function the amount of characters stays the same.. Hope that helps Link to comment https://forums.phpfreaks.com/topic/150983-solved-using-the-wordwrap-function/#findComment-793232 Share on other sites More sharing options...
trq Posted March 25, 2009 Share Posted March 25, 2009 Your looking for substr. Link to comment https://forums.phpfreaks.com/topic/150983-solved-using-the-wordwrap-function/#findComment-793235 Share on other sites More sharing options...
dazzclub Posted March 25, 2009 Author Share Posted March 25, 2009 Hi thorpe, Thanks for that! here is the finished code if anyone else can find it helpful; function getHeadlines() { global $dbc; $query = "SELECT NewsID, NewsDate, NewsTitle, News FROM news ORDER BY NewsDate"; $result = mysqli_query ($dbc, $query)or die(mysqli_error() . "<p>With query:<br>$query"); while ($row = mysqli_fetch_array($result)) { //need to format the date on this function //get NewsID echo '<strong class="news_header"><a href="news_article.php?news_article='.$row['NewsID'].' title="erer">'.$row['NewsTitle'].'</a></strong> <p class="date">'.$row['NewsDate'].'</p> <p class="intro">'.substr($row['News'], '0', '120').'...</p> '; } } Thanks again Link to comment https://forums.phpfreaks.com/topic/150983-solved-using-the-wordwrap-function/#findComment-793239 Share on other sites More sharing options...
trq Posted March 25, 2009 Share Posted March 25, 2009 substr's 2nd and 3rd arguments are also meant to be ints not strings. Whats with that? eg; substr($row['News'], 0, 120) not... substr($row['News'], '0', '120') php is smart enough to figure this out but its inificient programming. Link to comment https://forums.phpfreaks.com/topic/150983-solved-using-the-wordwrap-function/#findComment-793244 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.