IF Statement Looping Php - php

I have a problem with that script using the simulation calculation:
$no3=+1;
for ($i=0; $i<36; $i++)
{
$ No = + 1;
for ($ i = 0; $ i <36; $ i ++) {
enter code here
$ No3 ++;
}
Example :
No GradeA GradeB Total
1 5 5 10
2 10 5 15
3 15 5 20
4 20 5 25
5 25 5 30
6 30 5 35
7 35 5 40
8 40 5 45
9 45 5 50
10 50 5 55
11 55 5 60
12 60 5 65
13 65 5 70
14 70 10 80
15 80 10 90
16 90 10 100
17 100 10 110
18 110 10 120
19 120 10 130
20 130 10 140
21 140 10 150
22 150 10 160
23 160 10 170
24 170 10 180
25 180 10 190
26 190 20 210
27 210 20 230
28 230 20 250
29 250 20 270
30 270 20 290
31 290 20 310
32 310 20 330
33 330 20 350
34 350 20 370
35 370 20 390
36 380 20 410
$no3++;
}
how do I make the numbers 12,24 and 36 there is the text

Try this:
this is answer for your secound question to
<?php
$two = 5;
$three = 5;
$four = 10;
$mod = 14;
for ($i = 1; $i <=36; $i ++) {
echo $i."\t".$two."\t".$three."\t".$four."\n";
if($i % $mod == 0){
$three *= 2;
}
$two += $three;
$four += $three;
}
?>

Related

php for loop in for loop

