2015-01-14 2 views
-2

foreach 루프에 몇 가지 변수 만 넣을 수있는 방법을 찾고 있습니다.Foreach가 제한되어 있습니까?

기본적으로 널 (null)을 찾고 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']) 
); 

이 그들이 양식에서 숨겨져 있기 때문에 사용자가 이렇게 말한다 않는 한, 그러나에 갈 수 없어 몇 마지막으로 (그들은 null도 가능) :

내 배열입니다
$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 자체에서 .

내 질문 : foreach 문에서 몇 가지 변수를 넣을 수 있습니까? (배열 끝 부분에 설정 한 것을 제한하려면)

이 방법이 효과가 없다면 실제로이 값을 채우기 위해 Javascript 함수를 실행하여 " NULL "이라고 말하면서, 그렇게하는 것이 가장 효율적인 방법은 아닙니다.

당신은 숨겨진 키의 배열을 만들 수 있습니다
+0

그래서 지금 질문은 무엇입니까? 이 배열 요소들을 거치지 않으시겠습니까? – Rizier123

+0

http://stackoverflow.com/a/184104/460557 –

+0

팁 :'$ case_changed = array_map ('strtoupper', $ _POST); ' –

답변

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); 

을 파악하고, 일관성을 위해 나는 모든 것이 하나 개의 배열에서 온 원한다.

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 
    } 
} 
+0

그렇다면 단순히 foreach에서는 말할 수 없다는 것을 의미합니다 당신은 단지 $ $ [0-x]를 $ raw로 사용하기를 원한다는 문장? – ilarsona

+0

'$ _POST'는 연관 배열입니다. 유효성을 검사하려는 키 또는 유효성을 검증하지 않으려는 키 중 하나를 지정해야합니다. 내 업데이트 답변을 참조하십시오. –

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 클래스에 폼을 캡슐화하려고합니다.

관련 문제