gpt4 book ai didi

php - 我的 while 循环有问题吗?我的代码语法?是不是太多了?

转载 作者:行者123 更新时间:2023-11-29 20:40:49 27 4
gpt4 key购买 nike

我正在尝试构建一个动态数据库驱动的日历应用程序。我正在执行第 1 步,尝试设置日历。我认为我应该走捷径并制作一个 PHP 脚本将其全部添加到我的数据库中,而不是手动输入所有内容。

问题是,我的脚本涉及很多 while 循环。加载需要很长时间。执行脚本是否需要一段时间,或者某个地方有错误?有没有更简单的方法来制作日历?

编辑:我的所有数据库配置都位于此 block 之前的另一个 block 中。这不是问题所在。

编辑 2:我注意到我的一些 SQL 包含保留关键字。我将它们更改为不同的单词。

这是我的代码:

    <?php


$calsql = "SELECT * FROM callender";

$calquery = mysqli_query($db,$calsql);

$calisfull = mysqli_num_rows($calquery);

if ($calisfull < 1) {
while ($all < 365) {
$year = "2016";
$month = 1;
if ($month == 1){

$dayofmonth = 1;
while ($dayofmonth < 32) {
$d = 0;
while ($d < 7) {
if ($d == 0){
$day = "Friday";
}

else if ($d == 1){
$day = "Saturday";
}

else if ($d == 2){
$day = "Sunday";
}

else if ($d == 3){
$day = "Monday";
}

else if ($d == 4){
$day = "Tuesday";
}

else if ($d == 5){
$day = "Wednesday";
}

else if ($d == 6){
$day = "Thursday";
}

$fillsql = "INSERT INTO callender (month, date, year, day) VALUES ('$month', '$dayofmonth', '$year', '$day')";
$fquery = mysqli_query($db,$fillsql);
$perfectly = "Perfectly";
$dayofmonth++;
$all++;
}

$d = 0;
}
$month++;
}
if ($month == 2){

$dayofmonth = 1;
while ($dayofmonth < 30) {
while ($d < 7) {
if ($d == 0){
$day = "Friday";
}

else if ($d == 1){
$day = "Saturday";
}

else if ($d == 2){
$day = "Sunday";
}

else if ($d == 3){
$day = "Monday";
}

else if ($d == 4){
$day = "Tuesday";
}

else if ($d == 5){
$day = "Wednesday";
}

else if ($d == 6){
$day = "Thursday";
}

$fillsql = "INSERT INTO callender (month, date, year, day) VALUES ('$month', '$dayofmonth', '$year', '$day')";
$fquery = mysqli_query($db,$fillsql);
$perfectly = "Perfectly";
$dayofmonth++;
$all++;
}

$d = 0;
}
$month++;
}
if ($month == 3){

$dayofmonth = 1;
while ($dayofmonth < 32) {
while ($d < 7) {
if ($d == 0){
$day = "Friday";
}

else if ($d == 1){
$day = "Saturday";
}

else if ($d == 2){
$day = "Sunday";
}

else if ($d == 3){
$day = "Monday";
}

else if ($d == 4){
$day = "Tuesday";
}

else if ($d == 5){
$day = "Wednesday";
}

else if ($d == 6){
$day = "Thursday";
}

$fillsql = "INSERT INTO callender (month, date, year, day) VALUES ('$month', '$dayofmonth', '$year', '$day')";
$fquery = mysqli_query($db,$fillsql);
$perfectly = "Perfectly";
$dayofmonth++;
$all++;
}

$d = 0;
}
$month++;
}
if ($month == 4){

$dayofmonth = 1;
while ($dayofmonth < 31) {
while ($d < 7) {
if ($d == 0){
$day = "Friday";
}

else if ($d == 1){
$day = "Saturday";
}

else if ($d == 2){
$day = "Sunday";
}

else if ($d == 3){
$day = "Monday";
}

else if ($d == 4){
$day = "Tuesday";
}

else if ($d == 5){
$day = "Wednesday";
}

else if ($d == 6){
$day = "Thursday";
}

$fillsql = "INSERT INTO callender (month, date, year, day) VALUES ('$month', '$dayofmonth', '$year', '$day')";
$fquery = mysqli_query($db,$fillsql);
$perfectly = "Perfectly";
$dayofmonth++;
$all++;
}

$d = 0;
}
$month++;
}
if ($month == 5){

$dayofmonth = 1;
while ($dayofmonth < 31) {
while ($d < 7) {
if ($d == 0){
$day = "Friday";
}

else if ($d == 1){
$day = "Saturday";
}

else if ($d == 2){
$day = "Sunday";
}

else if ($d == 3){
$day = "Monday";
}

else if ($d == 4){
$day = "Tuesday";
}

else if ($d == 5){
$day = "Wednesday";
}

else if ($d == 6){
$day = "Thursday";
}

$fillsql = "INSERT INTO callender (month, date, year, day) VALUES ('$month', '$dayofmonth', '$year', '$day')";
$fquery = mysqli_query($db,$fillsql);
$perfectly = "Perfectly";
$dayofmonth++;
$all++;
}

$d = 0;
}
$month++;
}
if ($month == 6){

$dayofmonth = 1;
while ($dayofmonth < 31) {
while ($d < 7) {
if ($d == 0){
$day = "Friday";
}

else if ($d == 1){
$day = "Saturday";
}

else if ($d == 2){
$day = "Sunday";
}

else if ($d == 3){
$day = "Monday";
}

else if ($d == 4){
$day = "Tuesday";
}

else if ($d == 5){
$day = "Wednesday";
}

else if ($d == 6){
$day = "Thursday";
}

$fillsql = "INSERT INTO callender (month, date, year, day) VALUES ('$month', '$dayofmonth', '$year', '$day')";
$fquery = mysqli_query($db,$fillsql);
$perfectly = "Perfectly";
$dayofmonth++;
$all++;
}

$d = 0;
}
$month++;
}
if ($month == 7){

$dayofmonth = 1;
while ($dayofmonth < 32) {
while ($d < 7) {
if ($d == 0){
$day = "Friday";
}

else if ($d == 1){
$day = "Saturday";
}

else if ($d == 2){
$day = "Sunday";
}

else if ($d == 3){
$day = "Monday";
}

else if ($d == 4){
$day = "Tuesday";
}

else if ($d == 5){
$day = "Wednesday";
}

else if ($d == 6){
$day = "Thursday";
}

$fillsql = "INSERT INTO callender (month, date, year, day) VALUES ('$month', '$dayofmonth', '$year', '$day')";
$fquery = mysqli_query($db,$fillsql);
$perfectly = "Perfectly";
$dayofmonth++;
$all++;
}

$d = 0;
}
$month++;
}
if ($month == 8){

$dayofmonth = 1;
while ($dayofmonth < 32) {
while ($d < 7) {
if ($d == 0){
$day = "Friday";
}

else if ($d == 1){
$day = "Saturday";
}

else if ($d == 2){
$day = "Sunday";
}

else if ($d == 3){
$day = "Monday";
}

else if ($d == 4){
$day = "Tuesday";
}

else if ($d == 5){
$day = "Wednesday";
}

else if ($d == 6){
$day = "Thursday";
}

$fillsql = "INSERT INTO callender (month, date, year, day) VALUES ('$month', '$dayofmonth', '$year', '$day')";
$fquery = mysqli_query($db,$fillsql);
$perfectly = "Perfectly";
$dayofmonth++;
$all++;
}

$d = 0;
}
$month++;
}
if ($month == 9){

$dayofmonth = 1;
while ($dayofmonth < 31) {
while ($d < 7) {
if ($d == 0){
$day = "Friday";
}

else if ($d == 1){
$day = "Saturday";
}

else if ($d == 2){
$day = "Sunday";
}

else if ($d == 3){
$day = "Monday";
}

else if ($d == 4){
$day = "Tuesday";
}

else if ($d == 5){
$day = "Wednesday";
}

else if ($d == 6){
$day = "Thursday";
}

$fillsql = "INSERT INTO callender (month, date, year, day) VALUES ('$month', '$dayofmonth', '$year', '$day')";
$fquery = mysqli_query($db,$fillsql);
$perfectly = "Perfectly";
$dayofmonth++;
$all++;
}

$d = 0;
}
$month++;
}
if ($month == 10){

$dayofmonth = 1;
while ($dayofmonth < 32) {
while ($d < 7) {
if ($d == 0){
$day = "Friday";
}

else if ($d == 1){
$day = "Saturday";
}

else if ($d == 2){
$day = "Sunday";
}

else if ($d == 3){
$day = "Monday";
}

else if ($d == 4){
$day = "Tuesday";
}

else if ($d == 5){
$day = "Wednesday";
}

else if ($d == 6){
$day = "Thursday";
}

$fillsql = "INSERT INTO callender (month, date, year, day) VALUES ('$month', '$dayofmonth', '$year', '$day')";
$fquery = mysqli_query($db,$fillsql);
$perfectly = "Perfectly";
$dayofmonth++;
$all++;
}

$d = 0;
}
}
if ($month == 11){

$dayofmonth = 1;
while ($dayofmonth < 31) {
while ($d < 7) {
if ($d == 0){
$day = "Friday";
}

else if ($d == 1){
$day = "Saturday";
}

else if ($d == 2){
$day = "Sunday";
}

else if ($d == 3){
$day = "Monday";
}

else if ($d == 4){
$day = "Tuesday";
}

else if ($d == 5){
$day = "Wednesday";
}

else if ($d == 6){
$day = "Thursday";
}

$fillsql = "INSERT INTO callender (month, date, year, day) VALUES ('$month', '$dayofmonth', '$year', '$day')";
$fquery = mysqli_query($db,$fillsql);
$perfectly = "Perfectly";
$dayofmonth++;
$all++;
}

$d = 0;
}
$month++;
}
if ($month == 12){

$dayofmonth = 1;
while ($dayofmonth < 32) {
while ($d < 7) {
if ($d == 0){
$day = "Friday";
}

else if ($d == 1){
$day = "Saturday";
}

else if ($d == 2){
$day = "Sunday";
}

else if ($d == 3){
$day = "Monday";
}

else if ($d == 4){
$day = "Tuesday";
}

else if ($d == 5){
$day = "Wednesday";
}

else if ($d == 6){
$day = "Thursday";
}

$fillsql = "INSERT INTO callender (month, date, year, day) VALUES ('$month', '$dayofmonth', '$year', '$day')";
$fquery = mysqli_query($db,$fillsql);
$perfectly = "Perfectly";
$dayofmonth++;
$all++;
}

$d = 0;
}
$month++;
}
}

}

