Unexpected results from function

I am a novice so I apologize if I have left out important details!
I have a simple function to warn if a manager is overbooking.
The function works fine as long as the places requested are not greater than the places allowed.
If the places requested exceed places allowed $results is null

PSEUDO-CODE

$placesRequest =  value from form
$placesAllowed =  value from settings
$result  = query db (count number of booked seats already in that time slot)

if (( $placesRequest + $result ) > $placesAllowed) { echo warning}

when requesting 6 places when 3 are already booked and the max places allowed is 6 will result in a proper warning “you are over by 3”
ie
if (( 6 + 3) > 6) { accurate warning}

requesting 7 tables when 3 are already booked and the max places allowed is 6 will result in an empty value for results and an inaccurate warning
ie
if (( 7 + #) > 6) { inaccurate warning “you are over by 1”}

To reiterate, things work fine until $placesRequest is greater than $placesAllowed .
The variable names are unique to this function.

Thanks in advance for direction!

Hello idamay,

If you have “unexpected results”, then you will have to post exact code (while removing credentials and such) if you would like use to be able to properly help you with a bug.

I knew you’d say that :slight_smile:

I cut lines that were just pertaining to building $hours



$hours = array_merge ( $hoursBefore, $hoursAfter );


	$placesAllowed = $placesvalue;
	$hoursRequest = $somehoursvalue;
	$dateRequest = $somedatevalue;
	
			foreach ( $hours as $hour ) {				
		
				$query = $this->_db->getQuery ( true );
				$query->select( 'SUM(a.places) AS sum');
				$query->from ( '#__booking AS a ');
			        $query->where ( 'a.time = '. $hour .' AND a.date = ' . $this->_db->Quote ( $dateRequest ) . ' AND a.status != ' . $this->_db->Quote('canceled') );
				$this->_db->setQuery ( $query );
				$result = $this->_db->loadResult();
				$difference = $placesRequest + $result - $placesAllowed;
				
				if (( $placesRequest + $result ) > $placesAllowed) {
			
		echo "WARNING:  The maximum seats for the time period selected will be exceeded by $difference";
	
				}
			}

	

How about the structure of $hours (sample array).

And please do a var_dump of $result of this line: $result = $this->_db->loadResult();

PHP uses the # hash character as a remark same as //.

Try this:



   if (( $placesRequest + $result ) > $placesAllowed) {
      echo '$query = ' .$query; 
      echo "<br />WARNING:  The maximum seats for the time period selected will be exceeded by $difference";
   }