scraptoft Posted March 24, 2006 Share Posted March 24, 2006 Hi phpfreaks,I have 4 rows I want to output from a mysql database. The fourth row is the article that will also be shown on a seperate article.php page. What I would like to do is instead of output the whole article text, to have a cut-off point after a certain amount of characters with a 'click here to read more' link.For example:TitleCategoryDateThis is an example article and after a certain amount of chracters...Click here to read more.[code]<?phpinclude('db.php');//this is our SQL query , note we are ordering by the submitted date//and setting a limit of ten items$query = "SELECT * FROM article ORDER BY id DESC LIMIT 1000";//execute the query$result = mysql_query($query);//start creating our tableecho ("<table width = '100%' border = '0' cellspacing = '0' cellpadding = '0'>");//loop through rows in the databasewhile ($rows = mysql_fetch_row($result)){//in the first row we display the title ($rows[1]) and the author $rows[3]echo (" <tr><td> <h1><a href= ' article.php?title=$rows[1] ' >$rows[1]</a></h1></td></tr> ");//in the next row we display the description which is $rows[2]echo (" <tr><td class= 'url' ><a href= ' http://domain.com ' >domain.Com</a>/<a href= ' category.php?category=$rows[2] ' >$rows[2]</a></td> </tr>");echo (" <tr><td class= 'date' >$rows[3]</td> </tr>");echo (" <tr><td class= 'description' >$rows[4]</td> </tr>");}//finish or tableecho "</table>";echo $rows?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5722-selecting-a-string-of-text-with-maximum-characters/ Share on other sites More sharing options...
redarrow Posted March 24, 2006 Share Posted March 24, 2006 The code[code]<?php$var = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // set variable$varlength = strlen($var); // count number of characters$limit = 10; // set character limitif ($varlength > $limit) { // if character number if more than character limit$var = substr($var,0,$limit) . "..."; // display string up to character limit, add dots}echo $var; // display variable?>[/code]Below i trie to put it alltogether.BACKUP YOUR DATA AS I AM ONLY A LEARNER.[code]<?phpinclude('db.php');$query = "SELECT * FROM article ORDER BY id";//execute the query$result = mysql_query($query);//start creating our tableecho ("<table width = '100%' border = '0' cellspacing = '0' cellpadding = '0'>");//loop through rows in the databasewhile ($rows = mysql_fetch_row($result)){$var = ('$description'); // set variable$varlength = strlen($var); // count number of characters$limit = 200; // set character limitif ($varlength > $limit) { // if character number if more than character limit$var = substr($var,0,$limit) . "..."; // display string up to character limit, add dots}echo (" <tr><td> <h1><a href= ' article.php?title=$rows[1] ' >$rows[1]</a></h1></td></tr> ");//in the next row we display the description which is $rows[2]echo (" <tr><td class= 'url' ><a href= ' http://domain.com ' >domain.Com</a>/<a href= ' category.php?category=$rows[2] ' >$rows[2]</a></td> </tr>");echo (" <tr><td class= 'date' >$rows[3]</td> </tr>");echo ("<tr><td class= 'description' >$rows[4]</td> </tr>");echo ("<tr><td><a href='http://moreinfo.php'>Read more</a></td> </tr>");}?>[/code]moreinfo.php[code]<?phpinclude('db.php');//this is our SQL query , note we are ordering by the submitted date//and setting a limit of ten items$query = "SELECT * FROM article ORDER BY id DESC LIMIT 1000";//execute the query$result = mysql_query($query);//start creating our tableecho ("<table width = '100%' border = '0' cellspacing = '0' cellpadding = '0'>");//loop through rows in the databasewhile ($rows = mysql_fetch_row($result)){//in the first row we display the title ($rows[1]) and the author $rows[3]echo (" <tr><td> <h1><a href= ' article.php?title=$rows[1] ' >$rows[1]</a></h1></td></tr> ");//in the next row we display the description which is $rows[2]echo (" <tr><td class= 'url' ><a href= ' http://domain.com ' >domain.Com</a>/<a href= ' category.php?category=$rows[2] ' >$rows[2]</a></td> </tr>");echo (" <tr><td class= 'date' >$rows[3]</td> </tr>");echo (" <tr><td class= 'description' >$rows[4]</td> </tr>");}//finish or tableecho "</table>";echo $rows?>[/code]I had a go hope it works nearly there i hope lolgood luckBACKUP YOUR DATA AS I AM ONLY A LEARNER. Quote Link to comment https://forums.phpfreaks.com/topic/5722-selecting-a-string-of-text-with-maximum-characters/#findComment-20407 Share on other sites More sharing options...
mlin Posted March 24, 2006 Share Posted March 24, 2006 use substr() Quote Link to comment https://forums.phpfreaks.com/topic/5722-selecting-a-string-of-text-with-maximum-characters/#findComment-20415 Share on other sites More sharing options...
scraptoft Posted March 25, 2006 Author Share Posted March 25, 2006 Redarrow, thankyou for the time you spent helping me it is very much appreciated. I have been playing around with the extra code you wrote for some hours, however it seams to be correct but it isn't shortening the text after 200 chars.I think the problem could be that I am not setting the $var variable correctly, could it be because i'm out putting $rows instead?Heres my current code:[code]<?phpinclude('db.php');$query = "SELECT * FROM article ORDER BY id DESC";//execute the query$result = mysql_query($query);//start creating our tableecho ("<table width = '100%' border = '0' cellspacing = '0' cellpadding = '0'>");//loop through rows in the databasewhile ($rows = mysql_fetch_row($result))$var = ('$rows[4]'); // set variable$varlength = strlen($var); // count number of characters$limit = 200; // set character limitif ($varlength > $limit) { // if character number if more than character limit$var = substr($var,0,$limit) . "..."; // display string up to character limit, add dots}echo (" <tr><td> <h1><a href= ' article.php?title=$rows[1] ' >$rows[1]</a></h1></td></tr> ");//in the next row we display the description which is $rows[2]echo (" <tr><td class= 'url' ><a href= ' http://domain.com ' >domain.Com</a>/<a href= ' category.php?category=$rows[2] ' >$rows[2]</a></td> </tr>");echo (" <tr><td class= 'date' >$rows[3]</td> </tr>");echo ("<tr><td class= 'description' >$rows[4]</td> </tr>");echo ("<tr><td><a href='http://moreinfo.php'>$text</a></td> </tr>");}?>[/code]I hope you or any other php genius can shed some light on this, thanks.I forgot to mention that the field I am trying to output is 'text' (which is $row[4]). I don't know if this helps you anymore or not.cheers Quote Link to comment https://forums.phpfreaks.com/topic/5722-selecting-a-string-of-text-with-maximum-characters/#findComment-20573 Share on other sites More sharing options...
redarrow Posted March 25, 2006 Share Posted March 25, 2006 Backup your files and try this tell me what happens please, Not at home at the moment sorry code not tested.the code should output the row[4] and show all text only 200 chr[code]<?phpinclude('db.php');$query = "SELECT * FROM article ORDER BY id DESC";$result = mysql_query($query);echo "<table width = '100%' border = '0' cellspacing = '0' cellpadding = '0'>";while ($rows = mysql_fetch_row($result)){$var = ('$rows[4]');$varlength = strlen($var); $limit = 200; if ($varlength > $limit) { $var = substr($var,0,$limit) . "..."; echo "This is 200 words<br><tr><td class= 'description' >'.$rows[4].'</td> </tr><br>";}}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5722-selecting-a-string-of-text-with-maximum-characters/#findComment-20598 Share on other sites More sharing options...
scraptoft Posted March 25, 2006 Author Share Posted March 25, 2006 Just tested it, unfortunatly it didn't ouput anything; just a blank page.I have just been reading about parentheses, could it possibly have anything to do with this? Instead of using echo() use print()?Any more comments are very very welcome. Quote Link to comment https://forums.phpfreaks.com/topic/5722-selecting-a-string-of-text-with-maximum-characters/#findComment-20617 Share on other sites More sharing options...
redbullmarky Posted March 25, 2006 Share Posted March 25, 2006 [!--quoteo(post=358289:date=Mar 25 2006, 05:46 PM:name=scraptoft)--][div class=\'quotetop\']QUOTE(scraptoft @ Mar 25 2006, 05:46 PM) [snapback]358289[/snapback][/div][div class=\'quotemain\'][!--quotec--]Just tested it, unfortunatly it didn't ouput anything; just a blank page.[/quote]possibly an error, but if your error_reporting is off, you wont see it.either way, mlin pretty much summed it up, and redarrow is pretty much spot on:[code]$article = article text goes here$max_len = 200;$article_len = strlen($article);if ($article_len>$max_len){ $article = substr($article,0,$max_len)."..."; $article.= "<a href='pathtoarticle.php'>[more]</a>";}echo $article;[/code]i've not written it around your code, but it'll give you the right idea to get it working yourself based on what you have already.Hope it helpsCheers Quote Link to comment https://forums.phpfreaks.com/topic/5722-selecting-a-string-of-text-with-maximum-characters/#findComment-20624 Share on other sites More sharing options...
redarrow Posted March 25, 2006 Share Posted March 25, 2006 Code tested ok.worked on the code and should work does here please tell me ok.[code]<?include('db.php');$tmp=mysql_query("SELECT * FROM article ORDER BY id"); while($row=mysql_fetch_array($tmp)){ $title = $row['title'];$category = $row['category'];$date = $row['date'];$description = $row['description'];$var = ("$description");$varlength = strlen($var); $limit = 200; if ($varlength > $limit) { $var = substr($var,0,$limit);}echo "<table width = '100%' border = '0' cellspacing = '0' cellpadding = '0'>";echo " <tr><td> <h1><a href= ' article.php?title=$title ' >$title</a></h1></td></tr> ";echo " <tr><td class= 'url' ><a href= ' http://domain.com ' >domain.Com</a>/<a href= ' category.php?category=$category ' >$category</a></td> </tr>";echo " <tr><td class= 'date' >$date</td> </tr>";echo "<tr><td class= 'description' >$var</td> </tr><tr><td><a href='http://moreinfo.php'>Read more</a></td> </tr>";echo"</table><br>";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5722-selecting-a-string-of-text-with-maximum-characters/#findComment-20631 Share on other sites More sharing options...
redarrow Posted March 25, 2006 Share Posted March 25, 2006 I wrote this for you, does it work on your computer or server does here.[code]<?include('db.php');$tmp=mysql_query("SELECT * FROM article ORDER BY id"); while($row=mysql_fetch_array($tmp)){ $title = $row['title']; // row[1]$category = $row['category']; //row[2]$date = $row['date'];// row[3]$text = $row['text']; //row[4]$var = ("$text");$varlength = strlen($var); $limit = 200; if ($varlength > $limit) { $var = substr($var,0,$limit);}echo "<table width = '100%' border = '0' cellspacing = '0' cellpadding = '0'>";echo " <tr><td> <h1><a href= ' article.php?title=$title ' >$title</a></h1></td></tr> ";echo " <tr><td class= 'url' ><a href= ' http://domain.com ' >domain.Com</a>/<a href= ' category.php?category=$category ' >$category</a></td> </tr>";echo " <tr><td class= 'date' >$date</td> </tr>";echo "<tr><td class= 'description' >$var</td> </tr><tr><td><a href='http://moreinfo.php'>Read more</a></td> </tr>";echo"</table><br>";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5722-selecting-a-string-of-text-with-maximum-characters/#findComment-20640 Share on other sites More sharing options...
scraptoft Posted March 25, 2006 Author Share Posted March 25, 2006 Mate it works like an absolute dream, thankyou so much for the time you have spent on this. I'm so happy you converted my code too (well kinda, more like changed) from the $rows[number] to $row[name of field] I understand it a whole lot better now.Cheers again! Quote Link to comment https://forums.phpfreaks.com/topic/5722-selecting-a-string-of-text-with-maximum-characters/#findComment-20656 Share on other sites More sharing options...
redarrow Posted March 25, 2006 Share Posted March 25, 2006 [!--quoteo(post=358329:date=Mar 25 2006, 06:47 PM:name=scraptoft)--][div class=\'quotetop\']QUOTE(scraptoft @ Mar 25 2006, 06:47 PM) [snapback]358329[/snapback][/div][div class=\'quotemain\'][!--quotec--]Mate it works like an absolute dream, thankyou so much for the time you have spent on this. I'm so happy you converted my code too (well kinda, more like changed) from the $rows[number] to $row[name of field] I understand it a whole lot better now.Cheers again![/quote]Good hope you enjoy good luck. Quote Link to comment https://forums.phpfreaks.com/topic/5722-selecting-a-string-of-text-with-maximum-characters/#findComment-20671 Share on other sites More sharing options...
litebearer Posted March 25, 2006 Share Posted March 25, 2006 Being a terribly old coot, my current motto is 'a day late and a dollar short'; never-the-less, for what its worth, here is my 2 cents.[code]<?PHP########################################## this function essentially makes # the PHP 5 function str_split # available for earlier versions of PHP#########################################if (!function_exists("str_split")) { function str_split($str,$length = 1) { if ($length < 1) return false; $strlen = strlen($str); $ret = array(); for ($i = 0; $i < $strlen; $i += $length) { $ret[] = substr($str,$i,$length); } return $ret; }}######################################## This little function will take a string, truncate it to a specific # length, make sure it is not truncated in # the middle of a word and add three trailing periods.#######################################Function display_teaser($article,$display_length) { if(strlen($article)>$display_length) { $display_portion = substr($article,0,$display_length); } else { $display_portion = $article; } $true=0; while ($true < 1) { if(substr($display_portion, -1) != " ") { $display_portion = substr($display_portion, 0, -1); }else{ $true = 1; } } $display_portion = $display_portion . "..."; return $display_portion;}$test_article = "The first articles about The Big Lie were not published in French newspapers. When the book began to appear in French bookstores and Thierry Meyssan had not been invited yet to any television program, two newspapers, one in Chile and another in Hungary, talked about his research on 9/11 with interest. Later, French dailies Le Monde and Libération wrote full pages to accuse him after the author’s appearance at the Thierry Ardisson’s show on March 16. The position of the two newspapers that accused him of -lying - and - revisionism - was completely accepted by the French media as a whole. But abroad, countless newspapers highlighted the pertinence of the research.<P>In Argentina, Page 12 explained that Thierry Meyssan rightfully questioned the official version whose contradictions and silences are numerous. In Switzerland, Le Courrier described The Big Lie as a book written in a clear and documented way in which the final result has an absolute coherence and the fabrication is convincingly revealed In China and Russia , the research awoke great interest. In the Balkans, the book was particularly welcomed and its first translation was made into Slovene. On the other hand, the book was published divided into 35 episodes in two Yugoslavian newspapers: Polítika and Draganic.";$test_len = 200;echo display_teaser($test_article,$test_len);?>[/code]Lite... Quote Link to comment https://forums.phpfreaks.com/topic/5722-selecting-a-string-of-text-with-maximum-characters/#findComment-20725 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.