33 - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of 33.php

  1. <?php
  2. class Category
  3. {
  4.     private $db;
  5.     private $primeCategories = [
  6.         "Post",
  7.         "Tour",
  8.         "Accommodation",
  9.         "Car",
  10.         ["Gear", "Car"],
  11.         ["Fuel", "Car"],
  12.         ["CarType", "Car"],
  13.         ["CarBrand", "Car"],
  14.  
  15.     ];
  16.     public function __construct(Database $db)
  17.     {
  18.         $this->db = $db;
  19.         $this->primeCategories();
  20.         //$this->createTable();
  21.     }
  22.     public function createTable()
  23.     {
  24.         if ($this->db->query("CREATE TABLE IF NOT EXISTS `categories` (
  25.             `id` int(11) NOT NULL AUTO_INCREMENT,
  26.             `name` varchar(255) NOT NULL,
  27.             `ParentCategory` int(11) DEFAULT NULL,
  28.             `lang` varchar(10) NOT NULL,
  29.             `IsPrime` tinyint(1) NOT NULL DEFAULT 0,
  30.             PRIMARY KEY (`id`),
  31.             KEY `PrentCategory` (`ParentCategory`)
  32.           ) ENGINE=InnoDB;")) {
  33.             /*  $this->db->query("ALTER TABLE `categories`
  34.         ADD CONSTRAINT `categories_ibfk_1` FOREIGN KEY (`ParentCategory`) REFERENCES `categories` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
  35.         COMMIT;
  36.         ");*/
  37.         };
  38.     }
  39.     public function add($name, $parentId = null, $isPrime = false, $lang = "ar")
  40.     {
  41.         $q = new QueryBuilder($this->db);
  42.         if ($this->alreadyExist($name, $parentId)) {
  43.             return json_encode(["status" => "already  exist"]);
  44.         } else {
  45.  
  46.  
  47.             $q->table("categories")->insert([
  48.                 'Name' => $name,
  49.                 'ParentCategory' => $parentId,
  50.                 'IsPrime' => $isPrime,
  51.                 'lang' => $lang
  52.             ])->execute();
  53.             return  json_encode(["status" => "success"]);
  54.         }
  55.     }
  56.     function primeCategories()
  57.     {
  58.  
  59.         $catArr = $this->primeCategories;
  60.         for ($i = 0; $i < count($catArr); $i++) {
  61.             if (!is_array($catArr[$i])) {
  62.                 # code...
  63.                 $this->add($catArr[$i], null, true, "");
  64.             } elseif (is_array($catArr[$i])) {
  65.                 $parent = $this->getIdByNames($catArr[$i][1]);
  66.                 $this->add($catArr[$i][0], $parent, true, "");
  67.             }
  68.         }
  69.     }
  70.     public function combobox(string $catId, bool $multiple = true, $id = "")
  71.     {
  72.         $cat = !is_numeric($catId) ? $this->getIdByNames($catId) : $catId;
  73.  
  74.         $allCategories = self::getChildrenCategories($cat);
  75.         $i = 0;
  76.         $select = "";
  77.         $multiAttr = $multiple "multiple" : null;
  78.         $idAttr = $id == "" ? $multiple "categorySelect" : 'categoryAddSelect' : $id;
  79.         $select .= "<select id='$idAttr' class='selectpicker'  $multiAttr data-live-search='true' data-cat-info='$cat'>
  80.         ";
  81.  
  82.         foreach ($allCategories as $category) {
  83.             //$select .= ' <option value="' . $category['id'] . ">" . $category['name'] . '</option>';
  84.             //if ($lang == $category['lang'])
  85.  
  86.             $select .= "<option value='$category[id]' data-lvl='$category[IsPrime]'>  $category[name]</option>";
  87.         }
  88.         $select .= "
  89.         </select>";
  90.  
  91.         return  $select;
  92.     }
  93.     public function comboboxAll()
  94.     {
  95.         $q = new  QueryBuilder($this->db);
  96.         $cats = $q->select("*")->table('categories')->get();
  97.         $options = '';
  98.         foreach ($cats as &$item) {
  99.             $options .= "<option value=\"{$item['id']}\">{$item['name']}</option>\n";
  100.         }
  101.         return "<select class='selectpicker' multiple>\n" . $options . "\n</select>";
  102.     }
  103.     public function categoryCheckList(int $catId)
  104.     {
  105.         $allCategories = self::getChildrenCategories($catId);
  106.         $list = "<ul id='catList' style='list-style:none'>";
  107.         $i = 0;
  108.         foreach ($allCategories as $category) {
  109.             $list .= "
  110.             <li class='catCheck d-flex w-100 justify-content-between '><label for='catCheck$i' >$category[name]</label><input type='checkbox' class='check-cat' value='$category[id]' id='catCheck$i'></li>";
  111.             $i++;
  112.         }
  113.         $list .= "
  114.         </ul>";
  115.         return $list;
  116.     }
  117.     public function form($catId, $id = "")
  118.     {
  119.         $cmb = $this->combobox($catId, false, $id);
  120.         $cat = !is_numeric($catId) ? $this->getIdByNames($catId) : $catId;
  121.         $div = <<<EOT
  122.         <label data-lang="category" data-dir-dynamic> category</label>
  123.         <button type="button" data-bs-toggle="modal" class="add-btn-cat" data-bs-target="#{$id}Modal">
  124.             <i class="far fa-plus"></i>
  125.         </button>
  126.         <div class="form-group">
  127.         <div class="modal fade" id="{$id}Modal" tabindex="-1" aria-labelledby="{$id}ModalLabel" aria-hidden="true">
  128.             <div class="modal-dialog">
  129.                 <div class="modal-content">
  130.                     <div class="modal-header">
  131.                         <h5 class="modal-title" id="{$id}ModalLabel">Add item</h5>
  132.                         <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
  133.                     </div>
  134.                     <div class="modal-body">
  135.                         <div class="form">
  136.     
  137.                             <h3>add</h3>
  138.                             <div class="col-lg-6 col-md-6 col-sm-12">
  139.                                 <label for="{$id}">parent {$id} </label>
  140.                                <span id="{$id}Second"> $cmb</span>
  141.                             </div>
  142.                             <div class="col-lg-6 col-md-6 col-sm-12">
  143.                                 <label for="{$id}Name"> name </label>
  144.                                 <input type="text" id="{$id}Name" name="{$id}Name" />
  145.                             </div>
  146.                         </div>
  147.                     </div>
  148.                     <div class="modal-footer">
  149.                         <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
  150.                         <button type="button" class="btn btn-primary addCat" data-target-cat="$id"data-cat-info="$cat" id="addCat">Save</button>
  151.                     </div>
  152.                 </div>
  153.             </div>
  154.         </div>
  155.     </div>
  156. EOT;
  157.         return $div;
  158.     }
  159.     private $s = 0;
  160.     function getChildrenCategories($catId = null,  &$result = array(), $lvl = 0)
  161.     {
  162.         error_reporting(0);
  163.         $q = new  QueryBuilder($this->db);
  164.         $children = $q->table('categories')->select('*')->where('ParentCategory', '<=>', $catId)->get();
  165.  
  166.         ($parent = $this->getById($catId));
  167.         if ($parent) {
  168.             $result[] =  $parent;
  169.         }
  170.         foreach ($children as  $child) {
  171.             $child['name'] = str_repeat('-', $lvl) . $child['name'];
  172.  
  173.             //$result[] =  $child;
  174.             // echo $child['name'] . "\n";
  175.             // $i = array_search($child, $result);
  176.             //echo $i;
  177.             array_push($result, $lvl);
  178.             $this->getChildrenCategories($child['id'], $result, $lvl + 1);
  179.         }
  180.  
  181.  
  182.  
  183.         return    $result;
  184.     }
  185.     function getById($catId)
  186.     {
  187.         $q = new  QueryBuilder($this->db);
  188.         return $q->table('categories')->select('*')->where('id', '=', $catId)->get()[0];
  189.     }
  190.     public function alreadyExist(string $name, $parentId = null)
  191.     {
  192.         $q = new QueryBuilder($this->db);
  193.  
  194.         $res = $q->table('categories')->select("*")->where('name', '=', $name,  $q::AND)->where("ParentCategory", "<=>", $parentId)->get();
  195.         return count($res) > 0; //@todo:implement this method
  196.     }
  197.     public static  function GetPostCategories($id, $table, Database $db)
  198.     {
  199.         $q = new QueryBuilder($db);
  200.         $s = $q->table("postcategories")->select("*")->where('tragetTable', "=", $table)->where('targetPost', "=", $id)->get();
  201.         $cat = [];
  202.  
  203.  
  204.         for ($i = 0; $i < count($s); $i++) {
  205.             $cat[$i] = (string)$s[$i]['catId'];
  206.         }
  207.         return json_encode($cat);
  208.     }
  209.     public function getPrimeCategoryByName($name)
  210.     {
  211.         $q = new QueryBuilder($this->db);
  212.         $res = $q->table("categories")->select("*")->where("name", '=', $name)->limit(1)->get()[0];
  213.         return  isset($res["id"]) ? $res['id'] : false;
  214.     }
  215.     public function getIdByNames($name)
  216.     {
  217.         return self::getIdByName($name, $this->db);
  218.     }
  219.     public static function getIdByName($name, Database $db)
  220.     {
  221.         $q = new QueryBuilder($db);
  222.  
  223.  
  224.         $res = $q->table("categories")->select("*")->where("name", '=', $name)->limit(1)->get();
  225.         return  isset($res[0]["id"]) ? $res[0]["id"] : "false";
  226.     }
  227. }
  228.  
File Description
  • 33
  • PHP Code
  • 13 Apr-2024
  • 8.46 Kb
You can Share it: