Function runs on page load

Hello all - second post on here I’m hoping someone can help me. I’m not a programmer and I’m messing with programming! I’ve come into a bug that I’m hoping someone can clear up for me I’m sure its something silly on my end. I have a function that should only be ran upon an IF condition yet it runs even when its IF value isn’t correct. I’m using a “print_r($GLOBALS);” function to check my steps.

To me this line says "if variable $lookup_by_name2 does not equal “blessed” then make it equal “cursed”

	if($lookup_by_name2 != "blessed");
{
	$lookup_by_name2 = "cursed";
}

followed by a code that to me reads " if variable $lookup_by_name2 is equal to “blessed” then run this function on another php.

				if($lookup_by_name2 == "blessed");
			 {
				 get_selected_biddersSUPER($auctioneer_id, $last_name, $first_name, $license_number, $zip, 20);
			 }

And this function returns a block of return records from the database. All of that works great. However, when I select a record, it sets my variable back to equaling “cursed” when to me… there should be nothing to do so. I made the value cursed and blessed because they are found nowhere else in the program.

Thank you ahead of time :slight_smile:

I have put it through a syntax checker to make sure that isn’t the fault; it has some thing to do with a piece beyond the if that I’m not comprehending.

The logic doesn’t make sense. You don’t show or say you do anything with cursed so all you need is the last if block.

Well - I don’t want the function to run always. Without the first block the variable gets set indefinitely to “blessed” and runs on page open.

Post the whole code.

1 Like

pass - should not be necessary; There isn’t a single line of code in the program that has either of these values or these variables set. They only exist on this page in these lines. I’m sure its just a misunderstanding of the sequence.

If it want necessary I wouldn’t have asked. Whatever you are doing, you are doing it wrong. If your not going to cooperate when people are helping you for free then don’t come here wasting our time. You obviously have no interest in learning anything.

1 Like

I have no interest in specifically your opinion when it gets hostile. If you can help, great! if not, I’ll wait for a less volatile user to provide an opinion.

I don’t deem it necessary and without any sort of logic to support otherwise I’ll pass. Thanks again.

I’ll take it then as well that you deem my two blocks of code to be written correctly! Which is good news. I appreciate that. So it must be a further function that I am overlooking :slight_smile: Thanks.

Is declare(strict_types=1); if not declare…

Is error_reporting set? If not set it to -1

Is ini_set(‘display_errors’, ‘1’);? If not…

Is $lookup_by_name2 set if not test with isset(`$lookup_by_name2‘);

A little more script would eliminate guessing :slight_smile:

Correct

Also correct

As you’ve already deduced, and others have already insinuated, whatever is causing your problem is not in these blocks of code, so it must be somewhere else.

Are these two blocks within the same variable scope? If they are in different functions for example, the variables $lookup_by_name2 are not the same variable, if though their name is the same.

Eg.

function foo()
{
  $a = 1;
}

function bar()
{
  // this does not change $a in the foo() function.
  // that will remain 1, because even though these
  // variables have the same name, they are not
  // same variable, because they are local to the function
  // they are in
  $a = 2;
}

// and this is yet another $a, that is not related to
// $a in foo() or $a in bar(), except that the name is the same
// but that's just a coincidence.
$a = 3;

function doNotDoTryThisAtHome()
{
   // if you really really wanted to, you can get access to the $a
   // with value 3 by using the global keyword, but this way
   // madness lies, so I wholeheartedly recommend against
   // doing that
   global $a; // this is 3
}

// however, even you really wanted to, you can never get direct access
// to $a in foo() or in bar(), they are out of scope, and there is no
// way to bring them in scope, nor should there be.
2 Likes

I suspect this is the bit of code we need to see, or perhaps a more detailed explanation of the steps involved in “selecting a record”. If, for example, that result in reloading the page, or loading another page, then your original variable value has gone by the time you get to that second page. But that’s just a guess. On the face of your code, as @rpkamp said, it looks fine as posted.

How, exactly, do you go from retrieving a block of records in the code you showed, to selecting an individual record?

I’m going to google 50% of what you said here. It definitely seems like it will help me, if not just to understand the concepts. I’m rank 0 at programming -thank you for the direction.

1 Like

This and everything beneath what you had put is HUGE and new to me.Thank you indefinitely this is quite easy to follow. I haven’t quite fixed the problem but I’m 100% sure I’m way out of variable scope.

if($lookup_by_name2 != “blessed”);

That piece of code (and the similar one in the next block) is not doing what you think. An if() statement rarely has an empty statement followed by a semicolon right after it. In this case, you’re testing if $lookup_by_name is not “blessed”, and then if so “;”, i.e. “do nothing”. Then you immediately assign $lookup_by_name to “cursed”, regardless of the prior if statement.

So basically, remove those two semicolons.

2 Likes

Someone else verify this - I’m quite sure he is wrong BUT I’m the non-programmer.
to my knowledge it goes

if(condition)
{
code to do if condition is met
}

I was about to point out the same syntax error, there should not be a semi-colon after the if brackets.

if($lookup_by_name2 != "blessed")
{

Thank you

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.