duanjiong5686 2011-06-14 21:59
浏览 35
已采纳

如何在没有重叠的情况下遍历一系列复选框和MySQL数据库?

for($j = 0; $j < $length; $j++){
    while($answersRow = mysql_fetch_row($fetch)){
        if($answersRow[0] == $curr_answer_id[$j]){ 

            echo "<input type='checkbox' value='$answersRow[0]' checked>$answersRow[1]</input> ";           
        } else {

            echo "<input type='checkbox' value='$answersRow[0]'>$answersRow[1]</input> ";           
        }
    }
}

Assuming $fetch is my result, here's my dilemma. The for loop represents the array of checkboxes. The first loop around, everything works fine. However, any more than that, we no longer cycle through the while loop because of the internal mysql pointer. I know I can move the pointer around using mysql_data_seek(), but don't have an idea on how to do so usefully. If I move it back to 0, then it just outputs everything for as many things that were checked.

I basically want to traverse through for each question through the database but without any overlap. It is a bit hard to explain so I apologize if I am not explaining this simple problem properly; I'll try and clarify if need be.

  • 写回答

1条回答 默认 最新

  • dongnaigu2052 2011-06-14 22:01
    关注

    You can save the results of your first while in an array:

    //> FIRST LOOP
      while($row = mysql_fetch_assoc($query)) {
        $rows[] = $row; //> Saving in the $rows array
        //> All your code from your first loop
      }
    
    //> SECOND LOOP    
      foreach($rows as $v) {
       //> All your code you need for your second loop without any additional query
      }
    

    Also sorry If I misunderstood your question but It is not so clear what you are asking for

    Addendum

    After reading better your question if I understood correctly you want to achive everything within only one loop. In this case you can save the html you need in the first while like this:

    $firstLoop = '';
    $secondLoop = '';
    while() {
       $firstLoop .= '<option> [...]';    
       $secondLoop .= '<div> [...]';
    }
    

    At this point with one loop you have built the html you need and they are in $firstLoop and $secondLoop. This is of course faster then any other solutions

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?