PHP Conditional code using MySQL lookup

 I want to run conditional code in a php script depending on database settings.

Psuedo code example:

$input_colour = $_GET['colour']      // passed into php file

get_database_colour_settings()

switch($input_colour)
{
  case 'RED'
    if(this colour is enabled in database){
    // some red code
    }
  break;

  case 'BLUE'
    if(this colour is enabled in database){
        // some BLUE  code
    }
  break;

  case 'GREEN'
    if(this colour is enabled in database){
        // some GREEN code
    }
  break;
}
 
  I guess the database would have colour_id, colour_name, on_off_setting.
 
  I want a way so we only query the database once and not everytime in the switch as this script is called many times.

Thanks
oddszoneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Andyc75Software ArchitectCommented:
Hey oddszone,  here is a script which should do as you wanted.

The database is only queried once, at which time the results are returned into an array.

That array is then used to validate the colors in the switch statement.


//Connect
$db = mysql_connect($host,$user,$pass) or die("Problem connecting");
mysql_select_db($database) or die("Problem selecting database");

if(isset($_GET['colour'])){
	$input_colour = $_GET['colour'];      // passed into php file

	$colour_array = array();

	$colour_array = get_database_colour_array();

	switch($input_colour)
	{
	  case 'RED':
		if(is_valid_color($colour_array, $input_colour)){
			echo 'some Red Code';
		}
	  break;

	  case 'BLUE':
		if(is_valid_color($colour_array,$input_colour)){
			echo 'some BLUE  code';
		}
	  break;

	  case 'GREEN':
		if(is_valid_color($colour_array,$input_colour)){
			echo 'some GREEN code';
		}
	  break;
	}

	
}

/*

Database Scripts

create table colours (
colour_id int not null auto_increment,
colour_name varchar(100) null,
on_off_setting boolean null,
primary key(colour_id)
);

insert into colours (colour_name) values ('RED');
insert into colours (colour_name) values ('BLUE');
insert into colours (colour_name) values ('GREEN');

*/

function is_valid_color($colour_array, $check_colour){
	
	$is_valid_colour = false;
	foreach($colour_array as $key => $value){
			if($check_colour == $value){
			    $is_valid_colour = true;
			}
	}
	
	return $is_valid_colour;

}

function get_database_colour_array(){
	$colour_array = array();
	
	$query = "select colour_id, colour_name from colours";
	$result = mysql_query($query);
	
	if(mysql_num_rows($result) > 0){
		while($row = mysql_fetch_array($result)){
			$colour_array[$row['colour_id']] = $row['colour_name'];
		}
	}
	
	return $colour_array;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
oddszoneAuthor Commented:
Code looks great and I can see what you are trying to do but how does code use the "on_off_setting" as it's not refered to in code?

Thanks
0
Andyc75Software ArchitectCommented:
ok np, what do you want the on_off_setting to do ?
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

oddszoneAuthor Commented:
your code checks wether color exists but it needs to check if it's on or off as well

I did this but don't know if a better way..

added..

$market_array1[$row['market_id']] = $row['on_off'];  

to get_database_colour_array which gives me on_off settings.

then

function is_valid_market($market_array,$market_array1, $check_market){
    $is_valid_market = false;
    foreach($market_array as $key => $value){
        if($check_market == $value){
            if($market_array1[$key] == "on"){
              $is_valid_market = true;
            }
        }
    }
    return $is_valid_market;
}

Iis this valid  (it works!) or is there an easier way?

Thanks


0
Andyc75Software ArchitectCommented:
Yup that works.

The other option is to just build another function called is_checked(market_array, market) where you would only run is_checked if is_valid returned true.

if(is_valid()){
   if(is_checked()){
  }
}

either way will work.
0
Andyc75Software ArchitectCommented:
Also if you don't need to go through the switch statement you could always just do a single query and check for a result.

in the above example the switch statement seems a little redundant (but you may have more uses for the switch )

$query = "select colour_id, colour_name, on_off_setting from colors where color_name = '" . $input_colour . '" and on_off_setting = 'on'";
0
oddszoneAuthor Commented:
Took time to code actual solution

Many thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.