Created
August 23, 2017 18:30
-
-
Save midorikocak/5f03a476d0a3ba06b3d87d87087aa1a2 to your computer and use it in GitHub Desktop.
Mind Breaker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
private function countRatings(string $placeId, $categories): array | |
{ | |
$params = [':place_id' => $placeId]; | |
$in_params = []; | |
$in = ""; | |
foreach ($categories as $i => $item) { | |
$key = ":category" . $i; | |
$in .= "$key,"; | |
$in_params[$key] = $item; // collecting values into key-value array | |
} | |
$in = rtrim($in, ","); // :id0,:id1,:id2 | |
var_dump($in); | |
$sql = " | |
SELECT | |
category, | |
SUM(CASE WHEN rating > 0 THEN rating ELSE 0 END) as upAmount, | |
SUM(CASE WHEN rating < 0 THEN rating ELSE 0 END) * -1 as downAmount | |
FROM ratings WHERE place_id = :place_id AND category IN ($in) GROUP BY category | |
"; | |
$stm = $this->db->prepare($sql); | |
$allParams = array_merge($params, $in_params); | |
/* | |
$check = 1; | |
foreach ($allParams as $key => $param) { | |
//$stm->bindParam($key, $param); | |
if($key==':place_id') { | |
$check &= ($placeId==$param); | |
} | |
elseif ($key==':category0'){ | |
$check &= ($categories[0]==$param); | |
} | |
elseif ($key==':category1'){ | |
$check &= ($categories[1]==$param); | |
} | |
} | |
var_dump($check); | |
$stm->bindParam(':place_id', $placeId); | |
$stm->bindParam(':category0', $categories[0]); | |
$stm->bindParam(':category1', $categories[1]); | |
*/ | |
$stm->execute($allParams); | |
$result = $stm->fetch(\PDO::FETCH_ASSOC); | |
var_dump($result); | |
die; | |
if ($result == false) { | |
foreach ($categories as $category) { | |
array_push($result, ['category' => $category, 'upAmount' => 0, 'downAmount' => 0]); | |
} | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment