I am not sure if I formatted the title well but this is continued from this question
How to do Mysql Union with check if row exists?
we got the UNION working well and combined 7 tables, issue now is that we want to give user the option to disable search from particular table. Sure you guessed it , sql returning error since if user wants data from last table we placed in UNION our sql starts with UNION instead SELECT ,
so we created multiple select list that returns array of available options
print_r($tables) returns Array ( [0] => table_1 [1] => table_2…
foreach ($tables as $key=> $source){
if($key ==0){
$UNION='';
}else{
$UNION='UNION ';
}
}
this is perfect, only first one from array is not getting the UNION but in sql we have a problem since we cant loop same query and we cant get the $UNION var outside of the loop more than once(global var for example).
if (in_array("table_1", $tables)) {
$sql1 ="".$UNION."SELECT i.title,i.category,'a' as source FROM table_a WHERE REGEXP 'news'";
}
if (in_array("table_2", $tables)) {
$sql2 ="".$UNION."SELECT i.title,i.category, 'b' as source FROM table_b WHERE REGEXP 'news'";
}
$query=$sql1.$sql2;
we tried to get the var $UNION outside the foreach loop by placing it in array and imploding but that does not work either, for example
foreach ($tables as $key=> $source){
if($key ==0){
$UNION='';
}else{
$UNION='UNION ';
}
$get_union[]=$UNION;
}
if (in_array("table_1", $tables)) {
$sql1 ="".implode($UNION)."SELECT i.title,i.category,'a' as source FROM table_a WHERE REGEXP 'news'";
}
if (in_array("table_2", $tables)) {
$sql2 ="".implode($UNION)."SELECT i.title,i.category, 'b' as source FROM table_b WHERE REGEXP 'news'";
}
i was just asked to edit the question so here is result I would need
user selects option from multiple select list to disable table_b and table_c
our multi select option outputs and array. I need to make sure that first in array does ot have UNION as prefix and than run the query. So something like this:
$table =array([0]=>table_a,[1]=>table_b,[2]=>table_c);
if($table[0]){
$UNION='';
}else{
$UNION='UNION ';
}
if (in_array("table_a", $tables)) {
$sql1 ="".$UNION."SELECT i.title,i.category,'a' as source FROM table_a WHERE REGEXP 'news'";
}
if (in_array("table_b", $tables)) {
$sql2 ="".$UNION."SELECT i.title,i.category, 'b' as source FROM table_b WHERE REGEXP 'news'";
}
if (in_array("table_c", $tables)) {
$sql3 ="".$UNION."SELECT i.title,i.category, 'c' as source FROM table_b WHERE REGEXP 'news'";
}
$query=$sql1.$sql2.$sql2;
Any brave soul that can help out please do. Thank you!
So you want to achieve a similar result to this:
given
$tables = array('table_a', 'table_b',...);The problem is that you don’t have a way to match ‘table_a’ to ‘a’ (unless of course these were your real table names, which I seriously doubt that)
So, what you need to do is to create a hastable to match each table to the fixed values you want to fix to them:
Once you have that the queries can be created this way:
Now let’s just implode the array:
I don’t know much of PHP but this should give you an idea of how to solve it.