Calc Element query question

nbradshaw

Active Member
Hello

I have a multi-selection dropdown {peg_alert___ospf_area} where the user selects a value or multiple values.

I use the following code to grab the value - this works great. However, I am wanting to concat the output if the users selects more than one value.

DB Structure:
id, peg_site_id, ospg_area

ospf_area values may be 'OSPF Area 01, OSPF Area 02, etc.

Can someone please provide some ideas on how to do this? Do I need to do a %Like% or CONTAINS statement?



PHP:
$id = '{peg_alert___ospf_area}';
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
    ->select(array('peg_site_id'))
    ->from('site_ospf')
    ->where('ospf_area = ' . $db->quote($id));
$db->setQuery($query);
$rows = $db->loadObjectList();

$list = array();
foreach ($rows as $row)
{
    $list[] = "<li>" . $row->peg_site_id .  "</li>";
}
return "<ol>" . implode($list) . "</ol>";
 
Thanks troester.

That is what I was thinking, as well - but I just don't know how to construct this piece for the IN ('OSPF Area 01', 'OSPF Area 02').

->where('ospf_area = ' . $db->quote($id));
 
You have to construct the string
('OSPF Area 01', 'OSPF Area 02')
from your dbjoin selections
$myids = implode(',', $foo); //whatever... (I don't know by heart what you will get from a multiselect, try the WIKI examples)

Then e.g.
->where('ospf_area IN (' . $db->quote($myids) . ')');
 
Is there a clever way to view the sql query within fabrik for the ajax calc query? fabrikdebug won't work because the page has already been loaded.
 
I have this...but I just can't figure out how to get it working...any thoughts on how to get it to the final mile would be greatly appreciated
PHP:
$id = '{peg_alert___ospf_area}';
$myids = implode(',', $id);
$first_names = implode(',', "{peg_alert___ospf_area}");
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
    ->select(array('peg_site_id'))
    ->from('site_ospf')
    ->where('ospf_area IN (' . $db->quote($first_names) . ')');
$db->setQuery($query);
$rows = $db->loadObjectList();
//var_dump($id);exit;

$list = array();
foreach ($rows as $row)
{
    $list[] = $row->peg_site_id;
}
return implode(", ",$list);
!
 
That's a JSON string, so it can be converted into an array with ...

PHP:
$ids = json_decode($id);

... then in your query, implode(',', $ids);

-- hugh
 
Thanks cheesegrits!
Here is what I have...I wasn't too sure what you meant by the comment "... then in your query, implode(',', $ids);". I played around with that - but I am obviously still missing something here.

PHP:
$id = '{peg_alert___ospf_area}';
$ids = json_decode($id);
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
    ->select(array('peg_site_id'))
    ->from('site_ospf')
    ->where('ospf_area IN (' . implode(',', $ids) . ')');
$db->setQuery($query);
$rows = $db->loadObjectList();
//var_dump($id);exit;

$list = array();
foreach ($rows as $row)
{
    $list[] = $row->peg_site_id;
}
return implode(", ",$list);
 
Just trying to keep this one on the radar - appreciate all the help so far...I think we are pretty close on this...I can't find any other posts on this same subject. Any help would be greatly appreciated!
 
Ok - here is what I get:

for echo query, $id, $ids
SELECT peg_site_id FROM site_ospf WHERE ospf_area IN ('')string(51) "["OSPF Area 02","OSPF Area 01"]" NULL

here is the error when I use the:
$foo = FabrikWorker::JSONtoData($data['peg_alert___ospf_area'], true);

Unknown column 'Array' in 'where clause' SQL=SELECT peg_site_id FROM site_ospf WHERE ospf_area IN (Array)

PHP:
$id = '{peg_alert___ospf_area}';
$ids = json_decode($id);
$foo = FabrikWorker::JSONtoData($data['peg_alert___ospf_area'], true);
//$myids = implode(',', $id);
//$first_names = implode(',', "{peg_alert___ospf_area}");
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
    ->select(array('peg_site_id'))
    ->from('site_ospf')
->where('ospf_area IN (' . $db->quote($foo) . ')');
    //->values(implode(',', $db->quote($values)));
$db->setQuery($query);
$rows = $db->loadObjectList();
echo $query;
var_dump($id, $ids,$foo);exit;

$list = array();
foreach ($rows as $row)
{
    $list[] = $row->peg_site_id;
}
return implode(", ",$list);
 
wait...I didn't have my IN statement in that one...

This is what I get with foo now using IN:
I think it is missing the single quotes in the IN statement.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Area 02,OSPF Area 01)' at line 3 SQL=SELECT peg_site_id FROM site_ospf WHERE ospf_area IN (OSPF Area 02,OSPF Area 01)
PHP:
$id = '{peg_alert___ospf_area}';
$ids = json_decode($id);
$foo = FabrikWorker::JSONtoData($data['peg_alert___ospf_area'], true);
//$myids = implode(',', $id);
//$first_names = implode(',', "{peg_alert___ospf_area}");
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
    ->select(array('peg_site_id'))
    ->from('site_ospf')
->where('ospf_area IN (' . implode(',', $foo) . ')');
    //->values(implode(',', $db->quote($values)));
$db->setQuery($query);
$rows = $db->loadObjectList();
echo $query;
var_dump($id, $ids,$foo);exit;

$list = array();
foreach ($rows as $row)
{
    $list[] = $row->peg_site_id;
}
return implode(", ",$list);
 
Okay - got it done!
Thanks troester, cheesegrits!!!

FINAL:
PHP:
$id = '{peg_alert___ospf_area}';
$ids = json_decode($id);
$foo = FabrikWorker::JSONtoData($data['peg_alert___ospf_area'], true);
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
    ->select(array('peg_site_id'))
    ->from('site_ospf')
->where('ospf_area IN ("' . implode('", "', $foo) . '")');

$db->setQuery($query);
$rows = $db->loadObjectList();


$list = array();
foreach ($rows as $row)
{
    $list[] = $row->peg_site_id;
}
return implode(", ",$list);
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top