php - How convert few sql select to multiple select queries

236

I have problem with last (3rd) dynamic dropdown list - wrong result.

<?php
include('dbconfig.php');
$action = $_REQUEST['action'];
if ($action=='showAll')   
{$id = "0";}
else $id=$_POST['id'];

switch($action)
{
case 'showAll';
    $stmt=$DB_con->prepare('SELECT * FROM slist GROUP BY site');
    $stmt->execute();
break;
case 'site';
    $stmt=$DB_con->prepare('SELECT menu FROM slist WHERE site=:id GROUP BY menu ORDER BY menu');
    $stmt->execute(array(':id'=>$id));
break;
case 'menu';
    $stmt=$DB_con->prepare('SELECT categ FROM slist WHERE menu=:id GROUP BY categ ORDER BY categ');
    $stmt->execute(array(':id'=>$id));
break;
case 'categ';
    $stmt=$DB_con->prepare('SELECT links FROM slist WHERE categ=:id GROUP BY categ ORDER BY links');
    $stmt->execute(array(':id'=>$id));
break;
default: 
    $stmt=$DB_con->prepare('SELECT * FROM slist GROUP BY site');
    $stmt->execute();
break;
}
?>

TheSELECT links FROM slist WHERE categ=:id GROUP BY categ ORDER BY links for$action=="categ" returns the wrong end value (gets the result from the first available in the list), because the query should look like this:SELECT links FROM slist WHERE menu=:id AND categ=:id GROUP BY categ ORDER BY links.

How do I pass the value of the "menu" variable to this query?

SQL table column names and several records from table:

`id`, `site`, `menu`, `categ`, `links`
28, 'CoursesWeb.net', 'PHP-MySQL', 'Tutorials', 'http://coursesweb.net/php-mysql/tutorials_t'
34, 'CoursesWeb.net', 'JavaScript', 'Tutorials', 'http://coursesweb.net/javascript/tutorials_t'
46, 'CoursesWeb.net', 'Flash-AS3', 'Tutorials', 'http://coursesweb.net/flash/tutorials_t'

People are also looking for solutions to the problem: php - Order array of strings based on a portion of each string

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.