php and mysql pagination help [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I need to make a pagination like this that displays the number of data from a table in my database like this:
1 of n pages
Can anyone recommend a good tutorial for this? Thanks.
It's not a link, it just has to indicate the number of rows inside the database.

You can calculate the total number of rows during a select by adding the SQL_CALC_FOUND_ROWS flag to your query, and following it up with a second SELECT statement:
SELECT SQL_CALC_FOUND_ROWS * FROM tablename WHERE criteria='here' LIMIT 0,25;
SELECT FOUND_ROWS();
By knowing that you've got 25 rows on the first page (in the LIMIT), and knowing how many total rows there are, you can calculate how many pages there should be.

Related

php quiz how to query 10 rows based on levels [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a table(quiz). And the fields are id,question,answer, and level.
There Many questions for level 1, many questions for level 2 and so on. Please how do i query 10 rows based on the levels consecutively i.e
question1(level 1)
question2(level 2)
question3(level 3)
..to
question10(level 10)
this is what i have so far:
$con->prepare("SELECT*FROM quiz WHERE level IN (1,2,3,4,5.....10) limit 0,10");
but the result i get is only 10 questions on level 1. Please i also want it to be random
Here's an attempt, though perhaps it needs a subquery:
SELECT * FROM quiz
WHERE level BETWEEN 1 AND 10
GROUP BY level
ORDER BY RAND(), level
Second attempt:
SELECT * FROM
(
SELECT * FROM quiz
WHERE level BETWEEN 1 AND 10
ORDER BY RAND()
) tmp
GROUP BY level
ORDER BY level

Get the closest value of a column in MYSQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a table that have a CP value (numeric), for example, 28030, 28060, 27100 etc. And the user can introduce a number via PHP. I want to, having this number for example, 28050, order in MYSQL my table putting 28060 as the first position.
This is the basic of my table:
SELECT * FROM `tiendas` ORDER BY `CP`
ABS() will work. Here's a query that does the job:
SELECT
CP
FROM tiendas
ORDER BY ABS(CP- 28050) ASC

How to create a Popularity parameter based on hits, likes, dislikes and time [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I got a database with a lot of articles created through time. Now I want my script to modify a "popularity" field in the database based on hits, likes, dislikes and time.
How would you do that? The older the article the less relevant of course. But if the article is two weeks old BUT got a lot of hits and likes I want it to show up even though.
Any ideas?
Considering you already have all the data, I would calculate the popularity like follows (in pseudo code):
Popularity = (1 / now - time) + likes - dislikes.
So in MySQL query it would be something like this:
UPDATE articles SET
popularity = (now() - article.timestamp) + article.likes - article.dislikes;
After the query is run and the data updated, you could apply the sorting on your data to fetch the most popular artcles:
SELECT * FROM articles
ORDER BY popularity DESC.
Hope this helps.

MySQL query to display one record before today's date [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I can't find an answer to my question, it's probably easy but... anyway!
I have a database with every NHL game for one specific team in a table. Every game has been entered in the good order.
On my home page, I would like to display the upcoming game as well as the result of the previous game. How can I create a mySQL query to display the previous game based on today's date?
Thanks!
Do your game records have a timestamp or datetime value?
If so you could write a query ordering your games by the date smaller that now() and limit by one.
The query should look like this:
select * from games where gamedate < now() order by gamedate desc limit 1

Need a little guidance on counting and calculating sum of rows. [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm making a little voting tool (voting from 1 to 5) and I have the votes stored in a DB. I'm wondering, what is the best manner to count all the rows (of a specific post ID) and then calculate the sum of those specific rows so that I can display the results.
i.e
Results: 3.5 / 5
(total votes: 1047)
Should I use a PHP loop or MYSQL sum?
SELECT AVG(vote), COUNT(vote) FROM vote_table WHERE post_id = 1234;
The first. column gives your average out of 5 and the count the total votes. To get all the stats at once:
SELECT AVG(vote), COUNT(vote), post_id FROM vote_table GROUP BY post_id;

Categories