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.










share|improve this question









New contributor




Mark_Ed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    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.










    share|improve this question









    New contributor




    Mark_Ed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      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.










      share|improve this question









      New contributor




      Mark_Ed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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






      share|improve this question









      New contributor




      Mark_Ed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Mark_Ed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited Nov 13 at 18:12









      Sᴀᴍ Onᴇᴌᴀ

      7,67561748




      7,67561748






      New contributor




      Mark_Ed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 13 at 17:52









      Mark_Ed

      132




      132




      New contributor




      Mark_Ed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Mark_Ed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Mark_Ed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          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>";
          }





          share|improve this answer























          • 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 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










          • Nvm, fixed it :D
            – Mark_Ed
            Nov 13 at 19:20


















          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.






          share|improve this answer





















          • 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











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Mark_Ed is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          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

























          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>";
          }





          share|improve this answer























          • 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 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










          • Nvm, fixed it :D
            – Mark_Ed
            Nov 13 at 19:20















          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>";
          }





          share|improve this answer























          • 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 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










          • Nvm, fixed it :D
            – Mark_Ed
            Nov 13 at 19:20













          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>";
          }





          share|improve this answer














          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>";
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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 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










          • 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












          • 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












          • 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
















          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












          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.






          share|improve this answer





















          • 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















          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.






          share|improve this answer





















          • 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













          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.






          share|improve this answer













          $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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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


















          • 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










          Mark_Ed is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          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.















           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Morgemoulin

          Scott Moir

          Souastre