SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to limit characters in a row

    Hey,
    Does anyone know how I can limit the description field on this query to just display 20 characters. I've tried some of the ideas I've seen mentioned such as using substr but it doesn't seem to work for this criteria. Here is the script. If anyone has any ideas what might be the best way to do it I would be really grateful.

    PHP Code:
    $query  "SELECT Product_ID, Genus, Common_name, Description FROM products ORDER BY Product_ID DESC";
    $result mysql_query($query) or die('Error, query failed');
    $numofrows mysql_num_rows($result);

    for(
    $i 0$i $numofrows$i++) {
        
    $row mysql_fetch_array($result); 
        if(
    $i 2) { 
            echo 
    "<tr bgcolor='lightgrey'>";
        } else { 
            echo 
    "<tr bgcolor='white'>";
        }
        echo 
    "<td>".$row['Product_ID']."</td><td>".$row['Genus']."</td><td>".$row['Common_name']."</td><td>".$row['Description']."</td>";
        echo 
    "</tr>";

    Thanks
    Russ

  2. #2
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What issues were you having with substr?

    PHP Code:
    substr($row['Description'], 020
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe it was my placement. I used that exact line, and I've tried it in various places but with no outcome. This is where I think it should go in the document:

    PHP Code:
    for($i 0$i $numofrows$i++) {
        
    $row mysql_fetch_array($result); 
        
    substr($row['Description'], 020); 
    Is that the correct place to put it? If so, what reason could there be for it having no effect?

    Cheers

  4. #4
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It returns the new result, in that case it is returning it nowhere.


    PHP Code:
     // Nowhere!
    substr($row['Description'], 020);

    or

    // To $var
    $var substr($row['Description'], 020);

    or

    // Replacing itself
    $row['Description'] = substr($row['Description'], 020); 

    or 

    // Inline
    echo "blah blah" substr($row['Description'], 020) . "blah blah"
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey thanks for the help! Think I was probably quite fatigued when I was doing this. Thanks for the different suggestions though, really useful!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •