nbarone Posted August 5, 2009 Share Posted August 5, 2009 I am setting up a registration form for a trip. The variable name is the location name the key is the number of people who can fit in a room at that location the value is the number of rooms available that will fit that many people. I want to scan the DB for the number of people registered for that location, with there number of roommates, and see if any rooms are still available. Here is my script, but I feel like it can be done in a better way. Any input is appreciated. <?php // NUMBERS $foxridge[8] = 3; $foxridge[6] = 6; $alpine[8] = 10; $alpine[6] = 10; $valley[8] = 5; $valley[6] = 1; $wildflower[2] = 23; $wildflower[4] = 32; $wildflower[6] = 20; $snowpine[2] = 4; $snowpine[4] = 18; $snowpine[6] = 30; $snowpine[8] = 15; foreach($foxridge as $k=>$v){ $sql = "SELECT * FROM breakaway WHERE b_location='foxridge' AND b_numRoommates='".$k."'"; if(mysql_num_rows(mysql_query($sql)) >= $v) $foxridge[$k] = false; } foreach($alpine as $k=>$v){ $sql = "SELECT * FROM breakaway WHERE b_location='alpinemeadow' AND b_numRoommates='".$k."'"; if(mysql_num_rows(mysql_query($sql)) >= $v) $alpine[$k] = false; } foreach($valley as $k=>$v){ $sql = "SELECT * FROM breakaway WHERE b_location='valleyvillage' AND b_numRoommates='".$k."'"; if(mysql_num_rows(mysql_query($sql)) >= $v) $valley[$k] = false; } foreach($wildflower as $k=>$v){ $sql = "SELECT * FROM breakaway WHERE b_location='wildflower' AND b_numRoommates='".$k."'"; if(mysql_num_rows(mysql_query($sql)) >= $v) $wildflower[$k] = false; } foreach($snowpine as $k=>$v){ $sql = "SELECT * FROM breakaway WHERE b_location='snowpine' AND b_numRoommates='".$k."'"; if(mysql_num_rows(mysql_query($sql)) >= $v) $snowpine[$k] = false; } ?> Link to comment https://forums.phpfreaks.com/topic/168955-is-this-the-best-way/ Share on other sites More sharing options...
flyhoney Posted August 5, 2009 Share Posted August 5, 2009 I would do something like this to make it more of a configuration thing to add data as opposed to writing new code every time you add a room/location: <?php $locations = array( 'foxridge' => array( 'rooms' => array( array(8, 3), array(6, 6) ) ), 'alpine' => array( 'rooms' => array( array(8, 10), array(6, 10) ) ), 'valley' => array( 'rooms' => array( array(8, 5), array(6, 1) ) ), 'wildflower' => array( 'rooms' => array( array(2, 23), array(4, 32), array(6, 20) ) ), 'snowpine' => array( 'rooms' => array( array(2, 4), array(4, 18), array(6, 30), array(8, 15) ) ), ); foreach ($locations as $name => $location) { foreach ($location["rooms"] as $capacity => $availability) { $query = " SELECT * FROM breakaway WHERE b_location = '$name' AND b_numRoommates = '$capacity' "; if (mysql_num_rows(mysql_query($query)) >= $availability) $locations[$name] = FALSE; } } Link to comment https://forums.phpfreaks.com/topic/168955-is-this-the-best-way/#findComment-891529 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.