Search code examples
phpmysqljoomlamariadbbreezingforms

MySQL - Select Column 'Name' according to user's choice Where rows in Column 'id' and 'parent' equal each other


When parent = 0 means category

When parent = 1 means subcategory 1 connected to category 1 (id=1)

When parent = 2 means subcategory 2 connected to category 2 (id=2)

When parent... etc until 19 categories (id=19 with parent=0)

What I need is to bring the names of subcategories in the sub-category form field according to user's choice in the category field. The category field works fine.

id      parent      name                            active
1       0           Arts & Entertainment            0
2       0           Automotive                      0
3       0           Business & Professional Serv.   1
4       0           Clothing & Accessories          0
5       0           Community & Government          0
6       0           Computers & Electronics         1
7       0           Construction & Contractors      0
8       0           Education                       0
9       0           Food & Dining                   0
10      0           Health & Medicine               0
11      0           Home & Garden                   0
12      0           Industry & Agriculture          0
13      0           Legal & Financial               1
14      0           Media & Communications          0
15      0           Personal Care & Services        0
16      0           Real Estate                     0
17      0           Shopping                        0
18      0           Sports & Recreation             0
19      0           Travel & Transportation         0
34      1           Acting Schools                  1
35      1           Aerial Photographers            1
36      1           Arcades & Amusements            1
37      1           Art Classes                     1
38      1           Art Galleries & Dealers         1
39      1           Art Schools                     1

1.This is the Query for the category field which works fine and gives us the user's choice ($judgePick)

$db->setQuery('SELECT name FROM #__professional_categ WHERE parent=0 AND active=1 ORDER BY name ASC');

2.This is the Query for the subcategory field trying to solve

$judgePick = JRequest::getVar('category');
$db = JFactory::getDBO();

$db->setQuery('SELECT `name` FROM `#__professional_categ` WHERE active = 1 AND (something here...) ORDER BY parent ASC, name ASC);

$result = $db->loadColumn();
 if(!$result){
echo "error";
} else {
    echo json_encode($result);
}

Assumption 1 - id to include ='.$db->quote($judgePick)

Assumption 2 - For parent > 0 has to be equal to id of user's choice in Assumption 1

Expected result

Subcategory field to have the names ONLY according to user's choice in category field ($judgePick) where user's choice id equals parent. In other words, e.g. Arts & Entertainment is the category (parent=0) and has (id =1) and when the user chooses it in category form field, the subcategory form field should show all names with (parent=1)


Solution

  • What you are looking for is probably a self join:

    SELECT x.name 
    FROM #__professional_categ x
    JOIN #__professional_categ y
      ON x.parent = y.id
    WHERE y.name = ‘. $judgePick .‘
      AND x.parent = y.id
      AND x.active = 1
    

    You can check the query on abstract sample here: http://www.sqlfiddle.com/#!9/ecc4bb/1/0

    Since with the input in your code you only get the name of the chosen category, thus we have to select its id too in the table, then we can find and select the subcategory's parent id and based on that, return the subcategories' names.

    In Joomla syntax, your code and query should look like this:

    $jinput = JFactory::getApplication()->input;
    $judgePick = $jinput->get(‘category’);
    
    $db = JFactory::getDbo();
    
    // Create a new query object.
    $query = $db->getQuery(true);
    
    $query
      ->select('x.name')
      ->from($db->quoteName('#__professional_categ', 'x'))
      ->join('LEFT', $db->quoteName('#__professional_categ', 'y') . ' ON ' . $db->quoteName('x.parent') .' = '. $db->quoteName('y.id'))
      ->where($db->quoteName('y.name') .' = '. $db->quote($judgePick))
      ->andWhere(array($db->quoteName('x.parent').' = '. $db->quoteName('y.id'), $db->quoteName('x.active').' = 1'), $glue = 'AND')
      ->order($db->quoteName('x.name') . ' ASC');
    
    // Reset the query using our newly populated query object.
    $db->setQuery($query);
    
    $result = $db->loadColumn();