Split all images over x amount of pages with $_GET
up vote
2
down vote
favorite
I would like to receive some feedback on the code I am currently using to display x amount of images on my website per
$success = true;
$page = array_key_exists('page', $_GET) ? $_GET['page'] : 1;
$all_pictures = glob('crossles/*.jpg');
$total_images = 0;
foreach ($all_pictures as $img_total) {
$total_images++;
}
$images_per_page = 30;
$max_pages = ceil($total_images / $images_per_page);
if ($page <= -1) {
$success = false;
}
if ($page > $max_pages) {
$success = false;
}
if ($success === false) {
echo "<div class='container text-center'><p class='text-danger fs-32 mt-100'>No pictures to be displayed.</p></div>";
}
I know that $_GET
is not the safest way of doing this but since it is only to retrieve images I don't think it is that big of an issue.
It would be nice if I could get some feedback to implement it to my code if needed.
php validation image
New contributor
add a comment |
up vote
2
down vote
favorite
I would like to receive some feedback on the code I am currently using to display x amount of images on my website per
$success = true;
$page = array_key_exists('page', $_GET) ? $_GET['page'] : 1;
$all_pictures = glob('crossles/*.jpg');
$total_images = 0;
foreach ($all_pictures as $img_total) {
$total_images++;
}
$images_per_page = 30;
$max_pages = ceil($total_images / $images_per_page);
if ($page <= -1) {
$success = false;
}
if ($page > $max_pages) {
$success = false;
}
if ($success === false) {
echo "<div class='container text-center'><p class='text-danger fs-32 mt-100'>No pictures to be displayed.</p></div>";
}
I know that $_GET
is not the safest way of doing this but since it is only to retrieve images I don't think it is that big of an issue.
It would be nice if I could get some feedback to implement it to my code if needed.
php validation image
New contributor
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I would like to receive some feedback on the code I am currently using to display x amount of images on my website per
$success = true;
$page = array_key_exists('page', $_GET) ? $_GET['page'] : 1;
$all_pictures = glob('crossles/*.jpg');
$total_images = 0;
foreach ($all_pictures as $img_total) {
$total_images++;
}
$images_per_page = 30;
$max_pages = ceil($total_images / $images_per_page);
if ($page <= -1) {
$success = false;
}
if ($page > $max_pages) {
$success = false;
}
if ($success === false) {
echo "<div class='container text-center'><p class='text-danger fs-32 mt-100'>No pictures to be displayed.</p></div>";
}
I know that $_GET
is not the safest way of doing this but since it is only to retrieve images I don't think it is that big of an issue.
It would be nice if I could get some feedback to implement it to my code if needed.
php validation image
New contributor
I would like to receive some feedback on the code I am currently using to display x amount of images on my website per
$success = true;
$page = array_key_exists('page', $_GET) ? $_GET['page'] : 1;
$all_pictures = glob('crossles/*.jpg');
$total_images = 0;
foreach ($all_pictures as $img_total) {
$total_images++;
}
$images_per_page = 30;
$max_pages = ceil($total_images / $images_per_page);
if ($page <= -1) {
$success = false;
}
if ($page > $max_pages) {
$success = false;
}
if ($success === false) {
echo "<div class='container text-center'><p class='text-danger fs-32 mt-100'>No pictures to be displayed.</p></div>";
}
I know that $_GET
is not the safest way of doing this but since it is only to retrieve images I don't think it is that big of an issue.
It would be nice if I could get some feedback to implement it to my code if needed.
php validation image
php validation image
New contributor
New contributor
edited Nov 13 at 18:12
Sᴀᴍ Onᴇᴌᴀ
7,67561748
7,67561748
New contributor
asked Nov 13 at 17:52
Mark_Ed
132
132
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
If you are concerned about using GET data then consider using POST data. You could also consider using filter_input()
or filter_var()
with FILTER_VALIDATE_INT
.
$total_images = 0;
foreach ($all_pictures as $img_total) {
$total_images++;
}
This entire block could be replaced by a call to count($all_pictures)
$total_images = count($all_pictures);
And that variable could be eliminated by calling count()
inline:
$max_pages = ceil(count($all_pictures) / $images_per_page);
if ($success === false) {
While this is totally fine, idiomatic PHP code simply checks for a value that equates to false
, often on the side of brevity:
if (!$sucess) {
The conditionals could also be combined, eliminating the need for $success
:
if ($page <= -1 || $page > $max_pages) {
echo "<div class='container text-center'><p class='text-danger fs-32 mt-100'>No pictures to be displayed.</p></div>";
}
Just to be sure$max_pages = ceil(count($total_images) / $images_per_page);
instead of the foreach loop? And for the$success === false
I totally forgot the ! operator. Thanks for the tips will use them!
– Mark_Ed
Nov 13 at 18:48
yes - I updated it for clarity
– Sᴀᴍ Onᴇᴌᴀ
Nov 13 at 18:50
Now I have this$max_pages = ceil(count($total_images) / $images_per_page);
but when I implementif ($page <= -1 || $page > $max_pages) { echo "<div class='container text-center'><p class='text-danger fs-32 mt-100'>No pictures to be displayed.</p></div>"; }
I get the message 'No pictures to be displayed' when the url is websiteurl.nl/imagegallery.php Without a page after it.
– Mark_Ed
Nov 13 at 18:54
I see the problem, the$max_pages = ceil(count($total_images) / $images_per_page);
gives me afloat(0)
if Ivar_dump($max_pages)
– Mark_Ed
Nov 13 at 18:56
Nvm, fixed it :D
– Mark_Ed
Nov 13 at 19:20
add a comment |
up vote
1
down vote
$total_images = 0;
foreach ($all_pictures as $img_total) {
$total_images++;
}
This could be just
$total_images = ($all_pictures === false) ? 0 : count($all_pictures);
or
if ($all_pictures === false) {
$total_images = 0;
$success === false;
} else {
$total_images = count($all_pictures);
}
The glob
function returns an array, so you can just do the normal array operations on it. You only have to check for an error. I'm not quite sure what your original code would have done if there was an error. You might want to check later so you can say something like "Oops, there was an error looking for images!" rather than "No images".
if ($page <= -1) {
This is a weird way to write this. First, what would you do if page were set to -.5 here? Second, what does a page value of 0 mean? You are defaulting to 1, so it would seem that you are 1-indexed. So why accept anything less than 1? Consider
if ($page < 1) {
That eliminates all the invalid values below the range.
For much the same reason,
$page = array_key_exists('page', $_GET) ? $_GET['page'] : 1;
should probably be
$page = array_key_exists('page', $_GET) ? intval($_GET['page']) : 1;
Get parameters can be user input, so you should sanitize them to what you expect.
echo "<div class='container text-center'><p class='text-danger fs-32 mt-100'>No pictures to be displayed.</p></div>";
This is an odd way to write a string in PHP. Using double quotes indicates that there is a value to interpolate in it. But you don't have one here. This could just as well be
echo '<div class="container text-center"><p class="text-danger fs-32 mt-100">No pictures to be displayed.</p></div>';
or
?>
<div class="container text-center">
<p class="text-danger fs-32 mt-100">No pictures to be displayed.</p>
</div>
<?php
Consider moving the whole thing into a function or even a class.
class Page_Handler {
private $image_count;
private $success = true;
private $all_pictures;
private $max_page_number;
private $page;
private $images_per_page = 30;
function __construct($path = 'crossles/*.jpg') {
$this->all_pictures = glob($path);
$this->success = $this->parse();
}
private function parse() {
if ($this->all_pictures === false) {
return false;
}
$this->image_count = count($this->all_pictures);
$this->max_page_number = ceil($this->image_count / $this->images_per_page);
$this->page = array_key_exists('page', $_GET) ? $_GET['page'] : 1;
if ($this->page < 1 || $this->page > $this->max_page_number) {
return false;
}
return true;
}
There would also be getters and possibly other helper methods. I'll leave those as an exercise for the reader (mostly because I'm lazy). Note that you could differentiate between a failure to load the images and an invalid page. Those are two separate problems and you may want to message them differently, as one is a server error and the other is user error. Page number out of range is often a problem that the user can fix.
I changed the names of some of the variables. I generally use a system where plural names indicate collections and singular names indicate scalars. You don't have to follow the same system, but it's the one I use when writing code.
One of the differences here is that the parse code doesn't have to keep processing after an error. At the first error, it returns failure.
Hey, thanks for the brief explanation of everything, I am going to use a few of your things in my code!
– Mark_Ed
Nov 13 at 19:02
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
If you are concerned about using GET data then consider using POST data. You could also consider using filter_input()
or filter_var()
with FILTER_VALIDATE_INT
.
$total_images = 0;
foreach ($all_pictures as $img_total) {
$total_images++;
}
This entire block could be replaced by a call to count($all_pictures)
$total_images = count($all_pictures);
And that variable could be eliminated by calling count()
inline:
$max_pages = ceil(count($all_pictures) / $images_per_page);
if ($success === false) {
While this is totally fine, idiomatic PHP code simply checks for a value that equates to false
, often on the side of brevity:
if (!$sucess) {
The conditionals could also be combined, eliminating the need for $success
:
if ($page <= -1 || $page > $max_pages) {
echo "<div class='container text-center'><p class='text-danger fs-32 mt-100'>No pictures to be displayed.</p></div>";
}
Just to be sure$max_pages = ceil(count($total_images) / $images_per_page);
instead of the foreach loop? And for the$success === false
I totally forgot the ! operator. Thanks for the tips will use them!
– Mark_Ed
Nov 13 at 18:48
yes - I updated it for clarity
– Sᴀᴍ Onᴇᴌᴀ
Nov 13 at 18:50
Now I have this$max_pages = ceil(count($total_images) / $images_per_page);
but when I implementif ($page <= -1 || $page > $max_pages) { echo "<div class='container text-center'><p class='text-danger fs-32 mt-100'>No pictures to be displayed.</p></div>"; }
I get the message 'No pictures to be displayed' when the url is websiteurl.nl/imagegallery.php Without a page after it.
– Mark_Ed
Nov 13 at 18:54
I see the problem, the$max_pages = ceil(count($total_images) / $images_per_page);
gives me afloat(0)
if Ivar_dump($max_pages)
– Mark_Ed
Nov 13 at 18:56
Nvm, fixed it :D
– Mark_Ed
Nov 13 at 19:20
add a comment |
up vote
0
down vote
accepted
If you are concerned about using GET data then consider using POST data. You could also consider using filter_input()
or filter_var()
with FILTER_VALIDATE_INT
.
$total_images = 0;
foreach ($all_pictures as $img_total) {
$total_images++;
}
This entire block could be replaced by a call to count($all_pictures)
$total_images = count($all_pictures);
And that variable could be eliminated by calling count()
inline:
$max_pages = ceil(count($all_pictures) / $images_per_page);
if ($success === false) {
While this is totally fine, idiomatic PHP code simply checks for a value that equates to false
, often on the side of brevity:
if (!$sucess) {
The conditionals could also be combined, eliminating the need for $success
:
if ($page <= -1 || $page > $max_pages) {
echo "<div class='container text-center'><p class='text-danger fs-32 mt-100'>No pictures to be displayed.</p></div>";
}
Just to be sure$max_pages = ceil(count($total_images) / $images_per_page);
instead of the foreach loop? And for the$success === false
I totally forgot the ! operator. Thanks for the tips will use them!
– Mark_Ed
Nov 13 at 18:48
yes - I updated it for clarity
– Sᴀᴍ Onᴇᴌᴀ
Nov 13 at 18:50
Now I have this$max_pages = ceil(count($total_images) / $images_per_page);
but when I implementif ($page <= -1 || $page > $max_pages) { echo "<div class='container text-center'><p class='text-danger fs-32 mt-100'>No pictures to be displayed.</p></div>"; }
I get the message 'No pictures to be displayed' when the url is websiteurl.nl/imagegallery.php Without a page after it.
– Mark_Ed
Nov 13 at 18:54
I see the problem, the$max_pages = ceil(count($total_images) / $images_per_page);
gives me afloat(0)
if Ivar_dump($max_pages)
– Mark_Ed
Nov 13 at 18:56
Nvm, fixed it :D
– Mark_Ed
Nov 13 at 19:20
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
If you are concerned about using GET data then consider using POST data. You could also consider using filter_input()
or filter_var()
with FILTER_VALIDATE_INT
.
$total_images = 0;
foreach ($all_pictures as $img_total) {
$total_images++;
}
This entire block could be replaced by a call to count($all_pictures)
$total_images = count($all_pictures);
And that variable could be eliminated by calling count()
inline:
$max_pages = ceil(count($all_pictures) / $images_per_page);
if ($success === false) {
While this is totally fine, idiomatic PHP code simply checks for a value that equates to false
, often on the side of brevity:
if (!$sucess) {
The conditionals could also be combined, eliminating the need for $success
:
if ($page <= -1 || $page > $max_pages) {
echo "<div class='container text-center'><p class='text-danger fs-32 mt-100'>No pictures to be displayed.</p></div>";
}
If you are concerned about using GET data then consider using POST data. You could also consider using filter_input()
or filter_var()
with FILTER_VALIDATE_INT
.
$total_images = 0;
foreach ($all_pictures as $img_total) {
$total_images++;
}
This entire block could be replaced by a call to count($all_pictures)
$total_images = count($all_pictures);
And that variable could be eliminated by calling count()
inline:
$max_pages = ceil(count($all_pictures) / $images_per_page);
if ($success === false) {
While this is totally fine, idiomatic PHP code simply checks for a value that equates to false
, often on the side of brevity:
if (!$sucess) {
The conditionals could also be combined, eliminating the need for $success
:
if ($page <= -1 || $page > $max_pages) {
echo "<div class='container text-center'><p class='text-danger fs-32 mt-100'>No pictures to be displayed.</p></div>";
}
edited Nov 13 at 18:50
answered Nov 13 at 18:42
Sᴀᴍ Onᴇᴌᴀ
7,67561748
7,67561748
Just to be sure$max_pages = ceil(count($total_images) / $images_per_page);
instead of the foreach loop? And for the$success === false
I totally forgot the ! operator. Thanks for the tips will use them!
– Mark_Ed
Nov 13 at 18:48
yes - I updated it for clarity
– Sᴀᴍ Onᴇᴌᴀ
Nov 13 at 18:50
Now I have this$max_pages = ceil(count($total_images) / $images_per_page);
but when I implementif ($page <= -1 || $page > $max_pages) { echo "<div class='container text-center'><p class='text-danger fs-32 mt-100'>No pictures to be displayed.</p></div>"; }
I get the message 'No pictures to be displayed' when the url is websiteurl.nl/imagegallery.php Without a page after it.
– Mark_Ed
Nov 13 at 18:54
I see the problem, the$max_pages = ceil(count($total_images) / $images_per_page);
gives me afloat(0)
if Ivar_dump($max_pages)
– Mark_Ed
Nov 13 at 18:56
Nvm, fixed it :D
– Mark_Ed
Nov 13 at 19:20
add a comment |
Just to be sure$max_pages = ceil(count($total_images) / $images_per_page);
instead of the foreach loop? And for the$success === false
I totally forgot the ! operator. Thanks for the tips will use them!
– Mark_Ed
Nov 13 at 18:48
yes - I updated it for clarity
– Sᴀᴍ Onᴇᴌᴀ
Nov 13 at 18:50
Now I have this$max_pages = ceil(count($total_images) / $images_per_page);
but when I implementif ($page <= -1 || $page > $max_pages) { echo "<div class='container text-center'><p class='text-danger fs-32 mt-100'>No pictures to be displayed.</p></div>"; }
I get the message 'No pictures to be displayed' when the url is websiteurl.nl/imagegallery.php Without a page after it.
– Mark_Ed
Nov 13 at 18:54
I see the problem, the$max_pages = ceil(count($total_images) / $images_per_page);
gives me afloat(0)
if Ivar_dump($max_pages)
– Mark_Ed
Nov 13 at 18:56
Nvm, fixed it :D
– Mark_Ed
Nov 13 at 19:20
Just to be sure
$max_pages = ceil(count($total_images) / $images_per_page);
instead of the foreach loop? And for the $success === false
I totally forgot the ! operator. Thanks for the tips will use them!– Mark_Ed
Nov 13 at 18:48
Just to be sure
$max_pages = ceil(count($total_images) / $images_per_page);
instead of the foreach loop? And for the $success === false
I totally forgot the ! operator. Thanks for the tips will use them!– Mark_Ed
Nov 13 at 18:48
yes - I updated it for clarity
– Sᴀᴍ Onᴇᴌᴀ
Nov 13 at 18:50
yes - I updated it for clarity
– Sᴀᴍ Onᴇᴌᴀ
Nov 13 at 18:50
Now I have this
$max_pages = ceil(count($total_images) / $images_per_page);
but when I implement if ($page <= -1 || $page > $max_pages) { echo "<div class='container text-center'><p class='text-danger fs-32 mt-100'>No pictures to be displayed.</p></div>"; }
I get the message 'No pictures to be displayed' when the url is websiteurl.nl/imagegallery.php Without a page after it.– Mark_Ed
Nov 13 at 18:54
Now I have this
$max_pages = ceil(count($total_images) / $images_per_page);
but when I implement if ($page <= -1 || $page > $max_pages) { echo "<div class='container text-center'><p class='text-danger fs-32 mt-100'>No pictures to be displayed.</p></div>"; }
I get the message 'No pictures to be displayed' when the url is websiteurl.nl/imagegallery.php Without a page after it.– Mark_Ed
Nov 13 at 18:54
I see the problem, the
$max_pages = ceil(count($total_images) / $images_per_page);
gives me a float(0)
if I var_dump($max_pages)
– Mark_Ed
Nov 13 at 18:56
I see the problem, the
$max_pages = ceil(count($total_images) / $images_per_page);
gives me a float(0)
if I var_dump($max_pages)
– Mark_Ed
Nov 13 at 18:56
Nvm, fixed it :D
– Mark_Ed
Nov 13 at 19:20
Nvm, fixed it :D
– Mark_Ed
Nov 13 at 19:20
add a comment |
up vote
1
down vote
$total_images = 0;
foreach ($all_pictures as $img_total) {
$total_images++;
}
This could be just
$total_images = ($all_pictures === false) ? 0 : count($all_pictures);
or
if ($all_pictures === false) {
$total_images = 0;
$success === false;
} else {
$total_images = count($all_pictures);
}
The glob
function returns an array, so you can just do the normal array operations on it. You only have to check for an error. I'm not quite sure what your original code would have done if there was an error. You might want to check later so you can say something like "Oops, there was an error looking for images!" rather than "No images".
if ($page <= -1) {
This is a weird way to write this. First, what would you do if page were set to -.5 here? Second, what does a page value of 0 mean? You are defaulting to 1, so it would seem that you are 1-indexed. So why accept anything less than 1? Consider
if ($page < 1) {
That eliminates all the invalid values below the range.
For much the same reason,
$page = array_key_exists('page', $_GET) ? $_GET['page'] : 1;
should probably be
$page = array_key_exists('page', $_GET) ? intval($_GET['page']) : 1;
Get parameters can be user input, so you should sanitize them to what you expect.
echo "<div class='container text-center'><p class='text-danger fs-32 mt-100'>No pictures to be displayed.</p></div>";
This is an odd way to write a string in PHP. Using double quotes indicates that there is a value to interpolate in it. But you don't have one here. This could just as well be
echo '<div class="container text-center"><p class="text-danger fs-32 mt-100">No pictures to be displayed.</p></div>';
or
?>
<div class="container text-center">
<p class="text-danger fs-32 mt-100">No pictures to be displayed.</p>
</div>
<?php
Consider moving the whole thing into a function or even a class.
class Page_Handler {
private $image_count;
private $success = true;
private $all_pictures;
private $max_page_number;
private $page;
private $images_per_page = 30;
function __construct($path = 'crossles/*.jpg') {
$this->all_pictures = glob($path);
$this->success = $this->parse();
}
private function parse() {
if ($this->all_pictures === false) {
return false;
}
$this->image_count = count($this->all_pictures);
$this->max_page_number = ceil($this->image_count / $this->images_per_page);
$this->page = array_key_exists('page', $_GET) ? $_GET['page'] : 1;
if ($this->page < 1 || $this->page > $this->max_page_number) {
return false;
}
return true;
}
There would also be getters and possibly other helper methods. I'll leave those as an exercise for the reader (mostly because I'm lazy). Note that you could differentiate between a failure to load the images and an invalid page. Those are two separate problems and you may want to message them differently, as one is a server error and the other is user error. Page number out of range is often a problem that the user can fix.
I changed the names of some of the variables. I generally use a system where plural names indicate collections and singular names indicate scalars. You don't have to follow the same system, but it's the one I use when writing code.
One of the differences here is that the parse code doesn't have to keep processing after an error. At the first error, it returns failure.
Hey, thanks for the brief explanation of everything, I am going to use a few of your things in my code!
– Mark_Ed
Nov 13 at 19:02
add a comment |
up vote
1
down vote
$total_images = 0;
foreach ($all_pictures as $img_total) {
$total_images++;
}
This could be just
$total_images = ($all_pictures === false) ? 0 : count($all_pictures);
or
if ($all_pictures === false) {
$total_images = 0;
$success === false;
} else {
$total_images = count($all_pictures);
}
The glob
function returns an array, so you can just do the normal array operations on it. You only have to check for an error. I'm not quite sure what your original code would have done if there was an error. You might want to check later so you can say something like "Oops, there was an error looking for images!" rather than "No images".
if ($page <= -1) {
This is a weird way to write this. First, what would you do if page were set to -.5 here? Second, what does a page value of 0 mean? You are defaulting to 1, so it would seem that you are 1-indexed. So why accept anything less than 1? Consider
if ($page < 1) {
That eliminates all the invalid values below the range.
For much the same reason,
$page = array_key_exists('page', $_GET) ? $_GET['page'] : 1;
should probably be
$page = array_key_exists('page', $_GET) ? intval($_GET['page']) : 1;
Get parameters can be user input, so you should sanitize them to what you expect.
echo "<div class='container text-center'><p class='text-danger fs-32 mt-100'>No pictures to be displayed.</p></div>";
This is an odd way to write a string in PHP. Using double quotes indicates that there is a value to interpolate in it. But you don't have one here. This could just as well be
echo '<div class="container text-center"><p class="text-danger fs-32 mt-100">No pictures to be displayed.</p></div>';
or
?>
<div class="container text-center">
<p class="text-danger fs-32 mt-100">No pictures to be displayed.</p>
</div>
<?php
Consider moving the whole thing into a function or even a class.
class Page_Handler {
private $image_count;
private $success = true;
private $all_pictures;
private $max_page_number;
private $page;
private $images_per_page = 30;
function __construct($path = 'crossles/*.jpg') {
$this->all_pictures = glob($path);
$this->success = $this->parse();
}
private function parse() {
if ($this->all_pictures === false) {
return false;
}
$this->image_count = count($this->all_pictures);
$this->max_page_number = ceil($this->image_count / $this->images_per_page);
$this->page = array_key_exists('page', $_GET) ? $_GET['page'] : 1;
if ($this->page < 1 || $this->page > $this->max_page_number) {
return false;
}
return true;
}
There would also be getters and possibly other helper methods. I'll leave those as an exercise for the reader (mostly because I'm lazy). Note that you could differentiate between a failure to load the images and an invalid page. Those are two separate problems and you may want to message them differently, as one is a server error and the other is user error. Page number out of range is often a problem that the user can fix.
I changed the names of some of the variables. I generally use a system where plural names indicate collections and singular names indicate scalars. You don't have to follow the same system, but it's the one I use when writing code.
One of the differences here is that the parse code doesn't have to keep processing after an error. At the first error, it returns failure.
Hey, thanks for the brief explanation of everything, I am going to use a few of your things in my code!
– Mark_Ed
Nov 13 at 19:02
add a comment |
up vote
1
down vote
up vote
1
down vote
$total_images = 0;
foreach ($all_pictures as $img_total) {
$total_images++;
}
This could be just
$total_images = ($all_pictures === false) ? 0 : count($all_pictures);
or
if ($all_pictures === false) {
$total_images = 0;
$success === false;
} else {
$total_images = count($all_pictures);
}
The glob
function returns an array, so you can just do the normal array operations on it. You only have to check for an error. I'm not quite sure what your original code would have done if there was an error. You might want to check later so you can say something like "Oops, there was an error looking for images!" rather than "No images".
if ($page <= -1) {
This is a weird way to write this. First, what would you do if page were set to -.5 here? Second, what does a page value of 0 mean? You are defaulting to 1, so it would seem that you are 1-indexed. So why accept anything less than 1? Consider
if ($page < 1) {
That eliminates all the invalid values below the range.
For much the same reason,
$page = array_key_exists('page', $_GET) ? $_GET['page'] : 1;
should probably be
$page = array_key_exists('page', $_GET) ? intval($_GET['page']) : 1;
Get parameters can be user input, so you should sanitize them to what you expect.
echo "<div class='container text-center'><p class='text-danger fs-32 mt-100'>No pictures to be displayed.</p></div>";
This is an odd way to write a string in PHP. Using double quotes indicates that there is a value to interpolate in it. But you don't have one here. This could just as well be
echo '<div class="container text-center"><p class="text-danger fs-32 mt-100">No pictures to be displayed.</p></div>';
or
?>
<div class="container text-center">
<p class="text-danger fs-32 mt-100">No pictures to be displayed.</p>
</div>
<?php
Consider moving the whole thing into a function or even a class.
class Page_Handler {
private $image_count;
private $success = true;
private $all_pictures;
private $max_page_number;
private $page;
private $images_per_page = 30;
function __construct($path = 'crossles/*.jpg') {
$this->all_pictures = glob($path);
$this->success = $this->parse();
}
private function parse() {
if ($this->all_pictures === false) {
return false;
}
$this->image_count = count($this->all_pictures);
$this->max_page_number = ceil($this->image_count / $this->images_per_page);
$this->page = array_key_exists('page', $_GET) ? $_GET['page'] : 1;
if ($this->page < 1 || $this->page > $this->max_page_number) {
return false;
}
return true;
}
There would also be getters and possibly other helper methods. I'll leave those as an exercise for the reader (mostly because I'm lazy). Note that you could differentiate between a failure to load the images and an invalid page. Those are two separate problems and you may want to message them differently, as one is a server error and the other is user error. Page number out of range is often a problem that the user can fix.
I changed the names of some of the variables. I generally use a system where plural names indicate collections and singular names indicate scalars. You don't have to follow the same system, but it's the one I use when writing code.
One of the differences here is that the parse code doesn't have to keep processing after an error. At the first error, it returns failure.
$total_images = 0;
foreach ($all_pictures as $img_total) {
$total_images++;
}
This could be just
$total_images = ($all_pictures === false) ? 0 : count($all_pictures);
or
if ($all_pictures === false) {
$total_images = 0;
$success === false;
} else {
$total_images = count($all_pictures);
}
The glob
function returns an array, so you can just do the normal array operations on it. You only have to check for an error. I'm not quite sure what your original code would have done if there was an error. You might want to check later so you can say something like "Oops, there was an error looking for images!" rather than "No images".
if ($page <= -1) {
This is a weird way to write this. First, what would you do if page were set to -.5 here? Second, what does a page value of 0 mean? You are defaulting to 1, so it would seem that you are 1-indexed. So why accept anything less than 1? Consider
if ($page < 1) {
That eliminates all the invalid values below the range.
For much the same reason,
$page = array_key_exists('page', $_GET) ? $_GET['page'] : 1;
should probably be
$page = array_key_exists('page', $_GET) ? intval($_GET['page']) : 1;
Get parameters can be user input, so you should sanitize them to what you expect.
echo "<div class='container text-center'><p class='text-danger fs-32 mt-100'>No pictures to be displayed.</p></div>";
This is an odd way to write a string in PHP. Using double quotes indicates that there is a value to interpolate in it. But you don't have one here. This could just as well be
echo '<div class="container text-center"><p class="text-danger fs-32 mt-100">No pictures to be displayed.</p></div>';
or
?>
<div class="container text-center">
<p class="text-danger fs-32 mt-100">No pictures to be displayed.</p>
</div>
<?php
Consider moving the whole thing into a function or even a class.
class Page_Handler {
private $image_count;
private $success = true;
private $all_pictures;
private $max_page_number;
private $page;
private $images_per_page = 30;
function __construct($path = 'crossles/*.jpg') {
$this->all_pictures = glob($path);
$this->success = $this->parse();
}
private function parse() {
if ($this->all_pictures === false) {
return false;
}
$this->image_count = count($this->all_pictures);
$this->max_page_number = ceil($this->image_count / $this->images_per_page);
$this->page = array_key_exists('page', $_GET) ? $_GET['page'] : 1;
if ($this->page < 1 || $this->page > $this->max_page_number) {
return false;
}
return true;
}
There would also be getters and possibly other helper methods. I'll leave those as an exercise for the reader (mostly because I'm lazy). Note that you could differentiate between a failure to load the images and an invalid page. Those are two separate problems and you may want to message them differently, as one is a server error and the other is user error. Page number out of range is often a problem that the user can fix.
I changed the names of some of the variables. I generally use a system where plural names indicate collections and singular names indicate scalars. You don't have to follow the same system, but it's the one I use when writing code.
One of the differences here is that the parse code doesn't have to keep processing after an error. At the first error, it returns failure.
answered Nov 13 at 18:57
mdfst13
17.1k42155
17.1k42155
Hey, thanks for the brief explanation of everything, I am going to use a few of your things in my code!
– Mark_Ed
Nov 13 at 19:02
add a comment |
Hey, thanks for the brief explanation of everything, I am going to use a few of your things in my code!
– Mark_Ed
Nov 13 at 19:02
Hey, thanks for the brief explanation of everything, I am going to use a few of your things in my code!
– Mark_Ed
Nov 13 at 19:02
Hey, thanks for the brief explanation of everything, I am going to use a few of your things in my code!
– Mark_Ed
Nov 13 at 19:02
add a comment |
Mark_Ed is a new contributor. Be nice, and check out our Code of Conduct.
Mark_Ed is a new contributor. Be nice, and check out our Code of Conduct.
Mark_Ed is a new contributor. Be nice, and check out our Code of Conduct.
Mark_Ed is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207577%2fsplit-all-images-over-x-amount-of-pages-with-get%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown