将开关保存到变量中或稍后在

时间:2016-08-09 09:11:26

标签: php forms switch-statement

我有一个foreach循环,打印1-12的数字,每个数字都是对问题和答案的引用。 switch语句获取信息并打印摘要。

foreach ($_POST as $name => $value) {


    $groupSplit = str_split($name, 5);
//  print_r("Group Num: " . $groupSplit[1] . "<br/>");

    $optSplit = str_split($value, 3); // splits opt and the num x 
//  print_r("Option Num " . $optSplit[1] . "<br/>");



switch ($groupSplit[1]) {
    case 1:
        echo  "<h2>" . $group1 . "</h2>" . isEven($optSplit[1] . "<br><button onclick='window.history.back()'>Start Over</button><button>Confirm and Send</button>") ;
        break;
    case 2:
        echo  "<h2>" . $group2 . "</h2>" . isEven($optSplit[1]) . "<br><button onclick='window.history.back()'>Start Over</button>";
        break;
    case 3:
        echo  "<h2>" . $group3 . "</h2>" . isEven($optSplit[1]) . "<br><button onclick='window.history.back()'>Start Over</button>" ;
        break;
    case 4:
        echo  "<h2>" . $group4 . "</h2>" . isEven($optSplit[1]) . "<br><button onclick='window.history.back()'>Start Over</button>";
        break;
    case 5:
        echo  "<h2>" . $group5 . "</h2>" . isEven($optSplit[1]) . "<br><button onclick='window.history.back()'>Start Over</button>";
        break;
    case 6:
        echo  "<h2>" . $group6 . "</h2>" . isEven($optSplit[1]) . "<br><button onclick='window.history.back()'>Start Over</button>";
        break;
    case 7:
        echo  "<h2>" . $group7 . "</h2>" . isEven($optSplit[1]) . "<br><button onclick='window.history.back()'>Start Over</button>";
        break;
    case 8:
        echo  "<h2>" . $group8 . "</h2>" . isEven($optSplit[1]) . "<br><button onclick='window.history.back()'>Start Over</button>";
        break;
    case 9:
        echo  "<h2>" . $group9 . "</h2>" . isEven($optSplit[1]) . "<br><button onclick='window.history.back()'>Start Over</button>";
        break;
    case 10:
        echo  "<h2>" . $group10 . "</h2>" . isEven($optSplit[1]). "<br><button onclick='window.history.back()'>Start Over</button>" ;
        break;
    case 11:
        echo  "<h2>" . $group11 . "</h2>" . isEven($optSplit[1]) . "<br><button onclick='window.history.back()'>Start Over</button>";
        break;
    case 12:
        echo  "<h2>" . $group12 . "</h2>" . isEven($optSplit[1]) . "<br><button onclick='window.history.back()'>Start Over</button>";
        break;
}

显示摘要后,我想将其作为电子邮件发送。由于有多种情况,我需要使交换机功能可重复用于电子邮件。我已经尝试将它存储在一个变量和一个带有值的函数中,但它没有用。

有没有办法在不复制粘贴的情况下多次调用此开关功能?

谢谢。

2 个答案:

答案 0 :(得分:1)

我自己做了,问题是必须在foreach之前声明函数。

<?php



$answers = array();
function isEven($number){
    if ($number % 2 == 0) {
      return "No";
    } else {
        return "Yes";
    }
}


function showSummary(){
$group1 = "q1 ?";
$group2 = "q1 ?";
$group3 = "q1 ?";
$group4 = "q1 ?";
$group5 = "q1 ?";
$group6 = "q1 ?";
$group7 = "Does it archive when Archive is clicked ?";
$group8 = "q1 ?";
$group9 = "Is an Outlook archiving dialogue open ?";
$group10 = "Is Outlook still respnonding ?";
$group11 = "Does it happen all the time ?";
$group12 = "Do you get an error message ?"; 

foreach ($_POST as $name => $value) {


    $groupSplit = str_split($name, 5);
//  print_r("Group Num: " . $groupSplit[1] . "<br/>");

    $optSplit = str_split($value, 3); // splits opt and the num x 
//  print_r("Option Num " . $optSplit[1] . "<br/>");



    switch ($groupSplit[1]) {
    case 1:
        echo  "<h2>" . $group1 . "</h2>" . isEven($optSplit[1] . "<br><button onclick='window.history.back()'>Start Over</button><button>Confirm and Send</button>") ;
        break;
    case 2:
        echo  "<h2>" . $group2 . "</h2>" . isEven($optSplit[1]) . "<br><button onclick='window.history.back()'>Start Over</button>";
        break;
    case 3:
        echo  "<h2>" . $group3 . "</h2>" . isEven($optSplit[1]) . "<br><button onclick='window.history.back()'>Start Over</button>" ;
        break;
    case 4:
        echo  "<h2>" . $group4 . "</h2>" . isEven($optSplit[1]) . "<br><button onclick='window.history.back()'>Start Over</button>";
        break;
    case 5:
        echo  "<h2>" . $group5 . "</h2>" . isEven($optSplit[1]) . "<br><button onclick='window.history.back()'>Start Over</button>";
        break;
    case 6:
        echo  "<h2>" . $group6 . "</h2>" . isEven($optSplit[1]) . "<br><button onclick='window.history.back()'>Start Over</button>";
        break;
    case 7:
        echo  "<h2>" . $group7 . "</h2>" . isEven($optSplit[1]) . "<br><button onclick='window.history.back()'>Start Over</button>";
        break;
    case 8:
        echo  "<h2>" . $group8 . "</h2>" . isEven($optSplit[1]) . "<br><button onclick='window.history.back()'>Start Over</button>";
        break;
    case 9:
        echo  "<h2>" . $group9 . "</h2>" . isEven($optSplit[1]) . "<br><button onclick='window.history.back()'>Start Over</button>";
        break;
    case 10:
        echo  "<h2>" . $group10 . "</h2>" . isEven($optSplit[1]). "<br><button onclick='window.history.back()'>Start Over</button>" ;
        break;
    case 11:
        echo  "<h2>" . $group11 . "</h2>" . isEven($optSplit[1]) . "<br><button onclick='window.history.back()'>Start Over</button>";
        break;
    case 12:
        echo  "<h2>" . $group12 . "</h2>" . isEven($optSplit[1]) . "<br><button onclick='window.history.back()'>Start Over</button>";
        break;
}
}






}showSummary();

showSummary();
?>

答案 1 :(得分:-1)

我需要更多信息来真正描述该功能,但以下代码至少更简洁:

<?php

    $url = 'http://myincludeserver.com/folder/fileiwanttoinclude.php';

    function get_data($url) 
    {
      $ch = curl_init($url);
      $timeout = 5;
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $_REQUEST);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
      $data = curl_exec($ch);
      curl_close($ch);

      return $data;
    }

    $returned_content = get_data($url);

    if(!empty($returned_content))
    {
      echo $returned_content;
    }
    else 
    {
      include('includes/local_error_message.php');  
    };

  ?>