I have a problem trying using for loop inside a loop i can't get the result i want
<?php
for($sg = 1; $sg <= 11; $sg++){
echo "<b>".echo $sg;."</b>";
for($g = 1; $g <= 11; $g++){
echo "<p>".echo $g."</p>";
}
?>
I looking the result to be like
1
1 2 3 4 5 6 7 8 9 10 11
2
12 13 14 15 16 17 18 19 20
3
21 22 23 24 25 26 27 28 29 30
continuous ....
<?php
for($sg = 1; $sg <= 11; $sg++){
echo "<br /> <b>". $sg."</b> ";
for($g = 1+(10*($sg-1)); $g <= (10*($sg-1))+10; $g++){
echo "". $g." ";
}
}
?>
Output:
1 1 2 3 4 5 6 7 8 9 10
2 11 12 13 14 15 16 17 18 19 20
3 21 22 23 24 25 26 27 28 29 30
4 31 32 33 34 35 36 37 38 39 40
5 41 42 43 44 45 46 47 48 49 50
6 51 52 53 54 55 56 57 58 59 60
7 61 62 63 64 65 66 67 68 69 70
8 71 72 73 74 75 76 77 78 79 80
9 81 82 83 84 85 86 87 88 89 90
10 91 92 93 94 95 96 97 98 99 100
11 101 102 103 104 105 106 107 108 109 110
<?php
$inner_start = 1;
$inner_max = 11;
for($sg = 1; $sg <= 11; $sg++){
echo "<b>".$sg."</b></br>";
echo "<p>";
for($g = $inner_start; $g <= $inner_max; $g++){
echo $g . ' ';
}
$inner_start = $inner_start + 10;
$inner_max += 10;
echo "</p></br>";
}
?>

querying opening Hours, late night In Laravel

I have a Openinghours Table and I want to query through so that I get the closing time of a Location.
Here is an instance of the OpeningHoursTable and my code current query Code, that dosn't work, however you might get an Idea...
For instance day_id = 3,
is open from 11:30 till 14:30. and from 17:30 till 00:00 next Day.
I Woud like to have 0000 of day 4 outputted
How can I query this?
Database entries:
location_id | day_id | opened | closed
81 1 1130 1430
81 1 1730
81 2 0000
81 3 1130 1430
81 3 1730
81 4 0000
81 4 1130 1430
81 4 1730
81 5 0000
81 5 1130 1430
81 5 1730
81 6 0000
81 6 1130 1430
81 6 1730
81 7 0000
81 7 1130 1430
81 7 1730
81 1 0000
My "idea" of Code in Location.php :
public function closesTodayAt($closeTo = 2000)
{
$thisDay = today()->dayOfWeek + 1;
$time = $this->openinghours()->where('day_id', $thisDay)->where('closed','!=', "")
orWhere()->get();
if ($time->first()->closed < $closeTo ) {
#code
}
if ($time < $closeTo ) {
$nextDay = today()->addDay(1)->dayOfWeek +1;
$time = $this->openinghours()->where('day_id', $thisDay)->where('opened','!=',"")->first()->opened;
}
if ($time === "0000") {
# code...
}
$time = str_split($time,2);
$time = implode(":", $time);
return $time;
}
Here another part of the Database:
94 1 1100
94 2 0100
94 2 1100
94 3 0100
94 3 1100
94 4 0100
94 4 1100
94 5 0100
94 5 1100
94 6 0200
94 6 1100
94 7 0400
94 7 1100
94 1 0400
If I'm reading this right, the closing hours would always be the earliest found hours of the next day, so simply taking the hours for the next day, sorting and taking the first record would get you the closing time:
public function scopeCloses($query)
{
$nextDayOfWeek = now()->addDay()->dayOfWeek;
$hours= $this->openinghours->where('day_id', $nextDayOfWeek)
->sortBy('closes')
->first();
return $hours->closes;
}

Is there a way to remove two numbers not affecting others?

The code is giving the following output and each time, it will give a different output.
Is there any way to remove to 2 numbers for example 15 and 60, so that other numbers are not effected they remain the same?
I have tried but could not get the desired result.
Any help or any idea how to do this?
<?php
$arr = array();
for ($i = 1; $i < 82; $i++) {
$arr[] = $i;
}
shuffle($arr);
$lines = array_chunk($arr, 9);
echo "<table>";
foreach ($lines as $key => $line) {
echo "<tr>";
for ($i = 0; $i < sizeof($line); $i++) {
echo "<td align='right'>" . $line[$i] . "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Out put
52 67 5 44 76 49 1 27 28
73 33 19 66 4 14 63 45 62
26 75 50 80 70 38 12 54 78
9 69 36 32 2 7 56 11 51
40 20 22 15 60 65 31 41 77
57 29 34 79 68 23 18 71 39
42 10 72 17 81 30 35 48 47
37 59 53 6 3 55 13 46 58
61 25 24 64 74 43 21 8 16
This should work for you:
Just place the code before your shuffle() call. Basically first we create an array of patterns, by looping through all numbers, which we want to extract from the list, with array_map().
The pattern basically has two anchors to match the numbers fully in your array and not only partially (E.g. 2 will not match 24, but it will match 2).
With the created pattern array we just use preg_replace() to replace the numbers in your array with an empty string.
$numbers = [15, 60];
$pattern = array_map(function($v){
return "/^$v$/";
}, $numbers);
$arr = preg_replace($pattern, "", $arr);

How to show these numbers in tabular format?

This code is giving the following output, I want to show these numbers in table is there any way to do this? and why it is not showing the numbers in proper format like why 53 is not exactly below 40 and others also not showing in proper order?
<?php
$arr = array();
for ($i=1;$i<82;$i++) {
$arr[] = $i;
}
shuffle($arr);
$lines = array_chunk($arr, 9);
foreach ($lines as $key => $line) {
$lines[$key] = implode("&nbsp&nbsp&nbsp", $line);
}
echo implode("<br>", $lines);
?>
Output
73 40 79 1 43 7 76 44 18
6 53 45 55 71 20 80 66 74
69 51 52 65 22 63 59 50 54
29 33 23 49 77 24 61 60 58
8 81 30 15 26 32 16 47 31
17 39 4 35 27 11 5 25 68
2 34 72 42 75 46 48 3 38
14 28 37 62 10 78 12 56 13
41 21 19 36 9 64 67 57 70
If you don't want to use tables or CSS. You can use a <pre> tag then use some tabs and newlines
<pre>
<?php
$arr = array();
for ($i=1;$i<82;$i++) {
$arr[] = $i;
}
shuffle($arr);
$lines = array_chunk($arr, 9);
foreach ($lines as $key => $line) {
$lines[$key] = implode("\t", $line);
}
echo implode("\n", $lines);
?>
Fiddle
Note: I didn't really care about your logic for creating those lines while answering this question since its only about formatting. You can trim down some code too.
Output
22 16 66 79 8 41 47 2 80
29 38 76 18 40 46 73 34 45
31 3 62 68 14 33 20 72 67
78 44 42 30 51 77 36 25 48
64 70 21 15 19 9 56 50 65
37 27 4 1 35 74 75 52 32
81 23 10 28 26 59 7 54 11
6 63 5 39 53 12 24 60 49
71 55 17 13 61 69 43 57 58
Now with slightly better code
<pre>
<?php
$numbers=range(1,81);
shuffle($numbers);
$c=0;
foreach($numbers as $n)
{
if($c%9==0)echo "\n";
echo $n."\t";
$c++;
}
?>
modify your code like this:
<?php
$arr = array();
for ($i=1;$i<82;$i++) {
$arr[] = $i;
}
shuffle($arr);
$lines = array_chunk($arr, 9);
echo '<table>';
foreach ($lines as $key => $line) {
echo '<tr><td align="right">';
echo $lines[$key] = implode('</td><td align="right">', $line);
echo '</td></tr>';
}
echo '</table>';
?>
Output
57 41 48 17 73 76 7 78 12
69 61 39 80 24 58 45 11 70
47 65 33 21 38 4 19 13 46
59 52 63 14 25 3 30 28 77
50 40 68 6 2 29 20 66 26
72 74 34 75 15 36 71 10 60
55 53 1 16 23 42 51 35 62
44 32 43 64 18 8 54 49 5
81 27 31 67 37 22 79 56 9
try this
<?php
$arr = array();
for ($i = 1; $i < 82; $i++) {
$arr[] = $i;
}
shuffle($arr);
$lines = array_chunk($arr, 9);
echo "<table>";
foreach ($lines as $key => $line) {
echo "<tr>";
for ($i = 0; $i < sizeof($line); $i++) {
echo "<td align='right'>" . $line[$i] . "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Your Out put

Nested for and do while loop

How do i create a nested loop that will output the following numbers?
each round the inner loop increase from 1 to 5, 5 to 10 and so on.
from i = 0 to 5
inner loop:
result: 1 2 3 4 5
result: 6 7 8 9 1 0
result: 11 12 13 14 15
result: 16 17 18 19 20
next
for($i=0;$i<50;$i++)
{
$s = $i +5;
echo $s;
}
unless you have to use an inner do...while loop, this will work:
<?php
$max = 5;
for($i=0;$i<$max;$i++){
for($j=1;$j<=$max;$j++){
echo str_pad(($i*$max)+$j,4);
}
echo "\r\n";
}
output:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
I used str_pad() just to make the columns more uniform

Categories