"Simple" if statement problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
robscriven
Forum Newbie
Posts: 6
Joined: Sun Jan 08, 2006 5:38 pm

"Simple" if statement problem

Post 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'];
	  }
	  ?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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;
}
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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.
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post by duk »

var_dump(); -> will help you
robscriven
Forum Newbie
Posts: 6
Joined: Sun Jan 08, 2006 5:38 pm

Post 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.
Post Reply