Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $text = $_GET['list'];
- $minPrice = $_GET['minPrice'];
- $maxPrice = $_GET['maxPrice'];
- $filter = $_GET['filter'];
- $order = $_GET['order'];
- $property = "price";
- $arrayOfProducts = array();
- $products = preg_split("/\r?\n/", $text, -1, PREG_SPLIT_NO_EMPTY);
- $counter = 1;
- array_walk($products, function(&$val) {
- $val .= "|" . $GLOBALS['counter'];
- $GLOBALS['counter']++;
- });
- $products = array_filter($products, function($val) {
- $info = explode('|', $val);
- if(trim($info[1]) != $GLOBALS['filter'] && $GLOBALS['filter'] != 'all') {
- return false;
- }
- if(trim($info[3]) < $GLOBALS['minPrice'] || trim($info[3]) > $GLOBALS['maxPrice']) {
- return false;
- }
- return true;
- });
- $products = array_combine(range(0, count($products) - 1), $products);
- for($i = 0; $i < count($products) ;$i++) {
- $currLine = explode('|', $products[$i]);
- $currLine = array_filter($currLine, function($val) {
- if($val == "") {
- return false;
- }
- return true;
- });
- $newEntry = new stdClass();
- $newEntry->name = trim($currLine[0]);
- $newEntry->type = trim($currLine[1]);
- $newEntry->comps = trim($currLine[2]);
- $newEntry->price = trim(number_format(floatval($currLine[3]), 2, '.', ''));
- $newEntry->id = trim($currLine[4]);
- $arrayOfProducts[] = $newEntry;
- }
- usort($arrayOfProducts, function($s1, $s2) use ($order, $property) {
- if ($s1->$property == $s2->$property) {
- return strcmp($s1->id, $s2->id);
- }
- return ($order == "ascending" ^ $s1->$property < $s2->$property) ? 1 : -1;
- });
- for($i = 0; $i < count($arrayOfProducts) ;$i++) {
- $components = explode(", ", $arrayOfProducts[$i]->comps);
- echo '<div class="product" id="product' . $arrayOfProducts[$i]->id . '">';
- echo '<h2>' . htmlspecialchars(trim($arrayOfProducts[$i]->name)) . '</h2>';
- echo '<ul>';
- foreach ($components as $comp) {
- echo '<li class="component">' . htmlspecialchars(trim($comp)) . '</li>';
- }
- echo '</ul>';
- echo '<span class="price">' . htmlspecialchars(trim($arrayOfProducts[$i]->price)) . '</span>';
- echo '</div>';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement