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 10 of 10
  1. #1
    New Coder
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Less than or Equal to

    PHP Code:
             if($note&&$user_id&&$posted)
                 
                 {  

    if(
    strlen($note) <= 160) {

                
    $update mysql_query("INSERT INTO notes (id, note, user_id, posted) VALUES('', '$note', '$user_id', '$posted')") or die("Error Updating Your Note");

                
    session_start();
                
    $_SESSION['post_success'] = 'showing the alert';
                
    header('location: member.php');
                 
                 } else { echo 
    "<div id='alertbar'>
             
                 <div id='alert'>
             
             
                 <p>Your post must be 160 characters or less!</p> 
             
                 </div>
                 </div>"
    ; }
                 
                 } else {
                 
                 echo 
    "<div id='alertbar'>
             
                 <div id='alert'>
             
             
                 <p>You can't make a blank post!</p> 
             
                 </div>
                 </div>"
    ;
                 
                 } 
    I'm having some difficulty with my code here. Basically as you can see i've got a posting script that only allows you to post 160 characters or less. But currently it's allowing me to post less than 160 but not 160 itself.

    I've tried <= but it doesn't seem to be working. Any ideas guys/girls ?
    Last edited by brianjamesward; 05-31-2011 at 03:18 PM.

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Well first off, you cannot have two consecutive else{} blocks.
    PHP Code:
    if ( '' == $note )
    {
        
    // empty! do not allow...
    }

    elseif ( isset( 
    $note160 ] ) )
    {
        
    // too long! do not allow...
    }

    else
    {
        
    // correct length! allow...


  • #3
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    827
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Have you tried putting the strlen into a variable and echoing it to make sure its counting the right number of characters?

  • #4
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    254
    Thanks
    1
    Thanked 63 Times in 63 Posts
    PHP Code:
    echo strlen($note);
    exit; 
    before the "if" statement. Do note that an empty space is counted as a character.

    If you still think there's a mistake, pls provide a sample test case of the data entered for $note.


    Edit:
    And what Kbluhm said; didn't go past 1st line of your code. :P

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Your code is right, except that there is an unseen if in there (dictated by the two level closing before the else). Assuming that there is no problem there, nothing else is wrong.
    That tells me you're either using a cached version of the page, which is quite rare with POST input, or that the length actually exceeds the desired length of 160.
    Print out your strlen or var_dump of $note prior to the if. That will tell you if you are actually above the 160. It won't work properly with multibyte characters.
    Also, move the session_start to the top of the script. Placing it in the middle increases the probability that headers have already been sent and will cause it to fail.

    Edit:
    I think everyone above me missed that other closing brace. There isn't two consecutive else calls, they are two distinct else clauses one of which is for an unseen branch.

  • Users who have thanked Fou-Lu for this post:

    brianjamesward (05-31-2011)

  • #6
    New Coder
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by kbluhm View Post
    Well first off, you cannot have two consecutive else{} blocks.
    PHP Code:
    if ( '' == $note )
    {
        
    // empty! do not allow...
    }

    elseif ( isset( 
    $note160 ] ) )
    {
        
    // too long! do not allow...
    }

    else
    {
        
    // correct length! allow...

    in your elseif (isset... what is the 160 in brackets? will that show anything above that 160 or just exactly 160?

  • #7
    New Coder
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Your code is right, except that there is an unseen if in there (dictated by the two level closing before the else). Assuming that there is no problem there, nothing else is wrong.
    That tells me you're either using a cached version of the page, which is quite rare with POST input, or that the length actually exceeds the desired length of 160.
    Print out your strlen or var_dump of $note prior to the if. That will tell you if you are actually above the 160. It won't work properly with multibyte characters.
    Also, move the session_start to the top of the script. Placing it in the middle increases the probability that headers have already been sent and will cause it to fail.

    Edit:
    I think everyone above me missed that other closing brace. There isn't two consecutive else calls, they are two distinct else clauses one of which is for an unseen branch.
    Thanks man, yeah my code was correct bar the session_start(), once i removed that it was fine! thanks a lot !!

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by brianjamesward View Post
    in your elseif (isset... what is the 160 in brackets? will that show anything above that 160 or just exactly 160?
    PHP is a string based language, but does allow the use of its string as char[]. Because these are char arrays, what is happening is that a check is being done to see if a character exists within the 160th offset of the string, which is character #161. If there is, that means the length has exceeded.

  • Users who have thanked Fou-Lu for this post:

    brianjamesward (05-31-2011)

  • #9
    New Coder
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    PHP is a string based language, but does allow the use of its string as char[]. Because these are char arrays, what is happening is that a check is being done to see if a character exists within the 160th offset of the string, which is character #161. If there is, that means the length has exceeded.
    I see, i understand now. Thanks!

  • #10
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by shadowmaniac View Post
    And what Kbluhm said; didn't go past 1st line of your code. :P
    Yeah... missed the first line. Whoops.


  •  

    Posting Permissions

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