Another PHP Question

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

I have a field in a form called description. Description can be 1000 characters long.

I have a short description page that lists all the listings with some little details. I want this page to have the description but only 100 characters of it. Can you help me out here?

In my language it would look something like this

<?
echo '{DESCRIPTION}' display '100' characters
?>

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Try something like this:

<?php
if(strlen($_POST['description']) > 100) {
   
$text = substr($_POST['description'], 0, 100);
  }
else {
   
$text = $_POST['description'];
  }
?>

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

If you are not needing the full description on the page at all (ie. you click a link to go to full details on another page) then I would recommend only retrieiving partial info from the database to begin with.
SELECT id,LEFT(description,100) AS shortdesc FROM tablename'
(see http://dev.mysql.com/doc/refman/4.1/en/string-functions.html)

Then assuming you get the results back into an array called row then you would have row['id'] and row['shortdesc']

This reduces the amount of information being returned from teh DB server back to PHP. If you have most fields over 100 characters, this will reduce your overhead.

-Greg

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

Thanks you guys. I am using Renegade's just because it works without too many modifications. I am guessing I have to do something in MySQL with yours Greg.

Thanks Laughing out loud

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.