请帮助如何将单个php文件重写为MVC格式? [英] Please help how to rewrite a single php file into a MVC format?
问题描述
朋友,
请帮我用MVC格式重写一下。它目前编码在一个PHP文件中。下面的代码将显示一年中剩余月份的列表,月份事件计数在该月份旁边(见下文)。
网页显示
七月(2)
八月(2)
九月(1)
十月(0)
十一月(6)
十二月(2)
PHP编码
Friends,
Please help me to rewrite this in MVC format only. It is presently coded in a single PHP file. Below are the codes that will display list of the remaining month of the year with monthly event count beside the month (see below).
WEB PAGE DISPLAY
July (2)
August (2)
September (1)
October (0)
November (6)
December (2)
PHP CODINGS
<?php
require_once ('../model/database.php');
//this function will provide all events count per month for this year
//the current year will be sent as parameter
function get_eventsCount($year) {
global $db;
$query = 'SELECT MONTH(E.dateStart) AS month, COUNT(*)
AS nbEvents FROM events E
WHERE YEAR(E.dateStart) = :year
GROUP BY MONTH(E.dateStart)';
$data = array();
try {
$statement = $db->prepare($query);
$statement->bindValue(':year', $year);
$statement->execute();
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
$data[$row['month']] = $row['nbEvents'];
}
$statement->closeCursor();
//we will get something like
/*
Array (
7 => 2,
8 => 2,
10 => 1,
11 => 1,
12 => 1
)
*/
return $data;
} catch (PDOException $e) {
display_db_error($e->getMessage());
}
}
// get the number of events for a specific month
function getNbEventsForMonth($m, $data) {
$nb = 0;
if (is_array($data) && isset($data[$m])) {
$nb = $data[$m];
}
return $nb;
}
//get all events number per month for the current year (date('Y'))
$data = get_eventsCount(date('Y'));
//get the timestamp for the first day of the current month / which will be our start month
$timeStamp = strtotime('first day of this month');
//get the month number (for the current month)
$m = date('n', $timeStamp);
//display with using of the getNbEventsForMonth() fucntion
echo '<p><a href="yourlink">'.date('F', $timeStamp).' ('.getNbEventsForMonth($m, $data).')</a></p>';
//while we do not reach the end of the year
while ($m < 12) {
//same as above / just we want to get here even the missing month (for example september)
$timeStamp = strtotime('next month', $timeStamp);
$m = date('n', $timeStamp);
echo '<p><a href="yourlink">'.date('F', $timeStamp).' ('.getNbEventsForMonth($m, $data).')</a></p>';
}
//the date('F') allow us to get the full month name
?>
请帮助我如何重写 php echo如果它将被合并到html (特别提到)。
我知道所有功能都将放在模型文件中。
非常感谢提前。
espi62
Please help me how to rewrite the php echo if it will be incorporated in html (specially mentioned).
I understand all functions will be placed in the model file.
Many thanks in advance.
espi62
推荐答案
year){
global
year) { global
db < /跨度>;
db;
query = ' SELECT MONTH(E.dateStart)AS月,COUNT(*)
AS nbEvents FROM events E
WHERE YEAR(E.dateStart)=:year
GROUP BY MONTH (E.dateStart)'跨度>;
query = 'SELECT MONTH(E.dateStart) AS month, COUNT(*) AS nbEvents FROM events E WHERE YEAR(E.dateStart) = :year GROUP BY MONTH(E.dateStart)';
这篇关于请帮助如何将单个php文件重写为MVC格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!