0

I'm trying to attribute a value to a var depending of its text value. I can't just attribute the numerical value directly (via my form) because i'm also saving in my database the text value.

$section = $_POST['section']; // required
$page = $_POST['page']; // required

if ($_POST['page'] == 'Template') { $Code == '00'; }
elseif ($_POST['page'] == 'Menu') { $Code == '01'; }
elseif ($_POST['page'] == 'Home Page') { $Code == '02'; }
elseif ($_POST['page'] == 'About Us') { $Code == '03'; }
elseif ($_POST['page'] == 'Contact Us') { $Code == '04'; }

$Code_Page = $Code.''.$section;

Exemple, the code for the section 18 of the Home Page is 0218.

I don't understand why it doesn't work ? Only the second part is saved ($section), not the first 2 numbers.

Thanks in advance

4

3 回答 3

0

编辑:作者已经澄清了他现在的目标。我保留了原始答案,因为我觉得这是一个更好的解决方案。

要在 PHP 中定义变量,请使用单个等号 (=)。您在定义$Code时使用了两个等号 (==) ,这是一个比较运算符

原答案:

我假设您希望优化此代码。如果是这样,我觉得最好的方法是创建一个数组,或者从您提到的数据库中提取。将数据集放入数组后,您可以进行一些简单的检查:

静态数组方法:

$pages = array('Template' => '00', 'Menu' => '01', 'Home Page' => '02', 'About Us' => '03', 'Contact' => '04');
if (in_array($_POST['Page'], $pages))
{
    //We have a match, deal with output here
    $Code_Page = $pages[$_POST['Page']].$section
}
else
{
    //Output an error message here, the page isn't in the array
}
于 2013-08-30T04:41:58.117 回答
0

先添加$Code声明

$section = $_POST['section']; // required
$page = $_POST['page']; // required
$Code = ''; // This was missing 

if ($_POST['page'] == 'Template') { $Code == '00'; }
elseif ($_POST['page'] == 'Menu') { $Code == '01'; }
elseif ($_POST['page'] == 'Home Page') { $Code == '02'; }
elseif ($_POST['page'] == 'About Us') { $Code == '03'; }
elseif ($_POST['page'] == 'Contact Us') { $Code == '04'; }

$Code_Page = $Code.''.$section;
于 2013-08-30T04:42:16.920 回答
0

您提供的代码似乎为您提供了您正在寻找 0218 主页的代码。我看到的唯一错误是您没有分配$Code. 你$Code == '02'应该是$Code = '02'

于 2013-08-30T04:43:24.117 回答