Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Truncating the value of an array in PHP5

    Hi i'm fairly new to PHP and I am slowly trying to contruct a guestbook. My end aim is to have a simple guestbook that runs off MySQL, while also on the front page having some sort of small "Flash Box" that shows the first 3 or so entries truncated down to 100 characters or so.

    Currently im working off a tutorial that had me create a jokes database. I can get the jokes within the database to show up fine. What i want to do now is is to truncate them down. I currently have..


    Code:
    <?php
    
    //Checks to see If a connection can be made to the MySQL server and displays the appropriate message
    
    $connect = @mysql_connect('localhost', 'root', '*******');
    
    if (!$connect) {
       exit ('Wont Connect to MySQL Server');
    } else {
       echo 'Connected to my SQL Server<br><br><hr><br>';
    }
    
    
    
    //Checks to see if a connection can be made to the database IJDB and displays appropriate message
    
    mysql_select_db('ijdb', $connect);
          if (!@mysql_select_db('ijdb')) {
             exit ('Wont Connect to Joke Database');
    } else {
             echo 'Connected to my Joke Database<br><br><hr><br>';
    }
    ?>
    <?php
    
    //Retrieves Jokes and loads them into array
    
    $result = @mysql_query('SELECT joketext, jokedate FROM joke');
    
    //Displays the loaded jokes
    
    while ($row = mysql_fetch_array($result)) {
       echo $row['joketext'].'<br>';   
       echo $row['jokedate']. '<br><br>';
       }
    ?>
    This works fine and shows the jokes as expected. I've been working with the following function, and while i don't understand exactly how it works, I understand what it does.

    Code:
    <?php
     
    function truncate($string, $del) {
      $len = strlen($string);
      if ($len > $del) {
        $new = substr($string,0,$del)."...";
        return $new;
      }
      else return $string;
    }
    ?>
    
    <?php
     
    $arg = "Hello World. How are you?";
     
    echo $arg."<br>";
    echo truncate($arg, 6)."<br>";
    echo truncate($arg, 7)."<br>";
     
    ?>
    I can't figure out how to integrate it into
    Code:
    echo $row['joketext'].'<br>';
    I've tried various things, but I think im having problems as that is based on a single variable and I want to use it against ALL jokes that come out of the joketext field.

    Any help would be appreciated.

    Thanks

    Richard

  • #2
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    Code:
    while ($row = mysql_fetch_array($result)) {
       echo truncate($row['joketext'], 100) .'<br>';   
       echo $row['jokedate']. '<br><br>';
       }
    John

  • #3
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    PHP Code:
    <?php


    function truncate($string$del) {
      
    $len strlen($string);
      if (
    $len $del) {
        
    $new substr($string,0,$del)."...";
        return 
    $new;
      }
      else return 
    $string;
    }


    //Checks to see If a connection can be made to the MySQL server and displays the appropriate message

    $connect = @mysql_connect('localhost''root''*******');

    if (!
    $connect) {
       exit (
    'Wont Connect to MySQL Server');
    } else {
       echo 
    'Connected to my SQL Server<br><br><hr><br>';
    }



    //Checks to see if a connection can be made to the database IJDB and displays appropriate message

    mysql_select_db('ijdb'$connect);
          if (!@
    mysql_select_db('ijdb')) {
             exit (
    'Wont Connect to Joke Database');
    } else {
             echo 
    'Connected to my Joke Database<br><br><hr><br>';
    }

    //Retrieves Jokes and loads them into array

    $result = @mysql_query('SELECT joketext, jokedate FROM joke');

    //Displays the loaded jokes

    while ($row mysql_fetch_array($result)) {
       echo 
    truncate($row['joketext'],6).'<br>';   
       echo 
    $row['jokedate']. '<br><br>';
       }
    ?>
    Something like this?
    You can not say you know how to do something, until you can teach it to someone else.

  • Users who have thanked timgolding for this post:

    Rithotyn (03-21-2009)

  • #4
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    Did you try

    PHP Code:
    while ($row mysql_fetch_array($result)) {
       echo 
    truncate($row['joketext'], 6). '<br>';   
       echo 
    truncate($row['jokedate'], 6). '<br><br>';

    This should work if you have your truncate() function in the same file or included from another file.

  • Users who have thanked steelaz for this post:

    Rithotyn (03-21-2009)

  • #5
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Worked perfectly. Many thanks for your help.



  •  

    Posting Permissions

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