简体   繁体   中英

Sorting of Alphabets through php

I am a complete noob in php and trying to learn it by working on a premade script and making changes to it. I have been trying to figure out how to display titles by their first letters in a table. I went through this site http://www.emirplicanic.com/php/php-a-to-z-sorting-script but wasn't able to make it work in the script.

      public function getProducts()
         {
               global $db, $core, $pager;

      require_once(BASEPATH . "lib/class_paginate.php");
              $pager = new Paginator();

      $pager = new Paginator();
      $counter = countEntries($this->pTable);
      $pager->items_total = $counter;
      $pager->default_ipp = $core->perpage;
      $pager->paginate();

      if ($counter == 0) {
        $pager->limit = "";
      }

      if (isset($_GET['sort'])) {
          list($sort, $order) = explode("-", $_GET['sort']);
          $sort = sanitize($sort);
          $order = sanitize($order);
          if (in_array($sort, array("title", "cid", "price", "created"))) {
              $ord = ($order == 'DESC') ? " DESC" : " ASC";
              $sorting = " p." . $sort . $ord;
          } else {
              $sorting = " p.created DESC";
          }
      } else {
          $sorting = " p.created DESC";
      }
      ----------added by me-----------------
      if (isset($_GET['letter'])) {
          list($letter, $order1) = explode("-", $_GET['letter']);
          $letter = sanitize($letter);
          $order1 = sanitize($order1);
        //  if (in_array($sort, "A", "B", "C", "D"))) {

            if (!(strcmp($letter, "A"))) {
              $ord1 = ($order1 == 'DESC') ? " DESC" : " ASC";
              $sorting1 = " p." . $letter . $ord1;
          } 
      }
               ------------------------------------------------------------------
      $sql = "SELECT p.*, p.id as pid, c.name, c.id as cid," 
      . "\n DATE_FORMAT(p.created, '" . $core->short_date . "') as cdate,"
      . "\n (SELECT COUNT(pid) FROM transactions WHERE pid = p.id) as sales"
      . "\n FROM " . $this->pTable . " as p"
      . "\n LEFT JOIN categories as c ON c.id = p.cid" 
      . "\n ORDER BY " . $sorting . $pager->limit;
              $row = $db->fetch_all($sql);

              return ($row) ? $row : 0;

           }

and then the html part of it is

             <li><a href="?letter=A"><span>A</span></a></li>
             <li><a href="?letter=B"><span>B</span></a></li>
                              .
                              .
                              .

The php part is giving me an Undefined offset error. Also i am not sure if i have to add anything extra on the html to make it work

The URLS in your HTML should be ?letter=A-DESC . (or ASC) The list($letter, $order1) is expecting two results from the call to explode('-', $_GET['letter']) , and it's only getting one. Thus, an 'undefined offset' in the array returned from explode().

Note that anyone can send anything in the ?letter part of the URL, not just what's in your links. You should "sanitize" (whatever that does for you) any input arguments as the very first step, and handle the situation where the data isn't what you expect before you start processing that data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM