dongyong2906 2013-01-16 08:20
浏览 49
已采纳

在while循环中更改id后添加新元素

I have while loop to get information from mysql table. But can't figure how to find las entry containing same factor_id and add append new element after it.

    if ($this->_db->numRows() > 0) {
        $i = 1;
        $j = '';
        $row = '';
        while ($row = mysqli_fetch_assoc($res_info)) {

            if ($row['task_factor_category'] != $j) {
                $title = '<tr><td colspan="4" class="title" style="text-align: left;">' . $row['task_factor_category'] . '</td></tr>';
                $comment = '<tr><td class="title" style="text-align: left" colspan="4">' . $this->_glan['comments'] . '</td></tr><tr><td colspan="4"><textarea cols="82" rows="3" name="factor_comment[' . $row['factor_id'] . ']" id="' . $row['factor_id'] . '"></textarea></td></tr>';
            } else {
                $title = false;
                $comment = false;
            }
            $this->reg['edit_fields']['factors'] .= $title . '<tr>
        <td>
        ' . $row['factors'] . '
        </td>
        <td>
        <input type="radio" name="task_factor[' . $i . '_' . $row['factor_id'] . ']" value="1"  />
        </td>
        <td>
        <input type="radio" name="task_factor[' . $i . '_' . $row['factor_id'] . ']" value="2" checked />
        </td>
        <td>
        <input type="radio" name="task_factor[' . $i . '_' . $row['factor_id'] . ']" value="3" />
        </td>
        </tr>
        ';
            $j = $row['task_factor_category'];
            $i++;
        }
    }

output is like:

         //results with $row['factor_id'] = 10;
title
    1. entry
    2. entry
    3. entry
    4. entry
    5. entry
  //need new element here

  //results with $row['factor_id'] = 11;
 title
    1. entry
    2. entry
    3. entry
    4. entry
    5. entry
   //need new element here

    ......

I need to add additional element after the last entry containing same factor id

  • 写回答

2条回答 默认 最新

  • doucong1853 2013-01-16 09:19
    关注

    You may want to try this one.

    $id = array();
    $i = 0;
    
    while ($row = mysqli_fetch_assoc($res_info))
    {
        $id[$i] = $row['id'];
    
        if ($i != 0 && $id[$i-1] != $row['id']) {
            //new element
        } else {
            //same element
        }
        $i++
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