I'm getting an array value from external source and I'm converting it to into unordered list to display on webpage. But sometime contents of the list are very large to display on the single page. Is it possible to paginate the content so that it can show 10 list item per page.
<div id="a">
<div id="object3">TRIVIA</div>
<div class="details">
<?php foreach ($tri as $key): echo "<ul><li>$key</li></ul>"; endforeach ?>
</div>
</div>
Yes, but it requires actually writing a paginator.
You will need, however, to be able to get a count, and be able to display a range of results based on a url string like ?start=10&end=20.
The rest is simply using either a for loop or a foreach with a (if(count<start)continue;) and (if(count>end)break;)
You also need to generate links to go forward and back at the very least, and to not generate a link if you are at the start or end of the total count.
The only piece of information you need to pass in to your script is the page number, since that is the only thing that changes between pages.
There are other bits of information you need to know, such as numberPerPage, but they are included in your script and never change.
Here is a MySQL statement that will get you started:
SELECT columnList
FROM table
ORDER BY someColumn
LIMIT 10,5
The LIMIT clause says skip the first 10 records, then give me the next 5. This is the foundation and starting point for all pagination scripts that utilize a MySQL backend.
So how do you know the offset?
By that external piece of information I mentioned earlier, the current page number, combined with the internal piece of information I also mentioned earlier, the number per page.
If you are wanting to view page 5 and you intend to show 10 elements per page, then it is pretty easy to see that you want to start at row #41:
Page 1 showed elements 1-10
Page 2 showed elements 11-20
Page 3 showed elements 21-30
Page 4 showed elements 31-40
Page 5 needs to show elements 41-50
You can calculate the offset then this way:
$offset = $numPerPage * ($curPage - 1)
On Page 1, the offset will be 0 (which is what you want).
On Page 2, the offset will be 10, and so on.
Here is a link to a more thorough tutorial on pagination.
PEAR's Pager package has been made for this task. See the examples.
Related
With my previous posts
1. PHPSpreadsheet generates an error "Wrong number of arguments for INDEX() function: 5 given, between 1 and 4 expected"
2. Excel - Getting the Top 5 data of a column and their matching title but produces duplicates
I have found out that the PHPSpreadsheet library for PHP is yet to allow the usage of the AGGREGATE() and complicated formulas/functions but I'm in dire need of their functionalities
Going back, I have 2 columns in my Excel (produced by my web applications made from CodeIgniter and Laravel)
The problem is, the Article Count column (on the right) contains 2 values of 54 which is supposed to belong to 2 different Publications (on the left) but with the use of the formula =INDEX(E$4:E$38,MATCH(M4,J$4:J$38,0)) it just fetches the 1st matched Publication.
The output should look like this:
The original Table:
My question is, what would be the right function or code in Excel so I could retrieve the SECOND Publication of my matched data?
I'm aiming to target those Publications that has the Article Count of 54, but I want to aim the SECOND ONE which is the letter D WITHOUT using the Aggregate() function of Excel
Here are my used codes
1) =LARGE(J4:J38,1) - J4:J38 is my range of raw data, I am using this to get the 5 highest numbers in descending order
2) =INDEX(E4:E38,MATCH(M4,J4:J38,0)) - I'm using this to retrieve the Publication Names that matched the Article Count
After communicating in chat, we got this correct formula:
=INDEX(E$2:E$38,IF(M4=M3,MATCH(L3,E$2:E$38,0),0)+MATCH(M4,OFFSET(J$2,IF(M4=M3,MATCH(L3,E$2:E$38,0),0),0,COUNT(J$2:J$38)-IF(M4=M3,MATCH(L3,E$2:E$38,0),0),1),0))
How this works:
This IF(M4=M3,MATCH(L3,E$2:E$38,0),0) returns the position of the previous row's publication title in the titles array (E), in case the current publication count is the same with the previous one. Let's call this number X. Instead of using J2:J38 for the results, we use J(2+X):J38. This trick is done by using offset to cut off the previous section, already used by the previous row. This way, on repeating publication counts the already mentioned titles get ignored.
You need to use AGGREGATE's SMALL sub-function to return the smallest matching row number and adjust the k argument to accommodate duplicate rankings.
'in M4
=LARGE(J$4:J$38, ROW(1:1))
'in L4
=INDEX(I:I, AGGREGATE(15, 7, ROW($4:$38)/(J$4:J$38=M4), COUNTIF(M$4:M4, M4)))
enter image description here
Today i'm working on my website, trying to display the last winner of the game. Here's my code :
$lastgame = fetchinfo("value","info","name","current_game");
$winnercost = fetchinfo("cost","games","id",$lastgame-1);
$winnerpercent = round(fetchinfo("percent","games","id",$lastgame-1),1);
$winneravatar = fetchinfo("avatar","users","steamid",$lastwinner);
$winnername = fetchinfo("name","users","steamid",$lastwinner);
echo '<p>Game #'.$lastgame.'</p>';
echo '<p>'.$winnername.' won the jackpot</p>';
echo '<p> valued at '.$winnercost.'</p>';
echo '<p>with a winning chance of '.$winnerpercent.'</p>';
The point is i use fetchinfo only, so it displays informations but not in real time, i have to refresh my page to display the latest winner. I'll need to make a mysql_query i guess.
My problem is that i don't understand how to use the mysql_query, knowing that each time a winner wins it creates a new row in my table. For example :
id : 1
startime :1441330544
total price : 3.17
Winner : Foxy
steam id : 76561198042016725
Percent chances to win : 98.7381703
Number of total item : 2
module : 0.24973750
Anyone has a solution to help me ? this -1
,$lastgame-1),1);
gives me some difficulties :(
The result on the website atm :
Game #4
Foxy won the jackpot
valued at 3.31
with a winning chance of 94.6
Based on the information you provided, I think the simplest query you could use would be something like:
select * from <tblname> order by steamid desc limit 1;
For the auto refresh you could do one of two things:
1) you could add: <meta http-equiv="refresh" content="5"> in the header of your html and the page will automatically refresh every 5 seconds. (basic)
2) you could use ajax to make a call to execute the query and update the page which is a much nicer user experience (more advanced)
Make an api page with a php or any other preferred serverside programming web language that when called gives you the most up to date query results it can.
Then create a loop in javascript that every x amount of seconds sends an ajax request to the api page and using that data,you can dynamically update your fields with jquery or js (really doesnt matter - you probably know how to update/change text)
Here some resources:
ajax getting started
link 1
more ajax
link 2
On my search engine I get results via xml from blekko.com
I use: http://blekko.com/ws/?q=google/rss
And adding: &p= *number*
I can get different pages of results, e.g.
Page 1, 1 - 15 results
Page 2, 15 - 30 results
Like at the bottom of google, however I also get a number of total results
With those two (total & page number) is there any way to generate the right amount of links at the bottom for a next button, page1 results, page 2 results ect...
$count is the total and there are 15 results per page
If I understand you correctly you want something like:
for($i; $i<10 && $i<$totalPages; $i++)
{
echo ''.$i.'';
}
?
Not sure of what you want to do but usually to create links to different pages of results you need the total number of results and the nb of results you want to display per page. You then make a simple division to get the number of pages.
I am trying to implement the pagination in php. I am using the Mysql as back end database. I am trying to implement the pagination logic.
I would be having lots of record. But the user will see only 10 at a time.
Now to show the first page, i do a
SELECT * from USERS LIMIT 10.
Now to get the next 10 and the subsequent 10 records i am not able to write a query. Please help me fetch the in between records to support pagination logic. Also provide if any other suggestions for pagination.
You should use the OFFSET option.
SELECT * FROM Users LIMIT 10 OFFSET 10 (or 20, or 30);
That way you just pass the start position in the request when you hit next (or the page number) and you'll retrieve the records you want.
MySQL's limit feature can take two arguments:
select * from USERS limit 10,10
The above would retrieve 10 rows starting at row 10. Bear in mind that the MySQL row offset is 0 based, not 1. The first argument is the starting row, the second is the page size.
Also, if your page size is consistent, all you need to do is pass in the current page (default to zero). That would then allow you to specify the start row as a page * size.
I mean what the most efficient way to get information about the quantity of your page's items and make sql query with LIMIT that you need. or I should get all items and then crop array with php functions?
now I do 2 queries: first to count all items and second to get items that I need with LIMIT.
OK, I'll be more concrete. For example I need to show a question on my page and 20 answers to this question. At the bottom there shold be page control: links to the next, prev page and so on. I want to show proper number of links (number of answers/20) and when I go to any link I want to recieve proper answers (for example 41 to 60 on the 3d page). So what's the best way to get number of items (answers) to show proper number of links and to get proper answers for each link?
I guess your'e trying to say you want to know how many items/answers there is in the query but only read up to 20 items at at time, for pagination.
Firstly: You really should look for a pagination package; lots and lots of people have had the same problem before and there probably exists both free/opensource and proprietary solutions for your programming language and framework. (If you say what language you are using I'm sure someone can reccomend a solution for you.)
Anyway, I know I like to know how things work, so this is how it usually does:
As far as I know the pagination code calculates the pages by doing one query using select count(*) from tblX where something divide this number with the items-per-page number and use ceiling (e.g. 4.1 => 5).
For listing the results per page a new query is required; don't worry the count query is terribly much faster than getting every result discarding the ones you don't need DO NOT DO THAT (that's the recipie for becoming the top story on this page). Something like select * from tblX where something limit Y offset Z where Y is the number of items per page, and Z is the the (requested_page - 1)*Y; page 1 will have offset 0, page 2 have offset 20 (if thats what Y are) etc..
But do not try to implement this manually, it's unneccesary, tedious and error prone, much better to use your time customizing a readymade solution.
I'm assuming you want a count of the number of rows you'll be reading so as to do some pagination or similar? I don't understand your need for the LIMIT in the context of your question. However, if you just want a count of how many rows have been found, use one of the following.
You select the count of all rows such as:
select count(*) as counted, name, address
from contact
Or found rows:
SELECT SQL_CALC_FOUND_ROWS, name, address
from contact
This may be mysql specific I'm not sure.
Update:
For pagination you would do something like the following - (Psuedocode)
$rows = array($result)
$num_rows = sql_calc_found_rows
$per_page = 20
$pages = ceil($num_rows / $per_page)
page
$rows_this_page = array()
$rows_this_page = get_values($rows, (min index)$page_number * $per_page - $per_page, (max index)$page_number * $per_page - 1)