日历应用程序 - 输出该月的天数

发布于 2024-11-03 04:04:46 字数 3482 浏览 2 评论 0原文

我正在开发日历/计划应用程序,我需要一些建议。

我正在处理我的应用程序的以下部分:

本月的天数

它显示了该月从 1 到 1 的天数月底,28/29、30 或 31。我实现了这一目标.. (这里),但我的代码非常丑陋,我确信一定有另一种方法可以做到这一点。

我在 CodeIgniter 工作。我的控制器包含以下函数,用于用一个月中的日期填充列表:

    public function init_days()
{
            // post values? in case of previous/next months (ajax)
    if($this->input->post('post_month') && $this->input->post('post_year'))
    {
        $month = $this->input->post('post_month');
        $year = $this->input->post('post_year');
        $data = $this->planner_model->calendar_data($month, $year);
    }
    else
    {
        $data = $this->planner_model->calendar_data();
    }

    // init empty calendar
    $data['calendar'] = '';

    // easy var names
    $current_month = $data['current_month'];
    $current_year = $data['current_year'];

    // echo list into $data['calendar']
    for($i = 1; $i <= $data['days_in_month']; $i++)
    {
        if($current_month == date('n') && $current_year == date('Y'))
        {
            if($i < $data['current_day_of_month'])
            {
                $data['calendar'] .= "<li class='prev_month' value='$i'>$i</li>";
            }
            if($i == $data['current_day_of_month'])
            {
                $data['calendar'] .= "<li class='today' value='$i'>$i</li>";
            }
            if($i > $data['current_day_of_month'])
            {
                $data['calendar'] .= "<li class='next_month' value='$i'>$i</li>";
            }
        }
        if( ($current_month > date('n') && $current_year == date('Y')) || ($current_year > date('Y')) )
        {
            $data['calendar'] .= "<li class='next_month' value='$i'>$i</li>";
        }
        if( ($current_month < date('n') && $current_year == date('Y')) || ($current_year < date('Y')) )
        {
            $data['calendar'] .= "<li class='prev_month' value='$i'>$i</li>";
        }
    }
    $data['month_name'] = ucfirst($this->get_month_name($current_month));

    header('Content-type: application/json');
    echo json_encode($data);
}

我的模型返回控制器调用的 $data 数组(在 else 子句中,第一部分):

    public function calendar_data($month = '', $year = '')
{
    if( ! empty($month) && ! empty($year))
    {
        $data['current_year'] = $year;
        $data['current_month'] = $month;
        $data['current_day_of_month'] = date('j');
        $data['current_day_of_week'] = date('w');
        $data['days_in_month'] = cal_days_in_month(CAL_GREGORIAN, $month, $year);
    }
    else
    {
        $data['current_year'] = date('Y');
        $data['current_month'] = date('n');
        $data['current_day_of_month'] = date('j');
        $data['current_day_of_week'] = date('w');
        $data['days_in_month'] = cal_days_in_month(CAL_GREGORIAN, $data['current_month'], $data['current_year']);
    }


    return $data;
}

然后我使用 AJAX 在我的视图中输出它调用 $(document).ready。

$("#day_list").html(data['calendar']).fadeIn();

我对代码不满意。这是一团糟,而且我相当确定我在这里破坏了 MVC;我不是吗?有人可以就如何以“更好”的方式做到这一点提供一些建议或见解吗?多谢。

完整来源位于此处,以防有人愿意查看它并告诉我是否还有其他内容我做错的事情。

I'm working on a calendar/planner application and I need some advice.

I'm working on the following part of my application:

days of the month

It shows the days of the month from 1 till the end of the month, 28/29, 30 or 31. I achieved this.. (here) but my code is extremely ugly and I'm sure there must be another way to do this.

