Trying to teach myself the concept of how to calculate the indexes for pagination, I have found the following text:
Say for example that we have per page equals to 10, so it will be ten
items per page. Once again our calculation for offset is per page,
which is ten, times the current page, minus 1. If the current page is
1, that calculation works out so that offset is 0, and that would mean
that records 1 through 10 would be 10, and that would mean that
records 11 through 20 would be returned
Maybe I'm looking at it the wrong way, but I don't get it.
The text states that using an example of 10 items per page, and we're on the first page, the formula should be:
10 * 1 - 1 -> this by my brain cramp should equal 9, but the text says it's 0
The next example they use is: 10 * 2 - 1 = 10, using normal math, is that not 19??
there's obviously something I'm missing here.
What they mean is
10 * (1-1) = 0
and
10 * (2-1) = 10
In the end this boils down to the fact that people count from 1 (the first page is page 1), but the offset starts at 0. So if you have a page-number, you need to lower it by one to get the offset number (the first page is 1, the first offset is 0 ).
THat means you first subtract (hence the brackets) and then multiply. The text is just a little bit confusing.
Related
I'm trying to calculate a sort or "daily random number" in a range, wich can't be guessed, for each user in our site but can't figure out how to do it.
I don't want a random number either, it must be a calculated number in PHP, not an additional database field or anything similar.
I tought at a function who can take the user's ID and the day of year and calculate this number.
Example:
USERID: 12345, Range: 0-7 (constant values for every user)
DayOfYear: 250 (change every day)
Then something like: ((12345 + 250) MODULO 8) (so I've range from 0 to 7 for each user). The problem is that the same number will come out every 8 days in a loop that user will find very fast.
Each user don't necessarly need a different number for every day, even the same number would be OK for a few days but not all users must have the same number. Also, most important, no loop scenario, so user can't guess his his daily number.
Thank you for your help.
There are so many answers to this question and none would be the best ... but I'm in a funny mood:
$id = hexdec(substr(md5($userId . date('z')), 0, 3)) % 8;
Use the md5-function to get a hex-string from a string. Then use a part of this string to calculate the mod 8.
For the next 10 days the id will be 5,7,5,3,6,0,2,0,2,4 when using your user id
But to guess a number between 0 and 7 isn't so hard, don't uses this for security ...
Current I use LengthAwarePaginator for paging on laravel.
I have a problem with number of row on list view.
I have total 8 record, and paging with 3 items per page => I have 3 page.
I want first page, column number is: 1, 2, 3.
and second page, column number is: 4, 5, 6
and next page, column number is: 7, 8, ...
And current I foreach array and get by key => it always begin from 1 for every page.
I spend many time for this problem. Please help me resolved it. many thanks.
Assuming your pagination object is called $results, I think you can use this:
($results->currentPage()-1) * $results->perPage() + $key
So, on page 1 it would be (1-1)*3+1=1 for the first record.
On page 2 it would be (2-1)*3+1=4 for the first record.
I have a list of questions in a category, and want to choose a subset of them to ask the user based on which ones they answered right/wrong previously.
I want to make it random, but in a way that the ones they have more trouble with are asked more frequently.
EDIT: I'm trying to figure out how to calculate the weight/bias/score for each question based on the number of times they've answered it right/wrong.
I came up with the following, but it seems odd to me:
I assign a score to each question based on how many times they answered it right/wrong
Obviously, if they've never been asked that question I need to assign an arbitrary score (I chose 5)
For all other question, I use the formula
score = wrong*2-right
so if I had the following 10 questions, the "score" would be calculated for each of them (R=# of times they got it right, W=# of times they got it wrong and S=score). From there, I take the lowest score and assign that a probability of 1 (in this case it was id=5 with a score of -7). I then take the difference between the lowest score and the second lowest score (id=1 with -5, a difference of 2) and assign it a probability of 1 + the difference = 3.
I continue this for every question, and then at the end I can just choose a random number between Min(1) and Max(82) and select the question that has the highest P where random < P. So if my random # was 79 I would choose id=2.
But this seems long and convoluted. Is there an easier way to do this (I'm using PHP and mysql, But I plan to do this within an app with a local datastore as well)
id R W S P
1 5 0 -5 3
2 3 5 7 82
3 6 2 -2 8
4 2 2 2 23
5 9 1 -7 1
6 3 1 -1 14
7 0 0 5 68
8 7 5 3 33
9 6 5 4 44
10 3 4 5 56
EDIT: to clarify, I'm stuck on the issue of "weight" (P value in my example)...I'm trying to find a good (and fast) way of calculating the "weight" for each problem, given the number of right and wrong answers they've given for the question
I am not sure if I understand your answer correctly but it seems you are looking for a sort of "weighted" random number generator. In essense what you want to do is give the problems they are having issues with more weight. Perhaps create a class called questions with a property of weight in it. That property can hold how much weight you put in it. Then when you select a random number generator use something like this.
http://codetheory.in/weighted-biased-random-number-generation-with-javascript-based-on-probability/
After doing some research, I realize that my initial method of calculating a weight is bit slow. After using the formula, I end up with some -ve weights. I then have to go through each one and add ABS(MIN(S)) to each weight, which is unnecessary.
My new formula would be S = CEILING(Wrong * 5 / Right)
Obviously I'd need to account for 0 values, so the code would be:
if (R == 0 AND W == 0) S = 10
else if (R == 0) S = W*5
else if (W == 0) S = CEILING(5/R)
else S = CEILING(W * 5 / R)
I've worked out the numbers for a few sample sets and this gives me fairly good results. It also allows me to keep the SCORE value updated in the database, so it doesn't need to be recalculated every time (just updated whenever that question is answered)
Once I have a set of 60 or so questions and I want to choose 5 or 10 of them, I can just create a random # between 1-SUM(SCORE) and then use a binary search to figure out which question that represents.
If anyone has a better suggestion for calculating the score/weight/bias or whatever it's called, I'd appreciate it.
I've been trying for several hours to make a random walk (a path) like this one.
From top to down.
x 1 x
x 2 3
x x 4
7 6 5
8 x x
9 10 x
My greatest difficulty is to calculate the displacement from right to left because the cycles (for, while..) go from left to right.
I am not proficient in math, so I'm using a simple approach.
I have two arrays. One with the position of the previous row.
$previousRow=array(1=>"x",2=>"1",3=>"x");
One with the current row I have to fill.
$currentRow=array(1=>"",2=>"",3=>"");
$p //Is the current position. 1, 2 or 3. Example $currentRow[$p]
$last //the last number that increases each time the path has a new step.
I'm using some cycles and conditions to set the displacement.
Is this approach wrong?
EDIT: further specifications as requested from comments:
Start point is located in the middle point of the first row
End point is located in the last row
End point can be located in any column of the last row
per each field you have three possibilities: left, right, forward.
some cases reduce this, e.g. there is no field to the left or right or that field was visited already.
so find out about possible moves, pick one at random and go on.
Does anyone know of a good resource on how to create the hyperlink numbers at the bottom of a results page as search engines do to load the next number of results?
The page would load the first 10 results. And then if you click on the number, it loads corresponding results in that 10 number range.
Example:
0-10 -> show no numbers
11-20 -> show 1, 2
21-30 -> 1, 2, 3
up to 50
anything more than 50 does 1,2,3,4,5.....67 [last number].
My thoughts so far (I'm doing this in PHP/mysqli but the logic is more important than the code):
$total = mysqli_num_rows($result) //total number of reqults from sql query
if ($total>10) {
$last = intval($total/10) + 1 //get the last number of the results
if($last <= 5){
for ($i, $i<$last, $i++){
//print the numbers as hyperlinks
} else {
//print 1 through 5 ... then $last
}
}
This though is static from only 1-5...last number while the search engines have it so if you click on the number, it remembers that number and bases the new logic on it. So if I click on the 5 in my formula, it should change to something like:
[previous] 3,4,5,6,7....67 [next]
And then I would just pass the number to the page itself again and limit the results based on what number was passed. Any suggestions also on the best way to pass the info?
You are looking for a pagination script. Visit this link The page is in Arabic but forth post is of pagination and you can download source for english or arabic version of pagination
Basically, you need two values to create a pagination, a limit and a offset.
1.The limit is the amount of items your are displaying at the same time.
2.The offset is from where you started your query.
So, let's say you have 5 items in each page and 25 items total.
In your query, you have to limit 5,0 (the amount of items and the position the query will start).
Now, if you divide 5(limit)/25(total) and you'll get 5 (amount of pages).
Now in page 0 (the start) you can get the offset by multiplying the page number with the limit, so 0 (page) * 5 (limit) gives you 0 (in the first page you start from the offset 0).
Now in the 3rd page, you multiply 3(page) * 5 (limit) it gives you 15, which means in page 3 (or four if you take into account that you actually started at page 0) you will display from offset 16 to 20.
Finally in page 4 (which to your users will be page 5 because they started at page 1, not page 0) you will display from offset 21 to 25 which are all the items in your query.