Your Location is: Home > Php
Print a Statement With There Privious And Next Value. SUNDAY MONDAY Program using PHP
Question
I am building a program to print the statement using Switch Statement in PHP as per users's input.
Whenver any user input any number then i am printing a value.
This is my code.
if (isset($_POST['inputnumber']))
{
fetchDays($_POST['inputnumber']);
}
This is my function which will print the data if any case matched.
function fetchDays($data)
{
switch ($data)
{
case 1:
echo "Sunday";
break;
case 2:
echo "Monday";
break;
case 3:
echo "Tuesday";
break;
case 4:
echo "Wedesday";
break;
case 5:
echo "Thursday";
break;
case 6:
echo "Friday";
break;
case 7:
echo "Saturday";
break;
default:
echo "Enter Valid Week Number";
break;
}
}
The question is that the how can I print the value with previous and next value,
Suppos if a user enter 2, I want to print the MONDAY with privious and next value.
for eg.
SUNDAY MONDAY TUESDAY
If user enter 1, as per the condition I want to print value with there next value, because the privious value doesn't exist. The same logic will be applied on the last input which is 7.
Best answer
You can the function multiple times using a loop.
Try this.
if (isset($_POST['inputnumber']))
{
$value = $_POST['inputnumber']; //Storing users input number into variable
if ($value>1)
{
if ($value<8)
$value = $value-1;
}
else
$value = 0;
$loopValue = $value + 3 ;
if ($value>=0 && $value<8)
{
while ($value<$loopValue)
{
if ($value==0)
{
$value++;
}
else if ($value==8)
{
break;
}
fetchDays($value);
echo "<br>";
$value++;
}
}
else
fetchDays($value);
}
For Better Understanding code with decription/comments.
if (isset($_POST['inputnumber']))
{
$value = $_POST['inputnumber']; //Storing users input number into variable
if ($value>1)
{
if ($value<8)
$value = $value-1; //Decreament The Value by one and assign to the $value variable
}
else
$value = 0; // if the conidition is not greater than then assigning to variable $value to 0
$loopValue = $value + 3 ; //Adding 3 to the value and creating a variable for Loop.
if ($value>=0 && $value<8) //if the value is greater than 0 and less than 8.
{
while ($value<$loopValue)
{
if ($value==0) //if the value is zero adding 1 in value
{
$value++;
}
else if ($value==8) // breaking the loop when the value reach 8
{
break;
}
fetchDays($value); // calling the function
echo "<br>";
$value++;
}
}
else
fetchDays($value);
}
Another answer
You can call fetchDays mutiple times
if (isset($_POST['inputnumber'])) {
$inputNumber = $_POST['inputnumber'];
if ($inputNumber > 1 && $inputNumber <= 7) {
fetchDays($inputNumber - 1);
echo ' ';
}
fetchDays($inputNumber);
if ($inputNumber < 7 && $inputNumber >= 1) {
echo ' ';
fetchDays($inputNumber + 1);
}
}
Another answer
Put the strings inside an array to make it concise and use implode()
to stitch days together. You can improvise it with complex bit operations but that would make the code unreadable for something so simple.
So, increment week day by 1 if day given is 1 and decrement day by 1 if day given is 7. Now, implode() them starting from a day before with an offset of 3.
<?php
function fetchDays($week_day){
if($week_day < 1 || $week_day > 7) return "Enter valid week number";
$days = ["", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
if($week_day == 1) $week_day++;
else if($week_day == 7) $week_day--;
return implode(" ",array_slice($days,$week_day - 1,3));
}
Driver Code:
foreach(range(1,7) as $day){
echo $day, " " ,fetchDays($day),PHP_EOL;
}