英文:
Unable to display next and previous weeks correctly
问题
我试图在我的费用系统上添加"下一周"和"上一周"按钮,但地址栏中的周值总是错误的。
URL应该是例如 ?selectWeek=w43,但返回的是 ?selectWeek=1。
我已经编写了以下代码:
if(isset($_GET['selectWeek'])){
$week = $_GET['selectWeek'];
$prev = $week - 1;
$next = $week + 1;
if ($prev < 1) $prev = 1;
if ($next > 53) $next = 53;
}else{
$ddate = date('y-m-d');
$date = new DateTime($ddate);
$week = 'w'.$date->format("W");
}
显示代码:
<a class='button' href='?selectWeek=<?=$prev?>'><</a>
<input class='weekno' type='text' name='selectWeek' value='<?=$week?>'>
<a class='button' href='?selectWeek=<?=$next?>'>></a>
数据库:
id - 1
base_charge - 44.25
week - w08
year - 2023
我知道这是一个简单的修复,但似乎无法解决它
英文:
I am trying to add a next and previous button onto my expenses system and it keeps coming back with the wrong week value in the address bar.
The URL should be for example ?selectWeek=w43 and is coming back as ?selectWeek=1
I have written the following the following code;
if(isset($_GET['selectWeek'])){
$week = $_GET['selectWeek'];
$prev = $week - 1;
$next = $week + 1;
if ($prev < 1) $prev = 1;
if ($next > 53) $next = 53;
}else{
$ddate = date('y-m-d');
$date = new DateTime($ddate);
$week = 'w'.$date->format("W");
}
Display code
<a class='button' href='?selectWeek=<?=$prev?>'>&lt;</a>
<input class='weekno' type='text' name='selectWeek' value='<?=$week?>'>
<a class='button' href='?selectWeek=<?=$next?>'>&gt;</a>
Database
id - 1
base_charge - 44.25
week - w08
year - 2023
I know it's a simple fix but can't seem to work it out
答案1
得分: 1
如果$week
是w08
,那它不是一个数字,不能用于数学方程。你可以做的是去掉开头的w
和0
,将其转换为一个数字。ltrim可以做到这一点,并保留尾随的0
,所以w08
变成8
,w28
变成28
,w20
变成20
。
$week = $_GET['selectWeek'];
$num_week = ltrim($week, 'w0');
$prev = $num_week - 1;
$next = $num_week + 1;
英文:
If $week
is w08
, then it's not a number and can't be used in math equations. What you can do is strip off the w and and beginning 0s, to turn it into a number. ltrim will do that, and leave the trailing 0s, so w08
becomes 8
, w28
becomes 28
, and w20
becomes 20
.
$week = $_GET['selectWeek'];
$num_week = ltrim($week, 'w0');
$prev = $num_week - 1;
$next = $num_week + 1;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论