2014-01-26 109 views
0

我可能會提出一個不太合適的問題標題,對此表示歉意。我發送數組中的一些值由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

回答

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

真棒。謝謝你... – coder101

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

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

+0

謝謝,但@ oleg的答案爲我工作.. – coder101

+0

@ coder101由它的外觀你需要一個更好的查詢/數據庫計劃 – Class

+0

如果由你的意思,我需要停止使用'mysql'的擴展名PHP,那麼原因是這是一箇舊的項目,我很快就必須將整個代碼庫轉換爲'mysqli'。 – coder101