dpa84373 2012-07-10 23:23
浏览 29
已采纳

如何防止我的算法迭代并回显已存在的值?

I'm trying to dynamically create 5 divs that represent 5 days of the week. In each div, I'm trying to loop through data fetched from my code igniter model and echo a series of un-ordered list based upon a project, a date, and tasks that are related to that project. I'm fetching the data from both my project table, to echo the project name in h4 tags, and from the task table, to echo related task under the project h4. The problem I'm having is that I can't seem to find a way to prevent my algorithm from repeating project names and related tasks. In other words, if there's two tasks related to one project, that project and the two related tasks will be echoed out twice. Here's an image to help explain the problem:

enter image description here

    <?php for($counter = 0; $counter < 5; $counter++): ?><!-- loops through and creates divs -->
        <?php 
          $date = date("F j, Y");
          $refDate = date("Y-m-d");
          $counterForLi = 0;
        ?>
                  <div class="span2 check-list date-container">
                    <div id="form-to-date">
                      <div class="replace">

                      <h2 class="day"></h2>
                        <h4 class="thedate">
                          <?php
                            if ($counter > 0) {
                              $tomorrow = mktime(0, 0, 0, date("m"), date("d")+$counter, date("y"));
                              $refDate = date("Y-m-d", $tomorrow);
                              $date = date("F j, Y", $tomorrow);
                              echo $date;
                            } else {
                              echo $date;
                            }
                          ?>
                        </h4>
                        <input type="hidden" name="thedate" class="hidden-date" value="<?php echo $refDate; ?>">

                            <?php if(isset($tasks)) : foreach($tasks as $taskRow) :?> <!-- loop through the tasks -->
                              <?php if($taskRow->dateadded == $refDate) : ?> <!-- in the context of the right date -->
                                <?php if(isset($taskRow->_project_fk)) : ?>
                                    <?php
                                        $relatedProjectId = $taskRow->_project_fk;
                                        $relatedProjectName = NULL;
                                        if(!isset($relatedProjectName)) {
                                            foreach ($projects as $ProjectRow) {
                                                if ($ProjectRow->__project_pk == $relatedProjectId) {
                                                    $relatedProjectName = $ProjectRow->project_name;
                                                    echo "<h4>" . $relatedProjectName . "</h4>";
                                                echo "<ul>";
                                                }
                                            }

                                        foreach ($tasks as $task) {
                                          if ($task->_project_fk == $relatedProjectId && $task->dateadded == $refDate) {
                                            echo "<li>".$task->task_name."</li>";
                                          }

                                        }
                                        echo "</ul>";
                                    } 


                                    ?>

                                <?php endif; ?>

                              <?php endif; ?>

                            <?php endforeach; ?>

                            <?php endif; ?>

                          </ul>
                        </div><!-- /replace -->
                      </div><!-- /form-to-date -->
                    </div>
    <?php endfor; ?>

I realize this code is kind of a mess so if you need clarification, please ask.

Edit: Here's the structure of my tasks array enter image description here

  • 写回答

1条回答 默认 最新

  • duanchou6534 2012-07-10 23:58
    关注

    Start by creating a hierarchical array structure that groups tasks by project and date. From your example and data structure it looks like each task belongs to one project and was started on one date. For each date, you want to show each project that had at least one task that started on that date along with those tasks that started on that date.

    $tasksByProjectAndDate = array();
    foreach ($tasks as $task) {
        $tasksByProjectAndDate[$task->dateadded][$task->_project_fk][] = $task;
    }
    

    Next create a map from project ID (PK) to the project. This makes looking up each project's name faster since you loop over all projects only once.

    $projectsById = array();
    foreach ($projects as $project) {
        $projectsById[$project->__project_pk] = $project;
    }
    

    Finally, you're ready to build your UI. I'll leave fitting it into HTML up to you and just output a simple text view. The logical flow will be identical.

    foreach ($tasksByProjectAndDate as $date => $tasksByProject) {
        echo "$date
    ";
        foreach ($tasksByProject as $projectId => $tasks) {
            $project = $projectsById[$projectId];
            echo "  $project->project_name
    ";
            foreach ($tasks as $task) {
                echo "    $task->task_name
    ";
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分