count pages round up PHP [closed] - php

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

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

how to display limited products per page using php [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 am creating my first ever project on the topic "online mobile store" on NETBEANS IDE using PHP and MySQL as backend,
on a page products.php I want to display 1st 10 products, then by clicking on NEXT button next 10 products will display....
Please tell me how to do this?
You need to use the LIMIT sentence on the Mysql query, like this:
SELECT * FROM table LIMIT 0,10
The first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return
You will need to increase by 10 the first number using PHP.
For example, if you want the second page (from 10 to 20) your query should be:
SELECT * FROM table LIMIT 10,10
https://dev.mysql.com/doc/refman/5.0/en/select.html
First of all, PHP and MySQL are both backends. HTML/CSS/JS is for frontend.
Second, the thing is achieved by altering your query using LIMIT.
For first ten : mysql_query(".... LIMIT 0, 10");
For second ten: mysql_query(".... LIMIT 10, 20");
... and so on
Where you get the START and OFFSET from your URL like this:
example.com/article.php?page=0 -> LIMIT 0, 10
example.com/article.php?page=1 -> LIMIT 10, 20
I leave the algorithm to you ;)

Use PHP to create new pages? [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 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");

How to get a percent value [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 8 years ago.
Improve this question
Okay, so for my website, I made a tests system, where you can take quizes. It all works, but getting the percentage of correct answers is broken.
I have 3 variables I am trying to use to get the percentage.
$incorrect // Int value of incorrect answers
$correct // Int value of correct answers
$perc // Percentage of correct answers from 0 to 100
Currently, I use this, but it does not work:
if($incorrect===0){
$perc = 100;
}elseif($correct===0){
$perc = 0;
}else{
$perc = ($incorrect / $correct)*100;
}
$perc = $correct*100/($incorrect+$correct)
User1950001, as Barmar mentioned, you need to calculate the TOTAL number of answers as a percentage is calculated by (NUMBER OUT OF TOTAL x 100 / TOTAL).
In this case, CORRECT. is your NUMBER OUT OF TOTAL and INCORRECT + CORRECT is your TOTAL, which is what you were missing.
With your original code, perc = ($incorrect/$correct) * 100 where total questions is 25, 20 are incorrect and 5 are correct would return 400% as the value. Obviously this is way off.

Listing photos with 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 8 years ago.
Improve this question
I have a problem and did not solve.
What i want is that listing photos side to side
like that:
The problem is that when i have 50 photos , this happens
What i want is that listing ten photos per columns. How can i do that ?
When looping to display the images you need to break every 10 photos like so:
$photosPerLine = 10;
for ( var $i = 0; $i < $totalNumPhotos; $i++ )
{
drawPhoto(); // This does the actual drawing - perhaps echo a <IMG> or whatever
// Now we check if we've reached 10 in a row, we do this by dividing the
// photo counter by 10 and checking for a reminder, only numbers in leaps
// of 10 will divide without a reminder. If we don't have a reminder it means
// we're at 10, 20, 30 ... so we break the line
if ( ( $i + 1 ) % $photosPerLine == 0 )
{
echo( '<br/>' ); // Or create a new row in a table or whatever
}
}
Or just place the images in a container (<div> for example) with a specified width to hold exactly 10 images and let the browser break lines to fit the content.

Categories