PHP Quizzes, PHP exercises
A few days ago in Baidu know inside see a netizen asked how to make a small program: in PHP language design a small program, calculate today to the number of days to the next month, all output these days, and make daily date in three colors cycle display, set three forms, let the user choose font Color, The three colors are then looped out to reflect those dates on the output . Today just have time to make it out for him, this actually does not have any practical use, but novice friends can be as a PHP small exercise program, with him to familiarize or deepen the knowledge of PHP!
At that time I gave him a general idea as follows:
1, calculate the number of days to the next month, you can use two arrays an array to access the month of 30 days, an array access to 31 days of the month (of course, there are 29, 28 days, this can be studied carefully, above is just an example)! Then get the month of the day to see in which array the total number of days in the current month is determined, and then you can output all days from the current day to the end of the month!
2, let the user choose three kinds of font color, I think a form on the line, with a checkbox to let users choose the three favorite colors!
3, loop output three kinds of colors, I think it can!
123456789 |
//$color 是存储三种颜色的数组
//$date 是存储所有天数的数组
$i = 0;
$content =
''
;
foreach (
$date as $value
) {
$content .=
'.
$color
[
$i
].
'">'
.
$value
.
''
;
$i ++;
if (
$i > 2)
$i = 0;
}
|
This $content contains all the days and colors of the style!
The following is the specific code that I have written out of the PHP small exercise project, we can see, the wrong place please correct me!
First, the HTML part of the code, let the user choose the color (no specified how many kinds)
12345678 |
<
form method
=
"post"
>
<
input type
=
"checkbox" value
=
"red" name
=
"color[]"
/> 红
<
input type
=
"checkbox" value
=
"black" name
=
"color[]"
/> 黑
<
input type
=
"checkbox" value
=
"blue" name
=
"color[]"
/> 蓝
<
input type
=
"checkbox" value
=
"yellow" name
=
"color[]"
/> 黄
<
input type
=
"checkbox" value
=
"green" name
=
"color[]"
/> 绿
<
input type
=
"submit" value
=
"提交" name
=
"send"
/>
form >
|
Then the PHP backend code
12345678910111213141516171819202122232425262728293031 |
if (isset(
$_POST
[
'send'
])) {
//获取颜色
$color =
$_POST
[
'color'
];
//计算天数
$date =
date
(
'Y-m-d'
, time());
$dates =
explode
(
'-'
,
$date
);
$sy =
array
(
'1'
,
'3'
,
'5'
,
'7'
,
'8'
,
'10'
,
'12'
);
$ss =
array
(
'4'
,
'6'
,
'9'
,
'11'
);
//判断
if (in_array(
$dates
[1],
$sy
)) {
$day = range(
$dates
[2], 31);
}
elseif (in_array(
$dates
[1],
$ss
)) {
$day = range(
$dates
[2], 30);
}
else {
if ((
$dates
[0]%4 == 0 &&
$dates
[0]%100 != 0)
|| (
$dates
[0]%100 == 0 &&
$dates
[0]%400 == 0)) {
$day = range(
$dates
[2], 29);
}
else {
$day = range(
$dates
[2], 28);
}
}
//将天数与颜色搭配
$i = 0;
$content =
''
;
foreach (
$day as $value
) {
$content .=
'.
$color
[
$i
].
'">'
.
$value
.
' '
;
$i ++;
if (
$i > (
count
(
$color
)-1))
$i = 0;
}
echo $content
;
|
So $content can output the desired effect!
Want to get the updated content of Wang Yip's personal blog in time every day? Quickly add the public number "LY89CN", or scan the QR code below!
This article originates from the personal blog of Wang Ye Building, this article address: http://www.ly89.cn/detailB/64.html
Welcome to share this article, reprint please indicate the source and address of this article
http://www.bkjia.com/PHPjc/1071498.html www.bkjia.com true http://www.bkjia.com/PHPjc/1071498.html techarticle PHP Quizzes, PHP exercises a few days ago in Baidu know inside see a netizen asked how to make a small program: in PHP language design a small program, calculate today to the next month ...