Use PHP to create new pages? [closed] - php

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 have an equipment database. There are 972 different items in that database. Including an image url, asset number, title, description, shipping weight, etc. I have a php script that creates a table for each item. However, I don't want all 972 items to load on one page. Is there a way to use PHP to set it so that every 10 items, or tables, it creates a new page with the next 10 items, then another page, and another until it hits the last item? I have to present the site to my boss Friday. Any help would be greatly appreciated!

To do pagination with PHP/MySQL, you need to do the following:
Provide the page requested via the URL query string (e.g. ?page=1)
Use LIMIT and OFFSET in your SQL query to show a different "page"
Maintain the ORDER BY portion of your query so the results are in the same order on each page
Optionally have a mechanism for determining what the last page is, and prevent viewing of pages beyond that last page.
To get you on the right track, your code should look something like this:
$limit = 10; // This never changes
$page = $_GET['page'];
if(!$page || !is_numeric($page) || $page < 1) {
$page = 1;
}
$offset = ($page - 1) * $limit; // Start at record 10 instead of record 0
$results = $my_db_object->getResults("SELECT * FROM equipment ORDER BY id LIMIT $limit OFFSET $offset");

Related

count pages round up PHP [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 4 years ago.
Improve this question
i've got a album site for photos but i want to count the amount of photos from the Database with the amount of images each page can be changed by Admin in PHP.
PHP Code
<button>Last page</button>
count($view) = 5
count($view) = 4
so he counts 1.25 and goes to page 1 as last page. but on page 2 is still one photo. so the last page is 2.
in an other example i got
count($view) = 20
count($view) = 4
Then he is going to page 5 correctly because it is rounded to the good amount.
Anyone knows a function?
Figuring out paging is relatively simple.
Imagine you have 42 records, and display ten per page.
$numPerPage = 10; // this is your limit clause in sql
$page = $_GET['page'] ?: 1; // default to 1 if not set.
$offset = ($page * $numPerPage) - $numPerpage;
$totalPages = ceil($numRecords / $numPerPage);
Offset eg page 3 = (10 * 3) - 10; // offset 20
TotalPages eg 42/10 = 4.2, ceiling makes it 5
So then in your SQL:
SELECT xFROM y WHERE z='a' OFFSET $offset LIMIT $numPerPage

PHP: MYSQL: Getting entries which are "hot / on the rise" [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 6 years ago.
Improve this question
I´m trying to get entries from my mysql database which represent a certain "hotness". Let´s say those entries are music tracks which carry attributes like "played" (how many times this track was played) and the attribute "added" (when the track was added (in timestamp format)). I already have the category "newest", which filters for the added-attribute and the category "top" which filters for the views-attribute. Now I need the category "hot" which should combine both. I came up with this formula: hotness = views / lifetime. So if a track has been played a lot of times but added recently it might be hot. And the other way round. Does that make sense? Anyway: How can I create a sql query in php which gives me the entries which have the highest "hotness"?
My query for the top-category:
$sql = "SELECT * FROM tracks ORDER BY views DESC LIMIT 0,35";
query for new-category:
$sql = "SELECT * FROM tracks ORDER BY added DESC LIMIT 0,35";
Thanks in advance.
To be really able to track the hotness, you have to have a history of when which track was played.
The total number of times a track was played and the date it was added added only allows for querying for all-time popular. The case that a track was added years ago but is getting popular now cannot be distinguished from it was added years ago, was popular back then but is not anymore.
You could add a new table where you store a date, a track ID and the number of times it was played on that date. Then you can query for all those tracks that were played a lot in the last day, 10 days and so on.

How can I Use the while loop until i get 3 rows from a table in mySQL? [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 want to retrieve content content based on their time stamp. And my query is
SELECT link,title,timestamp,photo,link,author,comments FROM posts
WHERE timestamp='*Todays Date -1*' LIMIT 6
If the query returns zero rows, how can I query for results 2 days back or three days back using a while statement?
Why not just query full table (without timestamp filter) and return limit of 6 rows. in this case there would be no reason to iteratatively query
SELECT link,title,`timestamp`,photo,link,author,comments
FROM posts
ORDER BY `timestamp` DESC
LIMIT 6
This gives you up to 6 records with most recent records returned first. You should have index on timestamp field for this query, which is OK because you should already have one because you were previously trying to filter on this field.
If you have a case where more than 3 rows show up within last day and you don't need to show records from previous days, this is easy enough to achieve by inspecting the values as you loop through the result set.
This also prevents you from getting into an infinite loop if you only have 2 records in the database.
Also note timestamp is a reserved word, you should be careful when using such field names. If you have to use a reserved word as field name, you must enclose it with backticks.
maybe a for could work
$i=1
for ($row='';$count=0;$i++) {
$result = mysql_query("SELECT link,title,timestamp,photo,link,author,comments FROM posts WHERE timestamp='*Todays Date -$i *' LIMIT 6");
$count = mysql_num_rows($result);
}
I think the query that you want both filters the results and orders them.
SELECT link, title, timestamp, photo, link, author, comments
FROM posts
WHERE timestamp <= '*Todays Date -1*'
ORDER BY timestamp desc
LIMIT 6;
If you actually want 3 rows and not 6 (as suggested by the post title), then change the 6 to 3.

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

Why when i select count with php i get just 1 instead of .. 55 for example || PHP Count, From last 100 [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
$nr333 = mysql_query("SELECT COUNT(*) AS cnt FROM (
SELECT * FROM games
WHERE human = '".mysql_real_escape_string($_GET[human])."'
ORDER BY id DESC LIMIT 100
) tmp WHERE changed = 'y'", $link) or die(mysql_error());
$frecventa333 = mysql_num_rows($nr333);
so bassicaly i dont get any error but .. instead of getting the real number i get just 1:|
http://s017.radikal.ru/i414/1310/a2/37958f7cdb48.png
That's because COUNT returns only one row, always. But in that row you'll find field with all rows counted, in one integer.
Try to fetch that row.
And next thing you should do is checking PDO extension. It's better than deprecated mysql_* functions and isn't so hard to learn.

Categories