Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If/Then Statement Issue 2

Status
Not open for further replies.

redzonne

Programmer
Feb 20, 2005
27
US
Can anyone point out whay exactly the below script won't quite work?

Code:
<?

$custype=$_POST['custype'];

$ccustype = addslashes($myrow["custype"]);

if ($ccustype = P )
{
 include("includes/leftnv_cpanelinclude.php");
}
else if ($ccustype = A )
{
 include("includes/leftnv_cpanelinclude_a.php");
}
?>

The Journey of a Thousand Miles Begins with the First Step...
 
You have written:
Code:
if ($ccustype = P )
{
 include("includes/leftnv_cpanelinclude.php");
}
else if ($ccustype = A )
{
 include("includes/leftnv_cpanelinclude_a.php");
}
You need a '==' for comparison. You also need to quote the literals:
Code:
if ($ccustype =[COLOR=red]=[/color] [COLOR=red]'[/color]P[COLOR=red]'[/color] )
{
 include("includes/leftnv_cpanelinclude.php");
}
else if ($ccustype =[COLOR=red]=[/color] [COLOR=red]'[/color]A[COLOR=red]'[/color] )
{
 include("includes/leftnv_cpanelinclude_a.php");
}

Ken
 
Just thought of something else... (no pun intended)

You can simplify your code by using a switch statement:
Code:
if (isset($_POST['custype'])) {
   switch ($myrow[$_POST['custype']) {
       case 'A':
            $inc_ext = '_a';
            break;
       case 'P':
            $inc_ext = '';
            break;
   }
   include('includes/leftnv_cpanelinclude' . $inc_ext '.php');
}
It may not look simplier for just two cases, but once you start adding more cases, if-then-else statements get very unwieldy very quickly.

Ken
 
Note:

The literals should be quoted. If they aren't PHP looks for a constant by that name. If it doen't find it the string equivalent is used.
So, in case there ever were a constant defined by that name it would fail. Quote string literals.
 
The == and '' worked. Thanx!!!

<?

if ($myrow["custype"] == 'P' )
{
include("includes/leftnv_cpanelinclude.php");
}
else if ($myrow["custype"] == 'A' )
{
include("includes/leftnv_cpanelinclude_a.php");
}
?>

The Journey of a Thousand Miles Begins with the First Step...
 
Also, an addition:
* If the files which are to be included are _vital_, use require() instead of include().
* If the file including or requireing files, is included or required several other places, remember the _once: include_once() or require_once(), or you might get errors with multiple definitions of the same functions.

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top