2

I'm having this problem where my PHP code is concatenating instead of adding

$offset=$_POST['offset']; //Get the offset
$searchLimit = 10;
$searchCount = count(sql) //For the purpose of this question, it returns the result count

Now I want to calculate the 'from' display for pagination, so I do

$from = ($offset*$searchLimit)+1;

It works fine when

$offset == 0

I get the expected result which is 1. But when

$offset == 1

It gives me 101. Basically it is concatenating the 10 and 1 to give me 101. I've tried the following

$from = (int)($offset*$searchLimit)+1
$from = ((int)($offset)*$searchLimit)+1
$from = (((int)($offset)*$searchLimit)+1)

I even tried

$offset = (int)$_POST['offset'];

But all of them are giving the same result.

8
  • 2
    Have you looked at PHP's intval function? php.net/manual/en/function.intval.php Commented Jan 6, 2013 at 1:53
  • I have .. Actually tried it before writing this comment .. So this is what I tried ... $from = (intval($offset*$searchLimit)+1) but that doesn't help either .. Commented Jan 6, 2013 at 2:30
  • I would try $offset = intval($_POST['offset']); instead Commented Jan 6, 2013 at 3:14
  • How is $from = ($offset*$searchLimit)+1; 'working fine' if it returns 0 as stated? (0*10)+1 == 1 surely? Commented Jan 6, 2013 at 3:52
  • Sorry .. Once again that was my mistake in asking the question .. It is returning 1 which is the expected result .. I will correct the same in the question .. I will also try putting the intval around $_POST['offset'] .. I had initially tried $offset = (int)$_POST['offset'] but that didn't do anything different .. Commented Jan 6, 2013 at 4:37

2 Answers 2

4

You are missing a $ before searchLimit. As a result, it is being treated as a string. This result in unexpected behaviour.

Sign up to request clarification or add additional context in comments.

2 Comments

A dollar sign might also be missing before sql. If you had enabled PHP's display_errors setting and set error_reporting to include E_NOTICE, you would have quickly discovered this problem.
Sorry .. The missing '$' is a typo .. I do have it in my code .. The sql line is just a pseudo-line. It is not the actual LOC in the application. It is the line that returns the count of the search result.
1

You missed a $ sign before searchLimit (and perhaps before sql). -_-

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.