Using MySql Views in PHP reports

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
rpierce
Veteran Member
Posts: 258
Joined: 2018-11-26 13:55
Location: Washington State

Using MySql Views in PHP reports

Post by rpierce » 2022-04-08 17:27

Is there a trick to using a MySql VIEW in a php report. I've experimented but had no luck. I would think you could use a view just like a table in php, but that doesn't seem to be the case. It seems that things would be a lot simpler if I could do the query in PhpMyAdmin, save it as a view and call for the fields from that view.

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1638
Joined: 2018-04-01 10:12

Re: Using MySql Views in PHP reports

Post by pbottcher » 2022-04-08 20:00

Hi,
of course you can do that. If you just want to use the view in a php, you can do that. If you want to use the generated TV you can create a table in AppGini with all the fields needed and then generate your App. Next, delete the table with phpmyadmin or equivalent and create your view.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

Alisson
Veteran Member
Posts: 81
Joined: 2017-02-25 20:32

Re: Using MySql Views in PHP reports

Post by Alisson » 2022-04-08 20:29

This i what I use for reports: Create a file in the hooks folder named "reports.php"
Add the code below and change the query to your desired database table:

Code: Select all

<link href="https://unpkg.com/[email protected]/dist/css/tabulator.min.css" rel="stylesheet">
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/js/tabulator.min.js"></script>
<?php
    define('PREPEND_PATH', '../');
    $hooks_dir = dirname(__FILE__);
    include("$hooks_dir/../lib.php");
    include("$hooks_dir/../header.php");
     
    /* grant access to all logged users */
    $mi = getMemberInfo();
    
    if(!$mi['username'] || $mi['username'] == 'guest'){
        echo "Access denied";
        exit;
    }

    $query = sql("SELECT * FROM `yourTableNameHere`", $eo); //Change this line to use your desired table
    
    $json_array = array();  
        while($row = mysqli_fetch_assoc($query))  
        {  
            $json_array[] = $row;
        }  
    $json = json_encode($json_array, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
?>

<div id="table"></div>
<script>
  var tabledata = <?php echo $json;?>;

  var table = new Tabulator("#table", {
      data:tabledata,           //load row data from array
      layout:"fitColumns",      //fit columns to width of table
      responsiveLayout:"hide",  //hide columns that dont fit on the table
      pagination:"local",       //paginate the data
      paginationSize:7,         //allow 7 rows per page of data
      movableColumns:true,      //allow column order to be changed
      resizableRows:true,       //allow row order to be changed
      // groupBy:"dept",       //allow row order to be changed
      autoColumns:true, //create columns from data field names
  });

</script>
After that you can customize it the way you want.

More information here: http://tabulator.info/examples/5.1

rpierce
Veteran Member
Posts: 258
Joined: 2018-11-26 13:55
Location: Washington State

Re: Using MySql Views in PHP reports

Post by rpierce » 2022-04-14 18:15

Thank you Alisson, I will experiment.

Post Reply