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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post

    limit characters

    how do i limit the number of characters displayed to 25 characters then display a '...' using the printf or sprintf function ?

  • #2
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    621
    Thanks
    0
    Thanked 6 Times in 6 Posts

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not sure this would work at all with printf(). AFAIK, you can't specify a maximum length of the string displayed that cuts it's end off. I'm happy if someone else knows how to do it though.

    But celtboy gave you enough alternatives, so take your pick.
    De gustibus non est disputandum.

  • #4
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    how come the following shows up blank? it is supposed to show the first 30 characters of the database....

    PHP Code:
    <?php
        $aa 
    mysql_connect("localhost""""");
          
    $db mysql_select_db("whmx_store",$aa);
          
    $sql_select "SELECT * FROM cds top1 ORDER BY dateofentry DESC LIMIT 1";
          
    $result mysql_query($sql_select) or die ("Queryproblem: " mysql_error());
                  while (
    $row mysql_fetch_array($result)) {
    substr("<font face='arial' size='1'><a href='$row[4]'>$row[1] - $row[0]</a></font>"030);
    }
    ?>

  • #5
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    I would've thought it would be from this line

    $sql_select = "SELECT * FROM cds top1 ORDER BY dateofentry DESC LIMIT 1";

    I didn't think you could search two tables at the same time like that within the same query

  • #6
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    no... top1 makes it so that it selects the newest-entered entries in the db.... the table is cds and it works if i just say print(string) but when i go to substr() it doesnt

  • #7
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    maybe i'm just stupid, but i don't see any printor echo before the substr()

  • #8
    me'
    me' is offline
    Senior Coder
    Join Date
    Nov 2002
    Location
    Warwickshire, England
    Posts
    1,229
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by boeing747fp
    no... top1 makes it so that it selects the newest-entered entries in the db...
    If my limited understanding of MySQL is correct, then:

    No it doesn't. ORDER BY dateofentry DESC LIMIT 1 selects the most recent one, not top1.
    David House - Perfect is achieved, not when there is nothing left to add, but when there is nothing left to take away. (Antoine de St. Exupery).
    W3Schools | XHTML Validator | CSS Validator | Colours | Typography | HTML&CSS FAQ | Go get Mozilla Now | I blog!

  • #9
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    That's what me and the mysql docs think too

  • #10
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    me' is correct. TOP1 is the JetSQL-version of LIMIT1

    (well, it's his buggy little brother since it doesn't make a correct cutoff when there are similar values)
    Your query will definitely produce an error and there wount be a recordset.

    the
    while ($row = mysql_fetch_array($result)) {

    is also unnesecary since the recordset will contain maximaly 1 record.
    you should also check the numer of returned rows, before processing the recordset.
    PHP Code:
    if (mysql_num_rows($result)== 0){
        
    $echo ('<strong>No rows found.</strong>') ;
    } else {
        
    $row mysql_fetch_array($result) ;
        
    mysql_free_result($result);
        ...

    Then about the substring: i can't imagen that the result of

    substr("<font face='arial' size='1'><a href='$row[4]'>$row[1] - $row[0]</a></font>", 0, 30);

    could be of any use. I mean, you don't know what the value of the recordsetvariable will be so you don't know h cutoff, and besides, it's hard to read like that + the result of you function isn't printed, not saved in a variable, so nothing will be visible.
    and it will generate invalid html-code...

  • #11
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    how come this doesnt work? it just shows nothing on the page and screws up the page arrangement.
    -------------
    PHP Code:
    <?php
                            $aa 
    mysql_connect("localhost""""");
          
    $db mysql_select_db("whmx_store",$aa);
          
    $sql_select "SELECT * FROM cds ORDER BY dateofentry DESC LIMIT 5";
          
    $result mysql_query($sql_select) or die ("Queryproblem: " mysql_error());
              while (
    $row mysql_fetch_array($result)) {        
                        
    $abcd "<a href='$row[4]'><img src='$row[2]' width='15' height='15' border='0'></a> - <font face='arial' size='1'><a href='$row[4]'>$row[1] - $row[0]</a></font>";
                        print 
    substr($abcd030);
                        print 
    "...<br>";
                        }
                            
    ?>

  • #12
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    try looking at your pages source in the browser. Micht be that yu have something like

    <a href='http://www.retgyhf.co

    inthere (the first 30 positions due to your mysterious substr feature that will never generate whatever you're trying to get) which obviously wount generate any visible output (read my previous posts)

    Maybe try it first without the substr()
    Last edited by raf; 12-24-2003 at 12:43 AM.

  • #13
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    http://store.whmx.com
    ----------------------
    that's the site... and it is right now without the substr and in the section with "Newest Hits" i only want the results to be one line and stop.

  • #14
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I suppose you want only the first 30 visibel characters then.
    So not

    Newsboys - Adoration: Worship
    Album

    but

    Newsboys - Adoration: Worship...


    So then you need to use the substr on the linktext (the part betwee the <a></a> tags.
    so that would be

    $link=($row[1] ." - " . $row[0] );
    print ("<a href='$row[4]'><img src='$row[2]' width='15' height='15' border='0'></a> - <font face='arial' size='1'><a href='$row[4]'>" . substr($link,0,30) . " ...</a></font>");


    This will work, but it's not exactly clean, because it will chop of at position 30, but it would be nicer to chop of at the end of a word.
    Last edited by raf; 12-24-2003 at 01:21 AM.

  • #15
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    thanks... that works


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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