PHP while loop for HTML Generation: Should I Check Array values at each While Loop Iteration or use Post-Loop String Pro
Date : March 29 2020, 07:55 AM
it should still fix some issue I'm working on a PHP-written html calendar that writes each day of the month inside a while loop. , Here's another solution: $day = 1;
$strings = array();
while($day < 31) {
if (array_key_exists($day, $array)) {
$strings[$day] = $array['$day'] . " spots available";
} else {
$strings[$day] = "No spots available for this date";
}
$day += 1;
}
foreach ($strings as $day => $string) {
echo '<td title="' . $string . '">' . $day . '</td>';
}
|
Triple nested loop (while loop insde of for loop inside of while loop)
Tag : python , By : user165781
Date : March 29 2020, 07:55 AM
Does that help If you want m1 to be the cumulative list of means, then the last line needs to be indented to be inside the final while block. while d <335:
d = d + 1
y = fpsd[d]
y1 = y1 + [y]
m = np.mean(y1)
m1 = m1 + [m]
|
My loop works like this: loop, script, loop. I want this: loop, loop, code. The ForgottenServer 0.3.6
Tag : loops , By : hyperNURb
Date : March 29 2020, 07:55 AM
hop of those help? Can you just move the "set storage" to a second loop after the first? That's the only way that you'll be able to send all of the messages before doing any of the set storage calls. for _, pid in ipairs(getPlayersOnline()) do
if getCreatureStorage(pid, tmp[i].storeDamage) > -1 then
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, getPlayerName(pid)..': '..getCreatureStorage(pid, 11005))
end
end
for _, pid in ipairs(getPlayersOnline()) do
doCreatureSetStorage(pid, tmp[i].storeDamage, -1)
end
|
From nested loop stopping loop and remove specific loop set if that loop has a content in laravel
Tag : php , By : Mariamario
Date : March 29 2020, 07:55 AM
hope this fix your issue I have several row in a table in db and every row related with three tables and every table has many rows. when i loop main table rows i also loop three tables rows now if one of tables has a content in row then i want to prevent showing entire sub that loop only which contain a value. , This method uses relationship count comparison. Controller public function classWiseResult(Request $request){
$students = Students::where('class',$request->class)
->withCount(['firstTerm as firstTermPromoted' => function($q) {
$q->where('number', '>', 32);
}])
->withCount('firstTerm as firstTermAllCount')
->withCount(['secondTerm as secondTermPromoted' => function($q) {
$q->where('number', '>', 32);
}])
->withCount('secondTerm as secondTermAllCount')
->withCount(['finalTerm as finalTermPromoted' => function($q) {
$q->where('number', '>', 32);
}])
->withCount('finalTerm as finalTermAllCount')
->with('firstTerm')
->with('secondTerm')
->with('finalTerm')
->get();
return view('admin.showResult.show',compact('students'));
}
@foreach($students as $student)
@if($student->firstTermPromoted == $student->firstTermAllCount
&& $student->secondTermPromoted == $student->secondTermAllCount
&& $student->finalTermPromoted == $student->finalTermAllCount)
{{ "Promoted" }}
@else
{{ "Not Promoted" }}
@endif
@endforeach
|
Loop-through-loop-through dataframe: How to improve performance on loop that calculates the result based on another loop
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I need to process a huge dataset of million entries, with the following format: , A data.table approach using non-equi joins: library(data.table)
setDT(visits)[, clicks_30days :=
visits[.(person_id=person_id, start=date, end=date+30L),
on=.(person_id, date>=start, date<=end), sum(clicks), by=.EACHI]$V1
]
person_id date clicks clicks_30days
1: 1 2017-05-04 4 4
2: 1 2018-05-04 1 8
3: 1 2016-02-04 5 5
4: 1 2018-05-06 7 7
5: 2 2018-05-04 8 9
6: 2 2018-05-16 1 1
7: 2 2018-01-04 1 1
8: 2 2018-02-04 2 2
library(data.table)
set.seed(0L)
npers <- 15e3L
ndates <- 150L
visits <- data.frame(person_id=rep(1L:npers, each=ndates),
date=sample(seq(Sys.Date()-5L*365L, Sys.Date(), by="1 day"), npers*ndates, TRUE),
clicks=sample(10, npers*ndates, TRUE))
vi <- visits
mtd0 <- function() {
visits$person_id <- as.integer(visits$person_id) # faster for integers
unique_visitors <- unique(visits$person_id)
# create columns as vectors (accessing elements in loop will be fast)
r <- visits$clicks_30days2 <- 0 # result vector
j <- 1L
person_id <- visits$person_id
CL <- visits$clicks
DATE_as_int <- as.integer(visits$date) # convert dates to integers
for (id in unique_visitors){
x <- person_id == id # indicates current person
dates <- DATE_as_int[x] # take dates of this person
clicks <- CL[x] # clicks of this person
for (i in 1:length(dates)) {
i_date <- dates[i] # take i-th date
ii <- i_date <= dates & dates <= i_date + 30 # test interval
# r[x][i] <- sum(clicks[ii]) # sum
r[j] <- sum(clicks[ii]) # faster using one index
j <- j + 1L
}
}
visits$clicks_30days2 <- r # assigne to results
visits
}
mtd1 <- function() {
setDT(vi)[, clicks_30days :=
vi[.(person_id=person_id, start=date, end=date+30L),
on=.(person_id, date>=start, date<=end), sum(clicks), by=.EACHI]$V1
]
}
library(microbenchmark)
microbenchmark(mtd0(), mtd1(), times=3L)
Unit: seconds
expr min lq mean median uq max neval cld
mtd0() 144.847468 145.339189 146.358507 145.830910 147.114026 148.397141 3 b
mtd1() 2.367768 2.398254 2.445058 2.428741 2.483703 2.538665 3 a
|