KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Dynamic report generation - Form Post issue #466

Open Dan Smith opened this topic on on Sep 14, 2018 - 2 comments

Dan Smith commented on Sep 14, 2018

So I'm using KoolReport to create a custom reporting module in a system I'm writing. The idea is that users can define a report by adding fields, selecting what fields to summarise in a footer etc etc.

In essence the process is as follows

  1. user creates a report definition which is saved in json format in the database
  2. php page reads the report data from the database, builds the query, parameter arrays, parameter queries, generates the parameter entry form etc when the page loads and then when they run the report it does it (... in theory)

What should happen is that when the run report button is clicked, the report gets generated. What in effect happens is that the page is just posted and I'm assuming as everything is dynamic, is starting from scratch again. I've not taken it to pieces fully to see if I get rid of the instant package and revert to the three files whether it will work but don't fully understand the mechanism koolreport uses and hence whether this would work.

I'm using the instant and inputs packages and have included the code below which should be relatively simple to understand.

Many thanks in advance for any pointers you can provide!

require_once "kreports/koolreport/autoload.php";

use \koolreport\widgets\koolphp\Table;
use \koolreport\inputs\Select2;
use \koolreport\inputs\DateRangePicker;
use \koolreport\instant\Widget;

class MyReport extends \koolreport\KoolReport
{
    use \koolreport\instant\SinglePage;
	use \koolreport\inputs\Bindable;
    use \koolreport\inputs\POSTBinding;
	
	function getData(){
		$query='select * from tblReport where rID="'.$_GET['id'].'"';	
		$result=mysqli_query($GLOBALS['link'],$query);	
		$row=@mysqli_fetch_array($result);
		$this->arrdata=json_decode(str_replace("'",'"',$row['rBody']),true);
	}
	
    function settings()
    {
        return array(
            "dataSources"=>array(
				"tag"=>array("connectionString"=>"mysql:host=localhost;dbname=".$GLOBALS['dbname'],"username"=>$GLOBALS['dbuser'],"password"=>$GLOBALS['dbpass'],"charset"=>"utf8"),  
            ),
        );
    }
    function bindParamsToInputs()
    {
		$paramarray='';
		for ($i=0; $i<count($this->arrdata); $i++){
			if ($this->arrdata[$i]['fldparameter']!='No'){
				$paramarray.='"p'.$i.'",';
			}
		}
        return array(substr($paramarray,0,strlen($paramarray)-1));
    }
    function setup()
    {
		$data=$this->getData();
		
		for ($i=0; $i<count($this->arrdata); $i++){
			if ($this->arrdata[$i]['fldparameter']!='No'){
				$this->src('tag')->query("SELECT distinct(".$this->arrdata[$i]['fldname'].") FROM tblInitial where ".$this->arrdata[$i]['fldname']." is not null ORDER BY ".$this->arrdata[$i]['fldname']." asc;")->pipe($this->dataStore("p".$i));
			}
		}
		
		$q='';
		$groupby='';
		$where='';
		$params=array();
		
		for ($i=0; $i<count($this->arrdata); $i++){
			$q.=$this->arrdata[$i]['fldname'].' as f'.$i.', ';
		}
		
		$q='select '.substr($q,0,strlen($q)-2).' FROM tblInitial ';
		
		for ($i=0; $i<count($this->arrdata); $i++){
			if ($this->arrdata[$i]['fldtotal']!='No'){
				$groupby.='f'.$i.', ';
			}
		}				
		$groupby='group by '.substr($groupby,0,strlen($groupby)-2);
		
		for ($i=0; $i<count($this->arrdata); $i++){
			if ($this->arrdata[$i]['fldparameter']!='No'){
				$where.=arrdata[$i]['fldname'].'=:p'.$i.' and ';
				$params[":p".$i]=$this->params["p".$i];
			}
		}				
		
		$where='where '.substr($where,0,strlen($where)-4);
				
		$this->src('tag')
        ->query($q.' '.$where.' '.$groupby)->pipe($this->dataStore('reportdata')); //->params($params)
    }
}

$report = new MyReport();
$report->start();

?>


		<h2><?php echo $reporttitle;?></h2>


		<form method="post">
		
		<?php
		for ($i=0; $i<count($data); $i++){
			if ($data[$i]['fldparameter']!='No'){
				echo "<div class='row'><div class='form-group'><label class='col-sm-2 control-label'>".$data[$i]['fldtitle']."</label><div class='col-md-6 form-group'>";

				if ($data[$i]['fldparameter']=='Single'){
					Select2::create(array(
						"name"=>"p".$i,
						"multiple"=>true,
						"dataStore"=>$report->dataStore("p".$i),
						"defaultOption"=>array("--"=>""),
						"dataBind"=>$data[$i]['fldname'],
						"attributes"=>array("class"=>"form-control",)
					));													
				} else if ($data[$i]['fldparameter']=='Multi'){
					Select2::create(array(
						"name"=>"p".$i,
						"dataStore"=>$report->dataStore("p".$i),
						"defaultOption"=>array("--"=>""),
						"dataBind"=>$data[$i]['fldname'],
						"attributes"=>array("class"=>"form-control",)
					));							
				} else if ($data[$i]['fldparameter']=='Date'){
					DateRangePicker::create(array(
						"name"=>"p".$i,
						"format"=>"YYYY-MM-DD"
					));							
				}
				
				echo "</div></div></div><br>";
			}
		}					
		?>
		<div class='row'>
			<div class='col-sm-2'><button class="btn btn-primary">Run Report</button></div>
		</div>	
		
		</form>
		<br><hr><br>		

		<div class='row'><div class='col-sm-12'>		
        <?php
		$cols=array();
			
		for ($i=0; $i<count($data); $i++){			
			$d=array("label"=>$data[$i]['fldtitle'],"prefix"=>$data[$i]['fldprefix'],"footer"=>$data[$i]['fldtotal'],"footerText"=>"<b>@value</b>");
			$cols["f".$i]=$d;
		}
        Widget::create(Table::class,array("dataSource"=>$report->dataStore('reportdata'),"showFooter"=>"bottom","columns"=>($cols)),false);
        ?>
		</div></div>		

<?php $report->end(); ?>
David Winterburn commented on Sep 14, 2018

Hi Dan,

Did you have any link or screenshot showing the problem? We didn't fully understand your problem right now to answer. Thanks!

Dan Smith commented on Sep 14, 2018

David, sorry it's part of a larger system so can't really send a link I'm afraid.

In essence the inputs package requires the input fields to be within a form and the method for the form is post. I think the issue is that when the form is submitted it reloads the page and therefore all the dynamic coding which builds the report is rerun. What I need to do really is a way to submit the form without reloading the page, so similar to Ajax. Is there a method to just render the report without reposting the page?

Hope that makes sense

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed

Instant