-1

I want to keep one content of the variable when I refresh because it is kind of like a counter and this is the code that I have got using session, but it does not seem to want to work.
And yes, I have looked at other websites for the error, and I have removed all unneccesary white spaces etc.

session_start();
$result = mysql_query('SELECT MIN(ID) AS min, MAX(ID) AS max FROM ytable') or exit(mysql_error()); 
$row = mysql_fetch_assoc($result); 
if($_SESSION['counter'] < $row['max']){
    if (isset($_SESSION['counter'])){
        $counter = $_SESSION['counter']++;

    }else{
            $counter = $_SESSION['counter'] = 0;
        }
 }

Is there something wrong with my code? If not, is there any other way to do the same thing?

Masayuki Tonoki
  • 167
  • 2
  • 13
  • "but it does not seem to want to work" --- rephrase it to carry some technical meaning. We have no idea what "does not want to work" means when you talk about programming language. – zerkms Aug 20 '15 at 01:18
  • Sorry, these two always come up: Warning: session_start() [function.session-start]: Cannot send session cookie and Warning: session_start() [function.session-start]: Cannot send session cache limiter – Masayuki Tonoki Aug 20 '15 at 01:23
  • you can't call session start after you have already sent output the page, if you are echoing anything or spitting out any html, you have to call session_start first. – chiliNUT Aug 20 '15 at 01:27
  • So, why haven't you tried to google about the error message? There are millions of such questions asked and answered – zerkms Aug 20 '15 at 01:29
  • @zerkms Cheers for that(!) Can you read? I clearly said that I have looked it up and it still doesn't work. Please read carefully before making any criticisms. – Masayuki Tonoki Aug 20 '15 at 01:41
  • @Masayuki Tonoki: "I have looked it up and it still doesn't work" --- so have you fixed the problem with headers? If not, why haven't you tried to google about the error message? – zerkms Aug 20 '15 at 01:45
  • `var_dump($_SESSION['counter']);` and this should come first `if (isset($_SESSION['counter'])){$result = mysql_query...} ... }` - if isset.. go on with the rest, and not the reverse – Funk Forty Niner Aug 20 '15 at 01:50
  • @zerkms I have no html headers before thing before the session start. I have googled the message and that is why I said: 'And yes, I have looked at other websites for the error, and I have removed all unneccesary white spaces etc.'. The 'etc' part means that I have done other things which I have seen on the other pages in order to fix the problem and that is why I am asking if there is any other way of doing the same thing? – Masayuki Tonoki Aug 20 '15 at 01:51
  • So do you still have the error message about headers session cookie? – zerkms Aug 20 '15 at 01:51
  • Yes, I do. Is there any other way to do the same thing? – Masayuki Tonoki Aug 20 '15 at 01:55
  • 1
    possible duplicate of http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Funk Forty Niner Aug 20 '15 at 01:56

1 Answers1

0
if($_SESSION['counter'] < $row['max']){
    if (isset($_SESSION['counter'])){
        $counter = $_SESSION['counter']++;
  1. it doesn't make sense to check if $_SESSION['counter'] is less than $row['max'] and then do an isset on it, you should do the isset first.

  2. This is likely your issue: When you use ++ on the right-hand-side it returns the current value, then increments (post-increment). if you want to increment it, and return the incremented value, you need to use the pre-inrement version, so do

    $counter = ++$_SESSION['counter'];

for example;

$a=1;
$b=$a++;
//$a=2, $b=1

$a=1;
$b=++$a;
//$a=2, $b=2

See also: Pre-incrementation vs. post-incrementation

Community
  • 1
  • 1
chiliNUT
  • 18,989
  • 14
  • 66
  • 106
  • I don't want pre-increment because I want the incremented value returned. Not the original value, and I don't think your code is right if you read the link you provided, or am I going crazy? Thanks for the help, though – Masayuki Tonoki Aug 20 '15 at 01:53
  • pre-increment: increment first, then return the incremented value. you want pre-increment. – chiliNUT Aug 20 '15 at 16:27