2016-06-12 68 views
1

我有多个页面,每个页面都有多个单选按钮,它们传递值并在检查所有单选按钮上的不同选择后输出当前我创建的唯一结果许多如果条件难以主导,可以为它做一个数组或循环。在php中创建一个循环或数组,用于比较来自不同页面的多个会话

<?php 
      if(isset($_POST['material'])) { 
       $_SESSION['material'] = $_POST['material']; 

       // for screw 
       if($_SESSION['category'] == "Screw") { 
       if($_SESSION['headtype'] == "Counter Sink Philips") { 
        if($_SESSION['diameter'] == "6 MM"){ 
        if($_SESSION['length'] == "10 MM"){ 
         if($_SESSION['pitch'] == "1 MM") { 
         if($_SESSION['material'] == "Brass") { 
          echo "kenenth start with database"; 
         } 
         } 
        } 
        } 
       } 
       } 


       // for self tapping 
       if($_SESSION['category'] == "Self Tapping") { 
       if($_SESSION['headtype'] == "Counter Sink Philips") { 
        if($_SESSION['diameter'] == "6 MM"){ 
        if($_SESSION['length'] == "10 MM"){ 
         if($_SESSION['pitch'] == "1 MM") { 
         if($_SESSION['material'] == "Brass") { 
          echo "Self Tapping"; 
         } 
         } 
        } 
        } 
       } 
       } 

       // for stud 
       if($_SESSION['category'] == "Stud") { 
       if($_SESSION['headtype'] == "Counter Sink Philips") { 
        if($_SESSION['diameter'] == "6 MM"){ 
        if($_SESSION['length'] == "10 MM"){ 
         if($_SESSION['pitch'] == "1 MM") { 
         if($_SESSION['material'] == "Brass") { 
          echo "Stud"; 
         } 
         } 
        } 
        } 
       } 
       } 

      } 
      ?> 

回答

1

你可以写一个递归函数来为你做的:

function in_array_r($find, $yourArray, $strict = false) { 
    foreach ($yourArray as $item) { 
     if (($strict ? $item === $find : $item == $find) || (is_array($item) && in_array_r($find, $item, $strict))) { 
      return true; 
     } 
    } 

    return false; 
} 

使用

$a['category'] = array("Screw", "Self Tapping", "Stud"); 
$a['headtype'] = array("Counter Sink Philips", "Counter Sink Philips")); 
echo in_array_r($_SESSION['category'], $a) ? 'found' : 'not found'; 
+0

我将如何比较它们? –

+0

以条件打印会话值。 :) – 2016-06-12 08:47:27

1

这应该这样做

$dataToCheck["category"] = "multipleoptions"; 
$dataToCheck["headtype"] = "Counter Sink Philips"; 
// etc... 

$found = true; 
$foundWhat = ""; 

foreach ($dataToCheck as $key => $value) 
{ 
    if($key == "category" && ($_SESSION[$key] == "Screw" || $_SESSION[$key] == "Self Tapping" || $_SESSION[$key] == "Stud")) 
     { 
      $foundWhat = $_SESSION[$key]; 
      continue; 
     } 
    else 
    { 
     found = false; 
     break; // Unknown category 
    } 
    if($_SESSION[$key] != $value) 
    { 
     $found = false; 
     break; 
    } 
} 


if($found == true) 
    echo $foundWhat; 
+0

但会议headtype本身具有3个单选按钮,每个会话和sel都是如此evey不同的序列不同的输出发生了怎么处理呢? –

+0

@KennethJohnFalbous更新了检查类别的答案但是,如果每个类别都有自己的规格,那么最好为每种类型设置3个dataValue数组和3个foreach。这种方法适用于一个foreach,因为它们具有相同的规格只是不同的类别 –

相关问题