I'm working in CodeIgniter. My controller contains the following function to populate the list with the days of the month:

    public function init_days()
{
            // post values? in case of previous/next months (ajax)
    if($this->input->post('post_month') && $this->input->post('post_year'))
    {
        $month = $this->input->post('post_month');
        $year = $this->input->post('post_year');
        $data = $this->planner_model->calendar_data($month, $year);
    }
    else
    {
        $data = $this->planner_model->calendar_data();
    }

    // init empty calendar
    $data['calendar'] = '';

    // easy var names
    $current_month = $data['current_month'];
    $current_year = $data['current_year'];

    // echo list into $data['calendar']
    for($i = 1; $i <= $data['days_in_month']; $i++)
    {
        if($current_month == date('n') && $current_year == date('Y'))
        {
            if($i < $data['current_day_of_month'])
            {
                $data['calendar'] .= "<li class='prev_month' value='$i'>$i</li>";
            }
            if($i == $data['current_day_of_month'])
            {
                $data['calendar'] .= "<li class='today' value='$i'>$i</li>";
            }
            if($i > $data['current_day_of_month'])
            {
                $data['calendar'] .= "<li class='next_month' value='$i'>$i</li>";
            }
        }
        if( ($current_month > date('n') && $current_year == date('Y')) || ($current_year > date('Y')) )
        {
            $data['calendar'] .= "<li class='next_month' value='$i'>$i</li>";
        }
        if( ($current_month < date('n') && $current_year == date('Y')) || ($current_year < date('Y')) )
        {
            $data['calendar'] .= "<li class='prev_month' value='$i'>$i</li>";
        }
    }
    $data['month_name'] = ucfirst($this->get_month_name($current_month));

    header('Content-type: application/json');
    echo json_encode($data);
}

My model returns the $data array that gets called by the controller (in the else clause, first part):

    public function calendar_data($month = '', $year = '')
{
    if( ! empty($month) && ! empty($year))
    {
        $data['current_year'] = $year;
        $data['current_month'] = $month;
        $data['current_day_of_month'] = date('j');
        $data['current_day_of_week'] = date('w');
        $data['days_in_month'] = cal_days_in_month(CAL_GREGORIAN, $month, $year);
    }
    else
    {
        $data['current_year'] = date('Y');
        $data['current_month'] = date('n');
        $data['current_day_of_month'] = date('j');
        $data['current_day_of_week'] = date('w');
        $data['days_in_month'] = cal_days_in_month(CAL_GREGORIAN, $data['current_month'], $data['current_year']);
    }


    return $data;
}

I then output this in my view with an AJAX call on $(document).ready.

$("#day_list").html(data['calendar']).fadeIn();

I'm not happy with the code. It's a mess, and I'm fairly sure I'm breaking MVC here; aren't I? Could someone perhaps give some advice or insights on how to do this in a 'better' way? Thanks a lot.

The full source is here in case anyone would be kind enough to look through it and tell me if there's other stuff I'm doing wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守不住的情 2024-11-10 04:04:46

MVC 和 Web 的问题是你永远无法真正在视图和控制器之间有清晰的界限。这就是网络的固有本质。我更喜欢 CakePHP,但原理是一样的。当我编写代码时,我总是问自己一些问题,以了解放置代码的最佳位置。

  1. 我是检索、存储还是
    操纵数据? (如果是这样的话,那就是
    模型)
  2. 我是在显示数据还是在呈现
    向最终用户提供数据? (如果是这样的话,那就是
    看法)。
  3. 其他一切都进入我的
    控制器。

话虽如此,只要快速查看一下您的代码,您就会发现您正在控制器中组合模型代码和视图代码,这就是您对此感到不安的原因。以下是一些可能对您有所帮助的简单逻辑:

将构建日期列表的所有代码移至模型中。称之为:

function create_date_list() {
  // code here
}

这只会构建一个您想要显示的日期的数组。请记住,您还可以在数组上传递一些内容,例如日期是否是假期、当天、周末等。这将帮助您在视图中确定如何格式化日期,而无需向视图添加代码这样做。例如,您可能有一个从模型返回的数组,如下所示:

