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

问题描述:

我可能会提出一个不太合适的问题标题,对此表示歉意。我发送数组中的一些值由foreach循环。现在,使用案例是这些是与教学责任有关的一些身份证,我需要将他们归类为更高级别的责任。但条件是这些id必须是相同的纸张类型。在foreach循环中测试每个数组值的条件

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

注意这是一个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请求作出适当的反应。

+0

'break'或'continue' – Class

+0

@class我需要检查所有的阵列中,将里面只能确定的值的ID类型循环,那么我该如何继续呢?请,如果你能提供更多的解释 – coder101

你只需循环两次。
考虑下面的代码:

$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'"); 
} 
+0

真棒。谢谢你... – coder101

你可以尝试像下面这样

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

或者你可以看到,如果你无法更新您的查询,以消除值

+0

谢谢,但@ oleg的答案为我工作.. – coder101

+0

@ coder101由它的外观你需要一个更好的查询/数据库计划 – Class

+0

如果由你的意思,我需要停止使用'mysql'的扩展名PHP,那么原因是这是一个旧的项目,我很快就必须将整个代码库转换为'mysqli'。 – coder101

我认为这将适用于y瓯这是如何使用contiune更多信息检查 http://www.php.net/manual/en/control-structures.continue.php

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