Jump to content

Cutoff String


danny232
Go to solution Solved by fastsol,

Recommended Posts

I've got a script running where I submit data into a form and that posts it into the database then posts the information onto a php page.

 

I'm wanting the results returned to cutoff after 20 characters with "...." after it to show there is more (there will be then a 'click here to see more')

 

I've tried searching for this numerous times but the results seem to direct to here: http://php.net/manual/en/function.substr.php

 

I've read through that but that seems to discount everything and displays an error message saying there's too much to display.

 

Can anyone help please?

 

 

Link to comment
Share on other sites

substr() returns a portion of a string. If you post some code and the (entire) error message, we may be able to help. Also, you might post the value being passed IN to the function and the value it RETURNS along with telling us what you EXPECT it to return.
Link to comment
Share on other sites

Thanks for the advice.

 

Here is the code:

<?php
// Make a MySQL Connection
$query = "SELECT * FROM jobs ORDER BY id DESC"; 
	 
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
echo "<strong>";
echo $row['title']. " <br /> </strong>". $row['body'] . " <br /><br />Salary: ". $row['wage'] . " <br />Closing Date: ". $row['expiry'] . " <br /><br />To apply for this position, please email your CV to ". $row['apply'];
echo "<br /><br />";
 }
 
?>
<?php
$charLimit = 25;
$myText = "$body";
$teaserText = substr($myText,0,$charLimit) .  "... ";
echo $teaserText;
?>

I understand this won't work the way I want it to due to the ordering but i've tried altering the code around and I get several errors.

 

Basically I want it to read from the table "jobs" and input the row from the column "body" but only display 30 words then have "..." at the end.

Link to comment
Share on other sites

  • Solution

What errors are you getting exactly.  Based on what you posted, $body is not defined anywhere.  Are you sure it's not supposed to be $row['body'] instead.  Also you say you want the first 30 "words", substr() can't count the words, ony the characters.  If you truly need the words then you'll need to do some more processing to achieve that. Here is a better example I think of your code

<?php
// Make a MySQL Connection
$query = "SELECT * FROM jobs ORDER BY id DESC"; 
	 
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
$charLimit = 25;
$teaserText = substr($row['body'],0,$charLimit) .  "... ";

echo "<strong>";
echo $row['title']. " <br /> </strong>". $teaserText . " <br /><br />Salary: ". $row['wage'] . " <br />Closing Date: ". $row['expiry'] . " <br /><br />To apply for this position, please email your CV to ". $row['apply'];
echo "<br /><br />";
 }
 ?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.