[dates] {
  [22] {
     [day] => [Friday]
     [type] => [weekday]
  }
  [23] {
     [day] => [Saturday]
     [type] => [Weekend]
  }
  [24] {
     [day] => [Sunday]
     [type] => [Weekend]
     [Holiday] => [Easter]
  }
}

这可能是多余的,也可能不是。但我用这个例子来表明模型是你完成所有这些繁重工作的地方,而不是控制器或视图。

接下来,构建视图。我不确定它在 CodeIgniter 中是什么,但在蛋糕中它们被称为 Elements。这些是可用于任何视图(可重用视图元素)的视图代码片段。因此,构建一个可重用的视图元素,它将获取一个日期数组,循环遍历它,并以 HTML 格式编写输出。请注意我如何使用日期数组来格式化我的视图。

<ul>
<?php foreach($dates as $date): ?>
  <li class="date <?php echo $dates[$date]['type']; ?>><?php echo $date; ?></li>
<?php endforeach; ?>
</ul>

漂亮、干净、简洁。优点是如果您想改变外观,则不必触摸控制器。 ;)

现在,对于控制器。将代码放入从模型调用日期数组的控制器中,并将其传递到视图。这个想法是 FAT 模型,SKINNY 视图。

function my_function() {
  // get code from model
  // set code for view
  // render view
}

虽然我还没有真正“清理”你的代码。我希望这能为您提供有关如何移动代码的指导,以便 a) 对您来说更有意义,b) 您可以以仍然符合您的 MVC 架构的方式清理代码。正在努力争取。

祝你好运,编码愉快!

The problem with MVC and the web is you can never really have clear delineation between the view and the controller. That's just the inherent nature of the web. I am more of a CakePHP guy, but the principles are the same. When I write my code, I always ask myself a handful of questions to know the best place to put my code.

  1. Am I retrieving, storing, or
    manipulating data? (if so, it's
    model)
  2. Am I displaying data or presenting
    data to the end-user? (if so, it's
    view).
  3. Everything else goes into my
    controller.

That being said, with only taking a quick look at your code, it appears you are combing model code and view code in the controller, and that is why you feel uneasy about it. Here is some simple logic that may help you:

Move all of the code that builds the list of dates into the model. Call it something like:

function create_date_list() {
  // code here
}

This will only build an array of the dates you want to display. Keep in mind you can also pass things on the array like whether or not the date is a holiday, current day, weekend, etc. This will help you determine in the view how to format the date without having to add code to the view to do so. So for example, you may have an array that returned from the model that looks like this:

[dates] {
  [22] {
     [day] => [Friday]
     [type] => [weekday]
  }
  [23] {
     [day] => [Saturday]
     [type] => [Weekend]
  }
  [24] {
     [day] => [Sunday]
     [type] => [Weekend]
     [Holiday] => [Easter]
  }
}

This may be overkill or not. But I used this example to show that the MODEL is where you do all of this heavy lifting, not the controller or the view.

Next, build the view. I am not sure what it is in CodeIgniter, but in cake they are called Elements. These are snippets of view code that are available to any view (reusable view elements). So build a reusable view element that will take a date array, loop through it, and write the output in HTML. Notice how I can use the date array to format my view.

<ul>
<?php foreach($dates as $date): ?>
  <li class="date <?php echo $dates[$date]['type']; ?>><?php echo $date; ?></li>
<?php endforeach; ?>
</ul>

Nice, clean, and concise. The advantage is if you ever want to change the look, you do not have to touch the controller. ;)

Now, for the controller. Put the code in the controller that calls the date array from the model, and passes it to the view. The idea is FAT model, SKINNY view.

function my_function() {
  // get code from model
  // set code for view
  // render view
}

While I haven't really "cleaned up" your code. I hope this gives you the guidance you were looking for on how to move the code around yourself so that a) it makes more sense to you and b) you can clean up the code in a way that will still conform to the MVC architecture you are striving for.

Good luck and Happy Coding!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文