• 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 with data

Status
Not open for further replies.

bea

Active Member
Hi,
I've tried to optimize a calculation with data from different databases, but without success.
The calculation works correct, but I want to get scrap2 and ist_bags with one query to calculate $total at the end. With array and loadObjectList doesn't work.
Would be great to get a hint for this.

Cheers,
Bianka

PHP:
$date ='{fab_cp4_line_tour___date_raw}';
$myDb = FabrikWorker::getDbo();
$myQuery = $myDb->getQuery(true);
$myQuery
    ->select('SUM(losses3_scrap1)')
    ->from('fab_cp1_line_tour')
    ->where('date= ' . $myDb->quote($date))
    ->where('machine= 7')
    ->where('status= 2')
    ->where('shift_records = shiftorder');
$myDb->setQuery($myQuery);
$scrap2 = $myDb->loadResult();

$myDb = FabrikWorker::getDbo(false, 2);
$myQuery = $myDb->getQuery(true);
$myQuery
    ->select('h24_scrap2')
    ->from('24h_daten')
    ->where('date= ' . $myDb->quote($date))
    ->where('machine= 7');
$myDb->setQuery($myQuery);
$scrap2 = $myDb->loadResult();
$myDb = FabrikWorker::getDbo(false, 2);
$myQuery = $myDb->getQuery(true);
$myQuery
    ->select('h24_ist_bags')
    ->from('24h_daten')
    ->where('date= ' . $myDb->quote($date))
    ->where('machine= 7');
$myDb->setQuery($myQuery);
$ist_bags = $myDb->loadResult();
$total = ((($scrap1+$scrap2)*100) /$ist_bags);

if ($total == NULL) {
return '';}
else return sprintf('%01.1f',$total).'%';

Test with loadObjectList:
PHP:
$myDb = FabrikWorker::getDbo(false, 2);
$myQuery = $myDb->getQuery(true);
$myQuery
    ->select(array('h24_scrap2','h24_ist_bags'))
    ->from('24h_daten')
    ->where('date= ' . $myDb->quote($date))
    ->where('machine= 7');
$myDb->setQuery($myQuery);
$rows = $myDb->loadObjectList();
var_dump($rows);exit;
Result:
array(1) { [0]=> object(stdClass)#4648 (2) { ["h24_scrap2"]=> string(4) "1000" ["h24_ist_bags"]=> string(5) "20000" } }
 
Last edited:
instead of var_dump

foreach ($rows as $row) {
$scrap2 = $row->h24_scrap2;
..


Gesendet von meinem SM-G930F mit Tapatalk
 
The loadObjectList() returns an array of objects, one array entry per row selected. If you are only expecting one row in the result, just do loadObject() instead ...

$row = $myDb->loadObject();

... then you'll have $row->ht4_scrap2 and $row->h24_ist_bags.

-- hugh
 
Hi troester, hi Hugh,
many thanks for your support. Finally loadObject works.
Cheers,
Bianka
PHP:
$date ='{fab_cp4_line_tour___date_raw}';
$myDb = FabrikWorker::getDbo();
$myQuery = $myDb->getQuery(true);
$myQuery
    ->select('SUM(losses3_scrap1)')
    ->from('fab_cp1_line_tour')
    ->where('date= ' . $myDb->quote($date))
    ->where('machine= 7')
    ->where('status= 2')
    ->where('shift_records = shiftorder');
$myDb->setQuery($myQuery);
$scrap2 = $myDb->loadResult();

$myDb = FabrikWorker::getDbo(false, 2);
$myQuery = $myDb->getQuery(true);
$myQuery
    ->select(array('h24_scrap2','h24_ist_bags'))
    ->from('24h_daten')
    ->where('date= ' . $myDb->quote($date))
    ->where('machine= 7');
$myDb->setQuery($myQuery);
$row = $myDb->loadObject();
$scrap1 = $row->h24_scrap2;
$ist_bags = $row->h24_ist_bags;
$total = (($scrap1+$scrap2)*100) /$ist_bags;
if ($total == NULL) {
return '';}
else return sprintf('%01.1f',$total).'%';
 
You should probably put the sanity check around the loadObject result, otherwise if that query doesn't return anything, it'll blow up with a fatal 'cannot access property of null' or some such when you try and user $row->whatever. Also make sure $scrap2 isn't empty.

Code:
$row=$myDb->loadObject();
if (!empty($row) && !empty($scrap2)) {
   $total = (($row->h24_scrap2 + $scrap2) * 100) / $row->h24_ist_bags;
   return sprintf('%01.1f',$total).'%';
}
return '';

Also, assigning an object to a variable before you use it, like ...

$scrap1 = $row->h24_scrap2;

... is kinda pointless, if you aren't then modifying the copy you just made. Just use it in-place.

-- hugh
 
  • Like
Reactions: bea
Hi Hugh,
many thanks. I noticed also the scrap name mistake in my script.
Now it works and looks much better.
Cheers,
Bianka

Update:
I had to change
Code:
if (!empty($row) && !empty($scrap1))
to
if (($row != NULL) && ($scrap1 != NULL))
because '0' can be a possible.

PHP:
$date ='{fab_cp4_line_tour___date_raw}';
$myDb = FabrikWorker::getDbo();
$myQuery = $myDb->getQuery(true);
$myQuery
    ->select('SUM(losses3_scrap1)')
    ->from('fab_cp1_line_tour')
    ->where('date= ' . $myDb->quote($date))
    ->where('machine= 7')
    ->where('status= 2')
    ->where('shift_records = shiftorder');
$myDb->setQuery($myQuery);
$scrap1 = $myDb->loadResult();

$myDb = FabrikWorker::getDbo(false, 2);
$myQuery = $myDb->getQuery(true);
$myQuery
    ->select(array('h24_scrap2','h24_ist_bags'))
    ->from('24h_daten')
    ->where('date= ' . $myDb->quote($date))
    ->where('machine= 7');
$myDb->setQuery($myQuery);
$row = $myDb->loadObject();
if (($row != NULL) && ($scrap1 != NULL)){
   $total = (($row->h24_scrap2 + $scrap1 ) * 100) / $row->h24_ist_bags;
   return sprintf('%01.1f',$total).'%';
}
return '';
 
Last edited:
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top