echo "Code worked " . $perfectly;

?>

最佳答案

您的代码花费了很长时间,因为您正在执行 365 个查询,而您本来可以只执行一个查询。您也不需要对一周中的每一天的每个月的值进行硬编码。下面我将你的巨大循环折叠成两行代码:

$start = strtotime('2016-01-01');
$end = strtotime('2016-12-31');
$current = $start;
$rows =[];

while ($current <=$end){
//insert eg: ('1', '31', '2016', 'Sunday') for Jan 31 2016.
$rows[] = strftime("('%m', '%d', '%Y', '%A')",$current);
$current = strtotime('+1 day',$current); //to next day
}
//build one combined query
$sql='INSERT into callender (month, date, year, day) VALUES '.implode(',',$rows);
$fquery = mysqli_query($db,$sql);

if(!$fquery) die('DB error: '.mysqli_error($db));

echo 'Success: '.mysqli_num_rows($fquery).' rows inserted';

查看 PHP 的两个最棒的函数:

  • strtotime将人类可读的字符串转换为 UNIX 时间戳
  • strftime将 UNIX 时间戳转换为您选择的格式的日期字符串

既然解决方案已经解决了:这种方法有一个更大的问题。存储日期会浪费数据库空间和资源,因为动态计算日期的速度比从数据库检索日期的速度要快。您可以调整上面的代码来创建一个函数,该函数接受 $startDate$endDate 并返回一个包含所有日历条目的数组以打印到 HTML 中。

关于php - 我的 while 循环有问题吗?我的代码语法?是不是太多了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38647804/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com