Advertisement
siristru

Joomla sql PIVOT method

May 26th, 2021
1,150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.48 KB | None | 0 0
  1. <?php
  2. $query = $noclegi_db->getQuery(true);
  3. $query
  4.   ->select(array('sid'))
  5.   ->from($noclegi_db->quoteName('#__sobipro_field_data'))
  6.   ->where($noclegi_db->quoteName('baseData') . " = " . $noclegi_db->quote('Tylicz'), 'AND')
  7.   ->where($noclegi_db->quoteName('lang') . " = " . $noclegi_db->quote('pl-PL'));
  8. $noclegi_db->setQuery($query);
  9. $obj_results = $noclegi_db->loadAssocList('sid');
  10.  
  11. $obj_ids = implode(",", array_keys($obj_results));
  12.  
  13. //print_r($obj_ids);
  14.  
  15. $query = $noclegi_db->getQuery(true);
  16. $query
  17.   //->select(array('sid','fid','baseData'))
  18.   ->select([
  19.             "MAX(CASE WHEN fid = 1 THEN baseData ELSE NULL END) AS name",
  20.             "MAX(CASE WHEN fid = 3 THEN baseData ELSE NULL END) AS city",
  21.             "MAX(CASE WHEN fid = 14 THEN baseData ELSE NULL END) AS street",
  22.             "MAX(CASE WHEN fid = 8 THEN baseData ELSE NULL END) AS short_description",
  23.             "MAX(CASE WHEN fid = 10 THEN baseData ELSE NULL END) AS image"
  24.           ])
  25.   ->from($noclegi_db->quoteName('#__sobipro_field_data'))
  26.   ->where(["lang = 'pl-PL'","sid IN (". $obj_ids .")", "fid IN (1,3,14,8,10)"])
  27.   ->group("sid")
  28.   ->order($noclegi_db->quoteName('sid') . ' ASC');
  29. //echo $query->dump();
  30. $noclegi_db->setQuery($query);
  31. $final_results = $noclegi_db->loadObjectList();
  32.  
  33. /*fid
  34. 1 nazwa
  35. 3 miejscowość
  36. 14 ulica
  37. 8 short description
  38. 10 obrazek*/
  39.  
  40. //$my_array = array_combine(array_map('array_shift', $DBresults), array_map('array_pop', $DBresults));
  41.  
  42. print_r($final_results);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement