• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

[SOLVED] calculation...

Status
Not open for further replies.

bea

Active Member
Hi,
it seems, that I'm out of Fabrik....

I need to calculate the order of records with same date and same shift, because I need it to get the right data from another database. There will be always 3 records within a shift and they should numbered like 1,2,3.
I've no idea how to write the calc ...
Cheers,
Bianka

Elements:
$shift = $data['fab_cp1_line_tour___shift_raw'];
$date = $data['fab_cp1_line_tour___date_raw'];
$id = $data['fab_cp1_line_tour___id_raw'];
 
Hi,
I've solved myself with 2 different scripts. Don't know, which one is better...
update: I had to add the date element, but in second script it doesn't work (error)
Cheers


PHP:
$db = JFactory::getDbo();
$date ='{fab_cp1_line_tour___date_raw}';
$shift = (int)'{fab_cp1_line_tour___shift_raw}';
$id = '{fab_cp1_line_tour___id_raw}';
$query = $db->getQuery(true);
$query
  ->select('MAX(id)')
  ->from('fab_cp1_line_tour')
  ->where('date= ' . $db->quote($date))
  ->where('shift = ' . $db->quote($shift));
$db->setQuery($query);
$maxid  = $db->loadResult();
$query = $db->getQuery(true);
$query
  ->select('MIN(id)+1')
  ->from('fab_cp1_line_tour')
  ->where('date= ' . $db->quote($date))
  ->where('shift = ' . $db->quote($shift));
$db->setQuery($query);
$midid  = $db->loadResult();
$query = $db->getQuery(true);
$query
     ->select('MIN(id)')
     ->from('fab_cp1_line_tour')
  ->where('date= ' . $db->quote($date))
  ->where('shift = ' . $db->quote($shift));
$db->setQuery($query);
$minid  = $db->loadResult();
if ($minid == $id) {
return '1';}
if ($midid == $id) {
return '2';}
if ($maxid == $id) {
return '3';}
else {return '';}
PHP:
$db = JFactory::getDbo();
$date ='{fab_cp1_line_tour___date_raw}';
$shift = '{fab_cp1_line_tour___shift_raw}';
$id = '{fab_cp1_line_tour___id_raw}';
$db = & JFactory::getDBO();
$query = "SELECT MAX(id) FROM `fab_cp1_line_tour` WHERE `shift` = $shift AND `date` = $date";
$db->setQuery($query);
$maxid =  $db->loadResult();
$query = "SELECT MIN(id)+1 FROM `fab_cp1_line_tour` WHERE `shift` = $shift AND `date` = $date";
$db->setQuery($query);
$midid =  $db->loadResult();
$query = "SELECT MIN(id) FROM `fab_cp1_line_tour` WHERE `shift` = $shift AND `date` = $date";
$db->setQuery($query);
$minid =  $db->loadResult();
if ($minid == $id) {
return '1';}
if ($midid == $id) {
return '2';}
if ($maxid == $id) {
return '3';}
else {return '';}
 
Last edited:
IMHO, it's always better to use the query builder rather than construct queries by hand, unless you need to do something the query builder can't handle.

And as you are querying the same table with the same conditions, you could do it in a single query instead of three:

Code:
$db = JFactory::getDbo();
$date = '{fab_cp1_line_tour___date_raw}';
$shift = (int)'{fab_cp1_line_tour___shift_raw}';
$id = '{fab_cp1_line_tour___id_raw}';

$query = $db->getQuery(true);
$query
   ->select('MAX(id) AS maxid')
   ->select('MIN(id)+1 AS midid')
   ->select('MIN(id) AS minid')
   ->from('fab_cp1_line_tour')
   ->where('date= ' . $db->quote($date))
   ->where('shift = ' . $db->quote($shift));
$db->setQuery($query);
$results = $db->loadObject();

if ($results->minid == $id) {
   return'1';
}
else if ($results->midid == $id) {
   return'2';
}
else if ($results->maxid == $id){
   return'3';
}
else {
   return'';
}

-- hugh
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top