Page 1 of 1

"Simple" if statement problem

Posted: Thu Jan 12, 2006 4:23 pm
by robscriven
Hi,

I've done the following if statement that checks the value of a student and then retrieves the corresponding row, all the field names are correct and are fine but it doesnt work...any ideas? the field type in the database is a varchar.

Thanks

- Rob

Code: Select all

<?php 
	  if ($row_bookings['student_type'] == 'student'){
		  $bookingsum = $bookingsum + $row_bookings['student_price'];
	  }
	  	  if ($row_bookings['student_type'] == 'teacher'){
		  $bookingsum = $bookingsum + $row_bookings['teacher_price'];
	  }
	  	  if ($row_bookings['student_type'] == 'external'){
		  $bookingsum = $bookingsum + $row_bookings['external_price'];
	  }
	  ?>

Posted: Thu Jan 12, 2006 4:27 pm
by RobertGonzalez
Try a switch statement. And when you say they are not working, what do you mean?

Code: Select all

<?php

switch ($row_bookings['student_type'])
{
    case 'student':
    $bookingsum = $bookingsum + $row_bookings['student_price'];
    break;

    case 'teacher':
    $bookingsum = $bookingsum + $row_bookings['teacher_price'];
    break;

    default:
    case 'external': 
    $bookingsum = $bookingsum + $row_bookings['external_price'];
    break;
}

Posted: Fri Jan 13, 2006 6:56 am
by BDKR
Another option is to dump the $row_bookings array before the test to make sure the 'student_price' element is there. print_r() is your freind.

Posted: Fri Jan 13, 2006 12:19 pm
by duk
var_dump(); -> will help you

Posted: Fri Jan 13, 2006 1:18 pm
by robscriven
I tried the switch and that's got it working and taken on board the other two suggestions. Thanks for your help guys.