我正在寻找一种只在foreach循环中放置几个变量的方法。
基本上,我正在寻找空值,并使用die告诉用户返回并填写表单。
这是我的阵列:
$posted = array(
$sales_rep = strtoupper($_POST['sales_rep']),
$c_first_name = strtoupper($_POST['c_first_name']),
$c_last_name = strtoupper($_POST['c_last_name']),
$c_address = strtoupper($_POST['c_address']),
$c_zip = strtoupper($_POST['c_zip']),
$deal_number = strtoupper($_POST['deal_number']),
$deal_stock_number = strtoupper($_POST['deal_stock_number']),
$deal_selling_model = strtoupper($_POST['deal_selling_model']),
$deal_trade_in_model_1 = strtoupper($_POST['deal_trade_in_model_1']),
$deal_trade_in_amount_1 = strtoupper($_POST['deal_trade_in_amount_1']),
$deal_trade_in_model_2 = strtoupper($_POST['deal_trade_in_model_2']),
$deal_trade_in_amount_2 = strtoupper($_POST['deal_trade_in_amount_2']),
$deal_new_status = strtoupper($_POST['deal_new_status']),
$deal_dh = strtoupper($_POST['deal_dh']),
$deal_finance_amount = strtoupper($_POST['deal_finance_amount']),
$deal_pack = strtoupper($_POST['deal_pack']),
$deal_retro = strtoupper($_POST['deal_retro']),
$deal_holdback = strtoupper($_POST['deal_holdback']),
$deal_reserve_amount = strtoupper($_POST['deal_reserve_amount']),
$deal_warrantee = strtoupper($_POST['deal_warrantee']),
$deal_gap = strtoupper($_POST['deal_gap']),
$deal_etch = strtoupper($_POST['deal_etch']),
$deal_maintenence = strtoupper($_POST['deal_maintenence']),
$deal_gross = strtoupper($_POST['deal_gross'])
);
然而,最后几个人无法进入,因为除非用户这样说,否则他们会从表单中隐藏它们(它们可能为空):
$deal_reserve_amount = strtoupper($_POST['deal_reserve_amount']),
$deal_warrantee = strtoupper($_POST['deal_warrantee']),
$deal_gap = strtoupper($_POST['deal_gap']),
$deal_etch = strtoupper($_POST['deal_etch']),
$deal_maintenence = strtoupper($_POST['deal_maintenence']),
$deal_gross = strtoupper($_POST['deal_gross'])
我知道有一种方法可以用if来做,但是我试图避免混乱我的代码,而且我正在寻找一种非常巧妙的方法来做到这一点,也许在foreach本身。
我的问题:在foreach声明中,是否可以说只放入几个变量? (限制我在阵列最后留出的那些)
如果这不起作用,我可能会运行一个Javascript函数来填充这些值,以实际说出" NULL,"但可憎的是,这不是最有效的方式。
答案 0 :(得分:0)
您可以创建隐藏键数组:
$hidden = array('deal_maintenence', 'deal_gross');
然后,您可以遍历$_POST
数组并检查密钥是否为隐藏密钥:
foreach ($_POST as $key => $value) {
if (!in_array($key, $hidden)) {
if (!empty($_POST[$key])) {
// valid
} else {
// invalid
}
}
}
或者您可以通过指定要验证的密钥以其他方式执行此操作:
$keys = array('sales_rep', 'c_first_name', 'c_last_name');
foreach ($keys as $key) {
if (isset($_POST[$key]) && !empty($_POST[$key])) {
// valid
} else {
// invalid
}
}
答案 1 :(得分:0)
也许你可以尝试这样的事情:
$variables = array ("c_first_name", "c_last_name" /* etc. */);
foreach ($variables as $v)
{
if (isset ($_POST[$v]))
{
$posted[] = $$v = $_POST[$v];
}
else
{
// scold the user for not having set the variable
}
}
这就是说,我宁愿将表单封装在PHP类中,以避免创建一堆变量。
答案 2 :(得分:0)
我想出了一个非常简单的方法......
$v_info = array( // V being visible
$sales_rep = strtoupper($_POST['sales_rep']),
$c_first_name = strtoupper($_POST['c_first_name']),
$c_last_name = strtoupper($_POST['c_last_name']),
$c_address = strtoupper($_POST['c_address']),
$c_zip = strtoupper($_POST['c_zip']),
$deal_number = strtoupper($_POST['deal_number']),
$deal_stock_number = strtoupper($_POST['deal_stock_number']),
$deal_selling_model = strtoupper($_POST['deal_selling_model']),
$deal_trade_in_model_1 = strtoupper($_POST['deal_trade_in_model_1']),
$deal_trade_in_amount_1 = strtoupper($_POST['deal_trade_in_amount_1']),
$deal_trade_in_model_2 = strtoupper($_POST['deal_trade_in_model_2']),
$deal_trade_in_amount_2 = strtoupper($_POST['deal_trade_in_amount_2']),
$deal_new_status = strtoupper($_POST['deal_new_status']),
$deal_dh = strtoupper($_POST['deal_dh']),
$deal_finance_amount = strtoupper($_POST['deal_finance_amount']),
$deal_pack = strtoupper($_POST['deal_pack']),
$deal_retro = strtoupper($_POST['deal_retro']),
$deal_holdback = strtoupper($_POST['deal_holdback'])
);
$nv_info = array( // NV being not visible
$deal_reserve_amount = strtoupper($_POST['deal_reserve_amount']),
$deal_warrantee = strtoupper($_POST['deal_warrantee']),
$deal_gap = strtoupper($_POST['deal_gap']),
$deal_etch = strtoupper($_POST['deal_etch']),
$deal_maintenence = strtoupper($_POST['deal_maintenence']),
$deal_gross = strtoupper($_POST['deal_gross'])
);
foreach ($v_info as $raw) {
if ($raw == null) {
die("Please fill in all the required elements in the form. ");
}
}
$merged_arrays = array_merge($v_info, $nv_info);
这是最好的,因为我计划将它放入数据库,为了保持一致性,我希望它来自一个数组。