If statement not working

I’m trying to check a database and do one thing depending on the results of a query. If there are ten or more entries then I want to basic tell people they can’t progress. If there’s less than ten then I want them to be able to progress…

This is what I’ve got so far

<?php

$query="select COUNT(customer_id) as cnt from orders where country=\"USA\"";
  $result=dbselect( $query,"dbLinkInt" );
  if( $result[0]['cnt'] >=3 ) { $exists=1; }
	
......

    if( $exists!=1 ) {
execute code here
}
?>

<?php if($_GET['commit']==1 ) { ?>
       This works

        <?php } elseif( $_GET['commit']==1 && $exists=1 ) { ?>
        This doesn't work

        <?php } else { ?>
This works
} ?>

var_dump($result); give this:

array(1) {
  [0] => array(1) {
    ["cnt"] =>  string(1) "12"
  }
}
$result=dbselect( $query,"dbLinkInt" );
  if( $cnt >=10 ) { $exists=1; }

Where are you assigning the value to $cnt?

I thought it was from this line:
query=“select COUNT(customer_id) as cnt from orders where country="USA"”;
$result=dbselect( $query,“dbLinkInt” );
if( $result[0][‘cnt’] >=3 ) { $exists=1; }

Am I wrong in thinking that?

Just checking, do you really mean to assign the value of “1” to $exists in the conditional?
elseif( $_GET['commit']==1 && $exists=1 )

The reason I though that was the best way of doing it is because if it’s 1 then I’ll send them to one place and if it’s not then I’ll send them else where

Executing it directly against the database, sure. But when running it in php, you need to assign it to a variable to be able to access it…

(I’m sure there’s a better way since my php is so rusty, but this is the general gist. You’d need to insert this right after the dbselect statement (I’m assuming that method does a mysqli query):

$row = $result->fetch_row();
$cnt = $row[0];

I changed the var_dump to show $exists instead and it showed 1 (it shows NULL if the number is less than 10) which is correct. So it looks like it’s this part that’s not working:

<?php } elseif( $_GET['commit']==1 && $exists=1 ) { ?>

oops. Should have caught that. The overly obvious ones are the easiest to miss.

This

&& $exists = 1

should be

&& $exists == 1

The first one assigns the variable the value, the second compares the values.

Thanks, it didn’t quite work but I added && !$exists to the other $_GET[‘commit’] and it now works.

I’ve now got this and it works perfectly:

<?php if($_GET['commit']==1 && !$exists ) { ?>
        success text

        <?php } elseif( $_GET['commit']==1 && $exists == 1 ) { ?>
        too many orders text

        <?php } else { ?>
thanks
} ?>

That makes sense (should have caught that as well… :blush:) . You never got to the else because the first statement is true.

You probably should have revamped your logic a little

<?php if( $_GET['commit']==1 && $exists == 1 ) { ?>
        too many orders text
<?php } elseif($_GET['commit']==1 && !$exists ) { ?>
        success text
<?php } else { ?>

or (how I would have done it to make it a little clearer…)

<?php if($_GET['commit']==1) 
         { 
            if($exists == 1 ) 
            { ?>
                too many orders text
<?php       } else { ?>
                 success text
<?php       }
         } else { ?>
thanks
<?php    } ?>

Thanks Dave, I should have noticed it too!

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