0

I might have put a not so right title of the question, apologies for that. I am sending some values in an array to be looped by foreach. Now, the use-case is that these are some id(s) related to a teaching responsibility and i am required to group them in a higher grouped responsibility. But the condition is that these id(s) have to be of same paper type.

Now, what i think i need to do in order to achieve this is to check the paper type for each array value and if they are not the same then i need to get out of the loop and give an error.

Note This is an AJAX request.

my code so far for looping through the array is:

$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";

Now, how do i get out of the loop if the condition fails and give an appropriate response for the AJAX request.

2
  • break or continue Commented Jan 26, 2014 at 9:12
  • @Class the type i need to check id of all the values in the array which will be only determined inside the loop so how do i continue with that? Please, if you could provide some more explanation Commented Jan 26, 2014 at 9:15

3 Answers 3

1

You just have to loop it twice.
Consider the following code:

$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'");
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think this would work for you this is how to use contiune for more info check http://www.php.net/manual/en/control-structures.continue.php

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

Comments

0

you can try something like the following

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

OR you can see if you can't update your query to eliminate values

3 Comments

@coder101 by the looks of it you need a better query/database scheme
if by that u mean, i need to stop using mysql extension of php, then the reason is that this is an old project and i soon will have to transform the whole code base to mysqli.
@coder101 I'm talking about adding to the WHERE (or other parts) clause so you only return the values you need or change something about your database's layout where you normalize it better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.