Jump to content

Mysterious Resetting Variable


Shadow Jolteon

Recommended Posts

Hello,

 

I have been working on a page for a couple days now for my website, and I seem to have hit a bump in the road, and I can't figure out what the problem could possibly be. The code below is what I'm having trouble with. The variable $watchPost gets reset (returning a 0) after the first if statement, and all variables that used the $watchPost variable adjust accordingly ( watchPosition returns 0, watchPlace returns 1 ). Before the if statement, everything works fine, however. The other two variables in the bind_result seem to work fine.

 

        if( $loginId && $threadSub = $mysqli -> prepare( $threadSubSql ) ) {
            $threadSub -> bind_param( "ss", $loginId, $arrKey );
            $threadSub -> execute();
            $threadSub -> bind_result( $watchType, $watchTime, $watchPost );
            $threadSub -> fetch();

            $watchPosition  = $watchPost;
            $watchPlace     = $watchPosition + 1;

            if( $watchTime < $thread[$arrKey]["puretime"] ) {
                // watchPost variable reset here?
                $thread[$arrKey]["newpost"] = $watchPosition;
                
                if( $watchPlace >= $loginThreadPage + 1 )
                    $watchPage = "page/" . floor( $watchPost / $loginThreadPage );
                
                $thread[$arrKey]["postlink"]    = $watchPage . "#post" . $watchPlace;
                $thread[$arrKey]["newmark"]     = ' class="thread_unread"';
            }
            
            $thread[$arrKey]["sub"] = $watchType;
            unset( $watchType, $watchTime, $watchPost, $watchPage, $watchPosition, $watchPlace );
            
            $threadSub -> close();
        }

 

This issue is driving me crazy now, so I would greatly appreciate any replies.

Thank you!

Link to comment
Share on other sites

How did you confirm that $watchPost is changing value after the if statement? Did you try something like this

echo "watchPost before if() statement: {$watchPost}<br>\n";
if( $watchTime < $thread[$arrKey]["puretime"] ) {
echo "watchPost after if() statement: {$watchPost}<br>\n";

 

I don't see anywhere in that code that $watchPost is actually used in output, so I'm curious how you verified that hypothesis. I don't see anything in the code that would cause that so there is probably some other explanation. It would be a good idea to echo all/most of your variables to the page at key points in the logic to verify they contain what you think they contain.

Link to comment
Share on other sites

I echoed the variable immediately before, inside, and after the if statement, the results for the first result of the $watchPost variable being: 60, 0, 0

 

I echoed everything in this area of my coding (and some from before) at several points throughout my file, all with the same results: anything before the if is fine. I have actually rewritten this entire section, as well, and the same thing keeps happening.

 

Thanks! =)

Link to comment
Share on other sites

Forgive me for being skeptical, but I've had similar situations before and there is always a logic explanation. Can you try the two lines I showed above EXACTLY as I put them - one immediately before the if() statement and one immediately after? In fact, I would suggest echoing the values of the variables used in the if() condition as well. Give this a try and post the results:

 

echo "watchPost before if() statement: {$watchPost}<br>\n";
echo "watchTime before if() statement: {$watchTime}<br>\n";
echo "arrKey before if() statement: {$arrKey}<br>\n";
echo "thread[arrKey]['puretime'] before if() statement: {$thread[$arrKey]['puretime']}<br>\n";
if( $watchTime < $thread[$arrKey]["puretime"] ) {
echo "watchPost after if() statement: {$watchPost}<br>\n";
echo "watchTime after if() statement: {$watchTime}<br>\n";
echo "arrKey after if() statement: {$arrKey}<br>\n";
echo "thread[arrKey]['puretime'] after if() statement: {$thread[$arrKey]['puretime']}<br>\n";

Link to comment
Share on other sites

Hey, thanks a ton for that, I found the issue after replacing the if statement with that. It turns out that I was getting confused in my own echoing of the variables because I forgot to add a line break after a couple of them and it kinda messed it up. So, it turns out there was not any problem at all, other than with myself. =p

 

Thank you very much for your help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.