Jump to content

SWITCH() CASE ...: Does not work with define() values


Rottingham

Recommended Posts

Hello all,

 

I have found over the last year or so, everytime I try to used predefined values for the switch/case checking, that it fails to recognize the defined value. I.e.

 

define("VIEW_GENERAL", "0");
define("VIEW_ADDRESS", "1");
define("VIEW_FAMILY", "2");

function view_person($id, $category)
{
      $person = $id;
      
      switch($category)
      {
      case VIEW_GENERAL:
          echo "View General Information";
      break;
      case VIEW_ADDRESS:
      ...
      break;
      default
      break;
      }

}

 

This never works! Rather than using VIEW_GENERAL I need to hard code the switch cases with "0" or "1", etc etc.

 

Is there any fix for this?

ran this

 

<?php
define("VIEW_GENERAL", "0");
define("VIEW_ADDRESS", "1");
define("VIEW_FAMILY", "2");

function view_person($category)
{
      switch($category)
      {
      case VIEW_GENERAL:
          echo "View General Information";
          break;
      case VIEW_ADDRESS:
          echo "View Address Information";
          break;
      case VIEW_FAMILY:
          echo "View Family Information";
          break;
      default:
          echo "View Default Information";
          break;
      }

}

$a = array (0,1,2,3,0);

foreach ($a as $cat) {
    echo "$cat : ";
    view_person($cat);
    echo '<br/>';
}

?>

 

got this

[pre]

0 : View General Information

1 : View Address Information

2 : View Family Information

3 : View Default Information

0 : View General Information

 

[/pre]

(NOTE: Some of my code is innefficient, such as if/else statements. That is in the process of being cleaned, but everything is valid and working. Until I use defined values.)

 

My Code

 

// [ VIEW CATEGORIES ]
define("VIEW_GENERAL", "0");
define("VIEW_ADDRESS", "1");
define("VIEW_FAMILY", "2");
define("VIEW_HISTORY", "3");
define("VIEW_CONTRIBUTIONS", "4");
define("VIEW_QUICKPRINT", "5");


function View_Person()
{
global $myTemp;
global $Content;

// If a specific category is not set, make it 0.
if(!isset($_REQUEST["cat"]))
	$cat = 0;
else
	$cat = $_REQUEST["cat"];

// If a view ID is not given, check for last_person_viewed in the session
if($_REQUEST["view"] <= 0)
	if(isset($_SESSION["last_person_viewed"]))
		$ID = $_SESSION["last_person_viewed"];
	else {
		GoTo("people.php");
		exit;
	}
else
	$ID = $_REQUEST["view"];


// Category
switch($cat)
{
case VIEW_GENERAL:
	$view_person = myTemplate::fetch_data_chunk("templates/", "people.tpl", "view_general");
	show_general($view_person, $ID);
	break;
case VIEW_ADDRESS:
	$view_person = myTemplate::fetch_data_chunk("templates/", "people.tpl", "view_address");
	show_address($view_person, $ID);
	break;
case VIEW_FAMILY:
	GoTo("family.php?view=$ID");
	exit;
	break;
case VIEW_HISTORY:
	$view_person = myTemplate::fetch_data_chunk("templates/", "people.tpl", "view_notes_history");
	show_notes_history($view_person, $ID);
	break;
case VIEW_CONTRIBUTIONS:
	break;
default:
	echo "Default";
	break;
}

$view_person = str_replace("{\$theme}", "people", $view_person);
$Content .= $view_person;

app_finish();
}

 

my link:

http://churchorg.macaction.org/people.php?view&cat=1

 

I can change cat to anything, and I always get default! As soon as I change the case to "0" or "1" or whatever the case may  be, it works... that is why I am stumped.

I am doing that right now. The number is output, and then the Default echo from the default statement.

 

such as,

 

0Default

 

or 1Default

 

             echo "Category: $cat<br />";
// Category
switch($cat)
{
case VIEW_GENERAL:
	$view_person = myTemplate::fetch_data_chunk("templates/", "people.tpl", "view_general");
	show_general($view_person, $ID);
	break;
case VIEW_ADDRESS:
	$view_person = myTemplate::fetch_data_chunk("templates/", "people.tpl", "view_address");
	show_address($view_person, $ID);
	break;
case VIEW_FAMILY:
	GoTo("family.php?view=$ID");
	exit;
	break;
case VIEW_HISTORY:
	$view_person = myTemplate::fetch_data_chunk("templates/", "people.tpl", "view_notes_history");
	show_notes_history($view_person, $ID);
	break;
case VIEW_CONTRIBUTIONS:
	break;
default:
	echo "Default";
	break;
}

 

That gets me

 

Category: 1

Default

 

If I start a test.php and put this in, then it works fine...

 

<?php
define("ONE", "1");
define("TWO", "1");
define("THREE", "1");

$test = 1;

switch($test)
{
case ONE:
echo "Found a value of ONE";
break;
case TWO:
echo "Found a value of TWO";
break;
case THREE:
echo "Found a value of THREE";
break;
default:
echo "Default value!";
break;
}
?>

 

Found the error...

 

I moved all of the define statements to the beginning of the PHP file and that worked... Unsure why that would be, but apparently having them set half way through the php file, something took place to block the defines from working.

No, I had the defines before the function declaration, but the call to the function was made from another function placed earlier in the file. I suppose that would mea that at run time, the actual call was placed in memory before the define.

 

I see the error of my ways! Thanks for the troubleshooting help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.