...

View Full Version : Less than or Equal to



brianjamesward
05-31-2011, 02:10 PM
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 ?

kbluhm
05-31-2011, 02:17 PM
Well first off, you cannot have two consecutive else{} blocks.


if ( '' == $note )
{
// empty! do not allow...
}

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

else
{
// correct length! allow...
}

tomharto
05-31-2011, 02:18 PM
Have you tried putting the strlen into a variable and echoing it to make sure its counting the right number of characters?

shadowmaniac
05-31-2011, 02:19 PM
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

Fou-Lu
05-31-2011, 02:24 PM
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.


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.

brianjamesward
05-31-2011, 02:26 PM
Well first off, you cannot have two consecutive else{} blocks.


if ( '' == $note )
{
// empty! do not allow...
}

elseif ( isset( $note[ 160 ] ) )
{
// 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?

brianjamesward
05-31-2011, 02:30 PM
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.


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 !!

Fou-Lu
05-31-2011, 02:31 PM
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.

brianjamesward
05-31-2011, 02:34 PM
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!

kbluhm
05-31-2011, 02:54 PM
And what Kbluhm said; didn't go past 1st line of your code. :P

Yeah... missed the first line. Whoops.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum