无法正确显示下一周和上一周。

huangapple go评论62阅读模式
英文:

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?>'>&lt;</a>
<input class='weekno' type='text' name='selectWeek' value='<?=$week?>'>
<a class='button' href='?selectWeek=<?=$next?>'>&gt;</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[&#39;selectWeek&#39;])){
$week  =       $_GET[&#39;selectWeek&#39;];

$prev = $week - 1;
$next = $week + 1;

if ($prev &lt; 1) $prev = 1;
if ($next &gt; 53) $next = 53;

}else{
    $ddate  =       date(&#39;y-m-d&#39;);
    $date = new DateTime($ddate);
    $week = &#39;w&#39;.$date-&gt;format(&quot;W&quot;);
}

Display code

&lt;a class=&#39;button&#39; href=&#39;?selectWeek=&lt;?=$prev?&gt;&#39;&gt;&amp;lt;&lt;/a&gt;
&lt;input class=&#39;weekno&#39; type=&#39;text&#39; name=&#39;selectWeek&#39; value=&#39;&lt;?=$week?&gt;&#39;&gt;
&lt;a class=&#39;button&#39; href=&#39;?selectWeek=&lt;?=$next?&gt;&#39;&gt;&amp;gt;&lt;/a&gt;

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

如果$weekw08,那它不是一个数字,不能用于数学方程。你可以做的是去掉开头的w0,将其转换为一个数字。ltrim可以做到这一点,并保留尾随的0,所以w08变成8w28变成28w20变成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[&#39;selectWeek&#39;];
$num_week = ltrim($week, &#39;w0&#39;);
$prev = $num_week - 1;
$next = $num_week + 1;

huangapple
  • 本文由 发表于 2023年2月23日 23:09:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定