dongquechan4414 2014-01-26 09:10
浏览 45
已采纳

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

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.

  • 写回答

3条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 在线教育培训平台,主要以点播视频和在线练习测试为主,除将视频做切片外,有其他哪种方式降低流量?(关键词-带宽速率)
      • ¥20 用c语音或c++实现银行叫号系统
      • ¥15 人工智能 规则正向演绎和推理
      • ¥20 基于STM32F401的电子密码锁设计
      • ¥15 famamacbeth回归中遇到only size-1 arrays can be converted to Python scalars,求解答
      • ¥15 单片机多个自锁按键的编程实践
      • ¥15 用python操作redis存储中文后,再取出的数据变成了乱码怎么办?
      • ¥15 C语言简单排序问题有偿求解
      • ¥20 请问图片的代码什么意思
      • ¥15 coq问题求带,有偿