测试foreach循环中每个数组值的条件

时间:2014-01-26 09:10:04

标签: php foreach

我可能已经提出了一个不太正确的问题标题,为此道歉。我在数组中发送一些值以foreach循环。现在,用例是这些是与教学责任相关的一些ID,我需要将它们分组到更高的分组责任中。但条件是这些id必须是相同的纸张类型。

现在,我认为我需要做的是检查每个数组值的纸张类型,如果它们不相同,那么我需要退出循环并给出错误。

注意这是一个AJAX请求。

到目前为止我的代码循环遍历数组:

$tid = mysql_real_escape_string($_POST['tid']);

$resp = $_POST['respid'];

$name_id = array_values($resp)[0];

$q1 = mysql_query("select p.p_name from papers p, iars ir where ir.id='$name_id' and ir.paperid = p.p_id");

$rows1 = mysql_fetch_array($q1);
$gresp_name = $rows1['p_name'];

$q2 = mysql_query("insert into giars set sessionid='$session', teacherid='$tid', name='$gresp_name'");
if(mysql_affected_rows()>0){
$gresp_id = mysql_insert_id();
}

foreach ($resp as $value ) {

   $query = mysql_query("select p.ptype from papers p, iars ir where p.p_id = ir.paperid and ir.id='$value'");

   $rows=mysql_fetch_array($query);
   $ptype=$rows['ptype'];

  // I am stuck here //

$q1 = mysql_query("insert into grp_resp(giars_id, iars_id, courseid, semester, paperid, groupid) select '$gresp_id', '$value', courseid, semester, PaperId, groupid from iars where id='$value'");

}
echo "done";

现在,如果条件失败,我如何退出循环并为AJAX请求提供适当的响应。

3 个答案:

答案 0 :(得分:1)

你只需要循环两次 请考虑以下代码:

$paper_type = null; // we'll be storing the prototype here (all should match it)
$error = false;     // no errors as for now

foreach ($resp as $value) 
{
   $query = mysql_query("select p.ptype from papers p, iars ir where p.p_id = ir.paperid and ir.id='$value'");
   $rows = mysql_fetch_array($query);
   $ptype = $rows['ptype'];
   if($paper_type === null) $paper_type = $ptype; // initializing the "prototype"
   if($paper_type != $ptype) { $error = true; break; } // if it doesn't match the prototype - throw an error
}

// Displaying an error for AJAX
if($error) { echo "ERROR"; exit; }

// Otherwise - everything is fine, let's do the inserts!
foreach ($resp as $value) 
{
    $q1 = mysql_query("insert into grp_resp(giars_id, iars_id, courseid, semester, paperid, groupid) select '$gresp_id', '$value', courseid, semester, PaperId, groupid from iars where id='$value'");
}

答案 1 :(得分:0)

你可以尝试类似下面的内容

$array_ids = array(1, 2 …);#array values you are looking for
foreach($resp as $value){
   $query = mysql_query("select p.ptype from papers p, iars ir where p.p_id = ir.paperid and ir.id = '$value'");
   $rows = mysql_fetch_array($query);
   $ptype = $rows['ptype'];
   if(in_array($ptype, $array_ids)){
       #do your error stuff or what not
       break;
   }
   #or do
   if(in_array($ptype, $resp)){
       #do your error stuff or what not
       break;
   }
  $q1 = mysql_query("insert into grp_resp(giars_id, iars_id, courseid, semester, paperid, groupid) select '$gresp_id', '$value', courseid, semester, PaperId, groupid from iars where id = '$value'");

}

OR

$array_ids = array();
foreach($resp as $value){
   $query = mysql_query("select p.ptype from papers p, iars ir where p.p_id = ir.paperid and ir.id='$value'");
   $rows=mysql_fetch_array($query);
   $array_ids[] = $rows['ptype'];
}
foreach($resp as $value){
    if(in_array($value, $array_ids)){
        #do your error stuff or what not
        break; #or continue; if you don't want to break out of loop
    }
    $q1 = mysql_query("insert into grp_resp(giars_id, iars_id, courseid, semester, paperid, groupid) select '$gresp_id', '$value', courseid, semester, PaperId, groupid from iars where id='$value'");
    $rows=mysql_fetch_array($query);
}

或者您可以查看是否无法更新查询以消除值

答案 2 :(得分:0)

我认为这对您有用,这是如何使用contiune进行更多信息检查 http://www.php.net/manual/en/control-structures.continue.php

$ptype=$rows['ptype'];
  If($ptype == 12 ) {
    continue;
   }else{ 
   do_something
   }