Jump to content


Photo

Selecting a string of text with maximum characters


  • Please log in to reply
11 replies to this topic

#1 scraptoft

scraptoft
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 24 March 2006 - 10:09 PM

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:
Title
Category
Date
This is an example article and after a certain amount of chracters...Click here to read more.
<?php
include('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 table
echo ("<table width = '100%' border = '0' cellspacing = '0' cellpadding = '0'>");
//loop through rows in the database
while ($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 table
echo "</table>";
echo $rows
?>


#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 24 March 2006 - 10:16 PM


The code

<?php

$var = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // set variable

$varlength = strlen($var); // count number of characters

$limit = 10; // set character limit

if ($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

?>


Below i trie to put it alltogether.
BACKUP YOUR DATA AS I AM ONLY A LEARNER.


<?php

include('db.php');


$query = "SELECT * FROM article ORDER BY id";
//execute the query
$result = mysql_query($query);
//start creating our table
echo ("<table width = '100%' border = '0' cellspacing = '0' cellpadding = '0'>");
//loop through rows in the database
while ($rows = mysql_fetch_row($result))
{

$var = ('$description'); // set variable

$varlength = strlen($var); // count number of characters

$limit = 200; // set character limit

if ($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>");
}

?>



moreinfo.php

<?php
include('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 table
echo ("<table width = '100%' border = '0' cellspacing = '0' cellpadding = '0'>");
//loop through rows in the database
while ($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 table
echo "</table>";
echo $rows
?>

I had a go hope it works nearly there i hope lol

good luck

BACKUP YOUR DATA AS I AM ONLY A LEARNER.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 mlin

mlin
  • Members
  • PipPipPip
  • Advanced Member
  • 91 posts

Posted 24 March 2006 - 10:53 PM

use substr()

#4 scraptoft

scraptoft
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 25 March 2006 - 04:33 PM

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:
<?php

include('db.php');


$query = "SELECT * FROM article ORDER BY id DESC";
//execute the query
$result = mysql_query($query);
//start creating our table
echo ("<table width = '100%' border = '0' cellspacing = '0' cellpadding = '0'>");
//loop through rows in the database
while ($rows = mysql_fetch_row($result))

$var = ('$rows[4]'); // set variable

$varlength = strlen($var); // count number of characters

$limit = 200; // set character limit

if ($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>");
}

?>
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

#5 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 25 March 2006 - 05:12 PM

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


<?php


include('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>";

}
}
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#6 scraptoft

scraptoft
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 25 March 2006 - 05:56 PM

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.

#7 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 25 March 2006 - 05:58 PM

[!--quoteo(post=358289:date=Mar 25 2006, 05:46 PM:name=scraptoft)--][div class=\'quotetop\']QUOTE(scraptoft @ Mar 25 2006, 05:46 PM) View Post[/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:

$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;

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 helps
Cheers
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#8 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 25 March 2006 - 06:09 PM

Code tested ok.

worked on the code and should work does here please tell me ok.

<?

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>";

}
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#9 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 25 March 2006 - 06:20 PM

I wrote this for you, does it work on your computer or server does here.

<?

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>";

}
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#10 scraptoft

scraptoft
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 25 March 2006 - 06:47 PM

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!

#11 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 25 March 2006 - 07:43 PM

[!--quoteo(post=358329:date=Mar 25 2006, 06:47 PM:name=scraptoft)--][div class=\'quotetop\']QUOTE(scraptoft @ Mar 25 2006, 06:47 PM) View Post[/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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#12 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 25 March 2006 - 11:43 PM

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.

<?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);



?>

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users