2014-01-26 1 views
0

질문의 제목이 잘못되었을 수 있습니다. 사과드립니다. foreach에 루프되도록 배열에 값을 보냅니다. 이제, 유스 케이스는 이들이 교수 책임과 관련된 일부 ID이며 더 높은 그룹의 책임으로 그룹화해야한다는 것입니다. 그러나 조건은이 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'을 확인 대한 추가 정보를 원하시면 contiune 사용하는 방법이다는 나'내가 단지 내에서 결정됩니다 배열의 모든 값의 ID를 확인해야 유형 @class – Class

+0

을 CONTINUE ' 루프 어떻게 그렇게 계속합니까? 제발, 좀 더 설명해 줄 수 있다면 – 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

AWESOME. 고마워요 ... – 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'"); 

} 

또는

$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

관련 문제