If-else在PHP中有多个条件

时间:2018-08-08 04:43:48

标签: php mysql variables if-statement

一段时间以来,我一直在尝试使此代码适用于此表单的数据库,但无法正常工作。

This is the form input without PI_ID. This is the form input with PI_ID.

$content = $_POST['cr_code'] . "-" . $_POST['cr_num'] . "-" . $_POST['cr_mon'] . "-" . $_POST['cr_year'] . "-" . $_POST['cr_modul'] . "-" . $_POST['cr_phase'] . "-" . $_POST['cr_pi_id'];

如果我不想输入任何模块或阶段,则该输出为CR-001-08-18- -,这是错误的,因为不应在18后面的行在那里。

基本上,输出为CR-001-08-18-Marketing-PH1-PI1,模块(市场营销),阶段(PH1)和PI_ID(PI1)是可选填充,因此可以是:

  • CR-001-08-18-Marketing(模块已填充,相位未填充,PI_ID未填充)
  • CR-001-08-18-PH1(模块未填充,相位已填充,PI_ID未填充)
  • CR-001-08-18-PI(模块未填充,相位未填充,PI_ID填充)

这是我尝试使用if-else的代码:

$phase=$_GET['cr_phase'];
$modul=$_GET['cr_modul'];
$crpid=$_GET['cr_pi_id'];

if ($phase!='' && $modul='' && $crpid='')
{
	$content = $_POST['cr_code'] . "-" . $_POST['cr_num'] . "-" . $_POST['cr_mon'] . "-" . $_POST['cr_year'] . "-" . $_POST['cr_phase'];
}else if($phase='' && $modul!='' && $crpid='')
{
	$content = $_POST['cr_code'] . "-" . $_POST['cr_num'] . "-" . $_POST['cr_mon'] . "-" . $_POST['cr_year'] . "-" . $_POST['cr_modul'];
}else if($phase='' && $modul='' && $crpid=!'')
{
	$content = $_POST['cr_code'] . "-" . $_POST['cr_num'] . "-" . $_POST['cr_mon'] . "-" . $_POST['cr_year'] . "-" . $_POST['cr_pi_id'];
}else if($phase!='' && $modul!='' && $crpid!='')
{
	$content = $_POST['cr_code'] . "-" . $_POST['cr_num'] . "-" . $_POST['cr_mon'] . "-" . $_POST['cr_year'] . "-" . $_POST['cr_modul'] . "-" . $_POST['cr_phase'] . "-" . $_POST['cr_pi_id'];
}else
{
	$content = $_POST['cr_code'] . "-" . $_POST['cr_num'] . "-" . $_POST['cr_mon'] . "-" . $_POST['cr_year'];
}

但是,它不起作用,因为即使我输入了阶段,模块和PI_ID,它也只显示CR-001-08-18而没有显示阶段和模块。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

Only three if condition needed.Also check the method(GET or POST) you are using to submit the form and fetch the data accordingly or use $_REQUEST.

$phase=$_POST['cr_phase'];
$modul=$_POST['cr_modul'];
$crpid=$_POST['cr_pi_id'];

$values = "";
if($phase != '')
{
$values .= "-$phase";
}
if($modul != '')
{
$values .= "-$modul";
}
if($crpid != '')
{
$values .= "-$crpid";
}

    $content = $_POST['cr_code'] . "-" . $_POST['cr_num'] . "-" . $_POST['cr_mon'] . "-" . $_POST['cr_year'] . $values;

答案 1 :(得分:0)

switch语句类似于同一表达式上的一系列IF语句。在许多情况下,您可能希望将相同的变量(或表达式)与许多不同的值进行比较,并根据其等于哪个值来执行不同的代码。这正是交换机声明的用途。  http://php.net/manual/en/control-structures.switch.php

相关问题