Skip to content

Instantly share code, notes, and snippets.

@tulik
Created May 18, 2016 22:21
Show Gist options
  • Save tulik/4a1e8b87e07455df8e8cfe8b5defc748 to your computer and use it in GitHub Desktop.
Save tulik/4a1e8b87e07455df8e8cfe8b5defc748 to your computer and use it in GitHub Desktop.
<?php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use AppBundle\Entity\Items;
use Symfony\Component\HttpFoundation\Response;
class TagsController extends Controller
{
const CITY = 'miejscowosc';
public function indexAction(Request $request)
{
$request->query->get('woj') == null ? $query = $request->attributes->get('woj') :
$query = $request->query->get('woj');
//wojewodzkie
if(!isset($query)){
$cities = $this->getCities('', '');
return $this->render('@App/tags/tags.html.twig', array(
'places' => $cities
));
}else {
//powiatowe
$cities = $this->getCities('', '');
foreach ($cities as $city)
$district[] = $this->getDistrict($city[self::CITY], 'woj');
$towns = $this->getCities($query, 'pow');
}
return $this->render('@App/tags/tags.html.twig', array(
'places' => $cities
));
}
private function getDistrict($city, $district)
{
$sql = "select $district from items where miejscowosc = '$city' group by $district";
$em = $this->getDoctrine()->getManager();
$stmt = $em->getConnection()->prepare($sql);
$stmt->execute();
return $stmt->fetch()[$district];
}
private function getCities($district, $code){
if($district != '' && $code != '') {
$where = "where $code = '$district'";
}else{
$where = '';
}
$sql = "select miejscowosc from items $where group by miejscowosc order by count(*) desc limit 10";
$em = $this->getDoctrine()->getManager();
$stmt = $em->getConnection()->prepare($sql);
$stmt->execute();
return $city = $stmt->fetchAll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment