EarthDay Posted February 23, 2023 Share Posted February 23, 2023 (edited) Hi there, I am not sure where to start here so was wondering if someone could point me in the right direction please? I've had a look on Google and can't find anything that works. I have an expense system that shows data by week (index.php?wk=1 for example) and I would like to create a button called Next and Previous to show the previous and next weeks. Database example ID - 1 Week - 1 issue_date - 02/02/2023 money_in money_out initials Many thanks, ED. Edited February 23, 2023 by EarthDay Quote Link to comment https://forums.phpfreaks.com/topic/315939-previous-and-next-button-based-on-week-id/ Share on other sites More sharing options...
Barand Posted February 23, 2023 Share Posted February 23, 2023 When you have more than 1 year's data, how will you know which "week 1" you need to show? Quote Link to comment https://forums.phpfreaks.com/topic/315939-previous-and-next-button-based-on-week-id/#findComment-1605913 Share on other sites More sharing options...
EarthDay Posted February 23, 2023 Author Share Posted February 23, 2023 Hi Barand, Thanks for your reply, I only ever store 1 years worth of data in and remove the old. So there is only one week 1. But will go back to week 52 at the beginning of January / end of December. Cheers, ED Quote Link to comment https://forums.phpfreaks.com/topic/315939-previous-and-next-button-based-on-week-id/#findComment-1605915 Share on other sites More sharing options...
Barand Posted February 23, 2023 Share Posted February 23, 2023 The basic principle is very simple <?php $week = $_GET['wk'] ?? 1; // set week to $_GET['wk'], or 1 if no week is provided $prev = $week - 1; $next = $week + 1; if ($prev < 1) $prev = 1; if ($next > 53) $next = 53; ?> Then display your buttons <a class='button' href='?wk=<?=$prev?>'><</a> <input class='weekno' type='text' name='wk' value='<?=$week?>'> <a class='button' href='?wk=<?=$next?>'>></a> Actually links, using css to make them look like buttons EG <style type='text/css'> .button { display: inline-block; border: 1px solid black; width: 40px; padding: 4px; text-decoration: none; text-align: center; font-family: arial, sans-serif; background-color: #808080; color: #FFFFFF; } .button:hover { background-color: #ACACAC; } .weekno { width: 50px; text-align: center; } Quote Link to comment https://forums.phpfreaks.com/topic/315939-previous-and-next-button-based-on-week-id/#findComment-1605916 Share on other sites More sharing options...
EarthDay Posted February 23, 2023 Author Share Posted February 23, 2023 Hi Barand, Thank you so much for this, it's awesome and worked on the page. I have also have an older script that uses w01 for the week (index.php?selectWeek=w27) - will this also work with the W in the week number? Database id - 1 base_charge - 44.25 week - w08 year - 2023 Cheers, ED. Quote Link to comment https://forums.phpfreaks.com/topic/315939-previous-and-next-button-based-on-week-id/#findComment-1605918 Share on other sites More sharing options...
EarthDay Posted February 23, 2023 Author Share Posted February 23, 2023 (edited) H Barand, I have changed the code slightly to match my older script; if(isset($_GET['selectWeek'])){ $week = $_GET['selectWeek']; $num_week = ltrim($week, 'w0')?? 1; $prev = $num_week - 1; $next = $num_week + 1; if ($prev < 1) $prev = 1; if ($next > 53) $next = 53; The issue that I am having now is giving the below error message; week in database is VARCHAR(50) Quote Fatal error: Uncaught TypeError: Unsupported operand types: string - int in And the link is showing as Quote .php?selectWeek= The error message is caused by line 18 $prev = $num_week - 1; Links <a class='button' href='?selectWeek=<?=$prev?>'><</a> <input class='weekno' type='text' name='selectWeek' value='<?=$num_week?>'> Cheers, ED. Edited February 23, 2023 by EarthDay Quote Link to comment https://forums.phpfreaks.com/topic/315939-previous-and-next-button-based-on-week-id/#findComment-1605919 Share on other sites More sharing options...
Barand Posted February 23, 2023 Share Posted February 23, 2023 If you must do stupid things such as storing numbers so that they can't be used as numbers, then you need to convert it back to a number $num_week = intval(ltrim($week, 'w0')); 1 Quote Link to comment https://forums.phpfreaks.com/topic/315939-previous-and-next-button-based-on-week-id/#findComment-1605922 Share on other sites More sharing options...
EarthDay Posted March 2, 2023 Author Share Posted March 2, 2023 On 2/23/2023 at 6:15 PM, Barand said: If you must do stupid things such as storing numbers so that they can't be used as numbers, then you need to convert it back to a number $num_week = intval(ltrim($week, 'w0')); Hi Berand, I know it's old code, it's part of a system that is getting upgraded later this year. Thank you for this. It's still showing the the week as w8 and not w08 when trying to go to the previous page. Cheers, ED. Quote Link to comment https://forums.phpfreaks.com/topic/315939-previous-and-next-button-based-on-week-id/#findComment-1606142 Share on other sites More sharing options...
jodunno Posted March 3, 2023 Share Posted March 3, 2023 23 hours ago, EarthDay said: It's still showing the the week as w8 and not w08 that is what ltrim(, 'w0') will do: trim the w0 from the string. I'm confused reading this post. What are you trying to do with w? PHP should not be used to format an output string anywhere other than the point of output. I recommend dropping any w (which, i guess represents week. And week is implied so no need to insult a users intelligence by adding a w to the number.) I have spent some time to code an example of previous and next query string based pages using only php. I also added a 'w' at the point of output. Perhaps you can find the problem in your code by analyzing my code. If you want numbers to be padded, then pad them. If you want to work with annual data, then try to remember leap years. Also, writing scalable code is better, so we add a decimal places variable (tens, hundreds, thousands, etc.), then the code can be changed accordingly. Maybe my script will be of use to you. <?php /* php 8.2 compatible */ $delimiter = 2; /* tens (01-09) */ if (empty($_GET['selectWeek']) || preg_match("/^[0-9]{1,$delimiter}+$/", $_GET['selectWeek']) === 0) { $gwetWK = '1'; } else { $gwetWK = intval($_GET['selectWeek']); } /* ltrim as a zero stripper: $gwetWK = ltrim($_GET['selectWeek'], '0'); */ $leapYear = false; /* set to true for 53 weeks */ $weeks = ['Weeks', $leapYear ? '53' : '52']; $thisWeek = ['This Week', $gwetWK]; $thisWeek[1] = $thisWeek[1] > $weeks[1] ? $thisWeek[1] = $weeks[1] : $thisWeek[1]; $lastWeek = ['Previous Week', $thisWeek[1] - 1 < 1 ? '1' : $thisWeek[1] - 1]; $nextWeek = ['Next Week', $thisWeek[1] + 1 > $weeks[1] ? $weeks[1] : $thisWeek[1] + 1]; $thisWeek[1] = str_pad(strval($thisWeek[1]), $delimiter, '0', STR_PAD_LEFT); $lastWeek[1] = str_pad(strval($lastWeek[1]), $delimiter, '0', STR_PAD_LEFT); $nextWeek[1] = str_pad(strval($nextWeek[1]), $delimiter, '0', STR_PAD_LEFT); echo '<html>' . "\r\n"; echo '<head>' . "\r\n"; echo ' <title>' . $thisWeek[0] . ' = w' . $thisWeek[1] . '</title>' . "\r\n"; echo ' <style>' . "\r\n"; echo ' body { font-family: "Arial", "Helvetica", "Chicago", sans-serif; font-size: 16px; }' . "\r\n"; echo ' .centered { text-align: center; }' . "\r\n"; echo ' ul { margin: 0px 0px; padding: 0px 0px; }' . "\r\n"; echo ' li { list-style-type: none; }' . "\r\n"; echo ' .wbuttons { background: #a0a0ac; border-radius: 4px; border: solid 1px #808080; color: #f8f8f8; }' . "\r\n"; echo ' .nounderline { text-decoration: none; }' . "\r\n"; echo ' .inlineblock { display: inline-block; }' . "\r\n"; echo ' .padding8 { padding: 8px 8px; }' . "\r\n"; echo ' .border1 { border: solid 1px #ccccff; }' . "\r\n"; echo ' .bgsnow { background: #f8f8f8; }' . "\r\n"; echo ' .embolden { font-weight: bold; }' . "\r\n"; echo ' </style>' . "\r\n"; echo '</head>' . "\r\n"; echo '<body>' . "\r\n"; echo "\r\n"; echo ' <div class="centered"><ul class="inlineblock">' . "\r\n"; echo ' <li class="inlineblock padding8 embolden"><a class="nounderline" title="Week 01" href="http://localhost/test/int/?selectWeek=01"><<</a></li>' . "\r\n"; echo ' <li class="inlineblock padding8 embolden"><a class="wbuttons nounderline padding8" title="' . $lastWeek[0] . '" href="http://localhost/test/int/?selectWeek=' . $lastWeek[1] . '">w' . $lastWeek[1] . '</a></li>' . "\r\n"; echo ' <li class="inlineblock padding8 embolden"><span title="' . $thisWeek[0] . '" class="padding8 border1 bgsnow">w' . $thisWeek[1] . '</span></li>' . "\r\n"; echo ' <li class="inlineblock padding8 embolden"><a class="wbuttons nounderline padding8" title="' . $nextWeek[0] . '" href="http://localhost/test/int/?selectWeek=' . $nextWeek[1] . '">w' . $nextWeek[1] . '</a></li>' . "\r\n"; echo ' <li class="inlineblock padding8 embolden"><a class="nounderline" title="Week ' . $weeks[1] . '" href="http://localhost/test/int/?selectWeek=' . $weeks[1] . '">>></a></li>' . "\r\n"; echo ' </ul></div>' . "\r\n"; /* why add a w to the number? week is implied: 'selectWeek' */ echo '</body>' . "\r\n"; echo '</html> Quote Link to comment https://forums.phpfreaks.com/topic/315939-previous-and-next-button-based-on-week-id/#findComment-1606181 Share on other sites More sharing options...
kicken Posted March 3, 2023 Share Posted March 3, 2023 On 3/2/2023 at 9:19 AM, EarthDay said: It's still showing the the week as w8 and not w08 You can use sprintf to easily ensure the 0 is there for your link. $prev = sprintf('w%02d', $num_week - 1); $next = sprintf('w%02d', $num_week + 1); 1 Quote Link to comment https://forums.phpfreaks.com/topic/315939-previous-and-next-button-based-on-week-id/#findComment-1606187 Share on other sites More sharing options...
jodunno Posted March 3, 2023 Share Posted March 3, 2023 (edited) 2 hours ago, kicken said: You can use sprintf to easily ensure the 0 is there for your link hey that is an excellent and time saving tip! I also did not know that php has this old friend from cpp. php is just a hobby for me. I don't have alot of time to invest. Now i have a new tool in my toolbox. I will play this over the weekend., haha. so you could drop the padding in my example and replace it with a better idea: <?php /* php 8.2 compatible */ $delimiter = 2; /* tens (01-99) */ if (empty($_GET['selectWeek']) || preg_match("/^[0-9]{1,$delimiter}+$/", $_GET['selectWeek']) === 0) { $gwetWK = '1'; } else { $gwetWK = intval($_GET['selectWeek']); } /* ltrim as a zero stripper: $gwetWK = ltrim($_GET['selectWeek'], '0'); */ $leapYear = false; /* set to true for 53 weeks */ $weeks = ['Weeks', $leapYear ? '53' : '52']; /* arrays are only used to store a label, title, caption text et cetera plus number. can be dynamic with db names */ $thisWeek = ['This Week', $gwetWK > $weeks[1] ? $gwetWK = $weeks[1] : $gwetWK]; $lastWeek = ['Previous Week', $thisWeek[1] - 1 < 1 ? '1' : $thisWeek[1] - 1]; $nextWeek = ['Next Week', $thisWeek[1] + 1 > $weeks[1] ? $weeks[1] : $thisWeek[1] + 1]; echo '<html>' . "\r\n"; echo '<head>' . "\r\n"; echo ' <title>' . $thisWeek[0] . ' = w' . $thisWeek[1] . '</title>' . "\r\n"; echo ' <style>' . "\r\n"; echo ' body { font-family: "Arial", "Helvetica", "Chicago", sans-serif; font-size: 16px; }' . "\r\n"; echo ' .centered { text-align: center; }' . "\r\n"; echo ' ul { margin: 0px 0px; padding: 0px 0px; }' . "\r\n"; echo ' li { list-style-type: none; }' . "\r\n"; echo ' .wbuttons { background: #a0a0ac; border-radius: 4px; border: solid 1px #808080; color: #f8f8f8; }' . "\r\n"; echo ' .nounderline { text-decoration: none; }' . "\r\n"; echo ' .inlineblock { display: inline-block; }' . "\r\n"; echo ' .padding8 { padding: 8px 8px; }' . "\r\n"; echo ' .border1 { border: solid 1px #ccccff; }' . "\r\n"; echo ' .bgsnow { background: #f8f8f8; }' . "\r\n"; echo ' .embolden { font-weight: bold; }' . "\r\n"; echo ' </style>' . "\r\n"; echo '</head>' . "\r\n"; echo '<body>' . "\r\n"; echo "\r\n"; echo ' <div class="centered"><ul class="inlineblock">' . "\r\n"; echo ' <li class="inlineblock padding8 embolden"><a class="nounderline" title="Week 01" href="http://localhost/test/int/?selectWeek=01"><<</a></li>' . "\r\n"; echo ' <li class="inlineblock padding8 embolden"><a class="wbuttons nounderline padding8" title="' . $lastWeek[0] . '" href="http://localhost/test/int/?selectWeek=' . $lastWeek[1] . '">' . sprintf('w%02d', $lastWeek[1]) . '</a></li>' . "\r\n"; echo ' <li class="inlineblock padding8 embolden"><span title="' . $thisWeek[0] . '" class="padding8 border1 bgsnow">' . sprintf('w%02d', $thisWeek[1]) . '</span></li>' . "\r\n"; echo ' <li class="inlineblock padding8 embolden"><a class="wbuttons nounderline padding8" title="' . $nextWeek[0] . '" href="http://localhost/test/int/?selectWeek=' . $nextWeek[1] . '">' . sprintf('w%02d', $nextWeek[1]) . '</a></li>' . "\r\n"; echo ' <li class="inlineblock padding8 embolden"><a class="nounderline" title="Week ' . $weeks[1] . '" href="http://localhost/test/int/?selectWeek=' . $weeks[1] . '">>></a></li>' . "\r\n"; echo ' </ul></div>' . "\r\n"; /* why add a w to the number? week is implied: 'selectWeek' */ echo '</body>' . "\r\n"; echo '</html> be sure to Thank Kicken for the tip. I hope that you can get your code working. Let usknow how it goes... Edited March 3, 2023 by jodunno optimization of code Quote Link to comment https://forums.phpfreaks.com/topic/315939-previous-and-next-button-based-on-week-id/#findComment-1606193 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.