query("SELECT id, model_name, make_id FROM car_models")->fetch_all(MYSQLI_ASSOC); // Build WHERE $where = []; if ($model_id) { $where[] = "car_model = $model_id"; } elseif ($make_id) { $model_ids = []; foreach ($models as $model) { if ($model['make_id'] == $make_id) $model_ids[] = $model['id']; } if (empty($model_ids)) $where[] = "0"; else $where[] = "car_model IN (" . implode(',', $model_ids) . ")"; } if ($body) $where[] = "car_type = '" . $conn->real_escape_string($body) . "'"; $where[] = "price BETWEEN $min_price AND $max_price"; $where[] = "manufacture_year BETWEEN $min_year AND $max_year"; // … בניית ה־WHERE … if ($fuel_type) { $where[] = "fuel_type = '" . $conn->real_escape_string($fuel_type) . "'"; } if ($current_tab === 'new') $where[] = "kilometers = 0"; elseif ($current_tab === 'used') $where[] = "kilometers > 0"; $where_sql = $where ? 'WHERE ' . implode(' AND ', $where) : ''; // Sorting $order = 'ORDER BY id DESC'; if ($sort == 'low-to-high') $order = 'ORDER BY price ASC'; elseif ($sort == 'high-to-low') $order = 'ORDER BY price DESC'; elseif ($sort == 'by-latest') $order = 'ORDER BY id DESC'; elseif ($sort == 'hand-low-to-high') $order = 'ORDER BY hand ASC, id DESC'; elseif ($sort == 'hand-high-to-low') $order = 'ORDER BY hand DESC, id DESC'; elseif ($sort == 'km-low-to-high') $order = 'ORDER BY kilometers ASC, id DESC'; elseif ($sort == 'km-high-to-low') $order = 'ORDER BY kilometers DESC, id DESC'; // Pagination $count_sql = "SELECT COUNT(*) as total FROM cars_sale_inventory $where_sql"; $total_result = $conn->query($count_sql)->fetch_assoc(); $total_pages = max(1, ceil($total_result['total'] / $per_page)); // Fetch cars $sql = "SELECT * FROM cars_sale_inventory $where_sql $order LIMIT $per_page OFFSET $offset"; $cars = $conn->query($sql); // Tabs $tabs = ['All car' => '', 'New car' => 'new', 'Used car' => 'used']; ?>