Welcome Guest, Not a member yet? Register   Sign In
how to call function in different module?
#1

(This post was last modified: 01-21-2018, 09:04 AM by richb201.)

I have a system written in PHP/CI  that needs to interact with Google Charts. If you know of another org chart tool that runs in PHP that would be great, BTW. So I have my controller and a user clicks on my menu and want to view an org chart.

I need to be able to run a PHP function in my controller, BUT IT NEEDS to setup a javacript callback. So I need to throw some javascript into my controller. This javascript function needs to initialize the callback. I already loaded the charts lib at startup. But I need to run these two javascript lines in my controller. 

google.charts.load('current', {packages: ['corechart']});
google.charts.setOnLoadCallback(drawChart);


Then when a user actually wants to see the chart I do this is my controller:

$this->load->view('configure_chart');
$this->output->set_content_type('application/json', 'utf-8');
$this->output->set_output(file_get_contents("sampleData.json"));


The configure chart View contains HTML and javascript. I want it to run when configure_chart runs. I'd like to take all of the javascript (just a few lines) out of the view and put it in separate module. I won't be able to debug it unless it is in a separate module. 

I am concerned about setting up the OnLoadCallback since drawChart is a javascript function, not in my PHP based controller. 

How can I reference a function in another (javascrip) module in PHP? I am happy to view the docs if you can just point to where in the docs> Thx
proof that an old dog can learn new tricks
Reply
#2

(This post was last modified: 01-21-2018, 09:35 AM by dave friend.)

You need to write the logic of the "drawChart" function. It is written in JavaScript and can be in the same file with other JavaScript you write, or in its own file (recommended).
If the JavaScript file is named "mydraw" then you need the following to load it on a page.

<script type="text/javascript" src="https://yoursite.com/where/you/keep/js/mydraw.js"></script>

I'm guessing you have already seen these pages but look again starting HERE.
Reply
#3

@richb201,
Have you tried github or any other js presentation libraries? I did a quick look on github and found this https://github.com/dabeng/OrgChart
Reply
#4

(This post was last modified: 01-21-2018, 10:10 PM by richb201.)

Hey Dave! This is how I am trying to get my json data over from my controller to my view file called configure_chart.php.
       $this->load->view('configure_chart');
       $this->output->set_content_type('application/json', 'utf-8');
       $this->output->set_output(file_get_contents("sampleData.json"));
Instead of loading the library package and then setting up the callback (see below), this code just prints the HTML of configure_chart.php (un rendered) to the browser window. Any ideas on why the browser is not rendering the HTML? Is it something obvious? I am hampered by the inability of phpStorm/xDebug to allow me to debug javascript code. I am flying blind. 

this is the script inside of configure_chart.php

  <script>
       google.charts.load('current', {packages: ['corechart']});
       google.charts.setOnLoadCallback(drawChart);

   function drawChart() {
       var jsonData = $.ajax({
           url: "/Configure.php/org_chart_management",
           dataType: "json",
           async: false
       }).responseText;

       var data = new google.visualization.DataTable(jsonData);

       // Create the chart.
       // Instantiate and draw our chart, passing in some options
       var chart = new google.visualization.OrgChart(document.getElementById('chart_div'));
       // Draw the chart, setting the allowHtml option to true for the tooltips.
       chart.draw(data, {allowHtml: true});
   }
   </script>

The other thing is I am using ajax as you can see above to pass my json data from php to javascript. But in
https://www.codeigniter.com/user_guide/g...ting-loops
They are just loading a view  with the data attached:
$this->load->view('blogview', $data); to transfer an array from PHP to javascript. 
Which is the right way to do it? 

Attached Files Thumbnail(s)
   
proof that an old dog can learn new tricks
Reply
#5

(This post was last modified: 01-22-2018, 06:36 AM by XtreemDeveloper.)

Use gitLab Repository for OrgChart.

https://www.orgchartpro.com/
Reply
#6

(01-22-2018, 06:35 AM)XtreemDeveloper Wrote: Use gitLab Repository for OrgChart.

https://www.orgchartpro.com/

It looks good but it is way too expensive to use in a free product. The org chart.part of my system is really minor.
proof that an old dog can learn new tricks
Reply
#7

(This post was last modified: 01-22-2018, 07:19 AM by richb201.)

Well I looked at it again on GIThub. I reviewed the license. And from my reading of it, it is OK for me to use. But it does seem strange that going to the OrgChartPro page shows https://www.orgchartpro.com/store/ for a considerable sum. Can anyone explain to me if this is free to use? Or are they two different products?

dabeng/OrgChart is licensed under the

MIT License
A short and simple permissive license with conditions only requiring preservation of copyright and license notices. Licensed works, modifications, and larger works may be distributed under different terms and without source code.

Permissions
Commercial use
Modification
Distribution
Private use
Limitations
Liability
Warranty
Conditions
License and copyright notice
proof that an old dog can learn new tricks
Reply
#8

@richb201: Why are you re-creating the thread every time? You got three threads with the same problem.
I have already answered you why it's just outputting your html code and not render it.
https://forum.codeigniter.com/thread-698...#pid350692

You need TWO function. You can't mix view() and set_output().
Reply
#9

(This post was last modified: 01-22-2018, 08:10 AM by richb201.)

Well they actually are on slightly different topics. But I do want to know why it is displaying the text of the view (not the json).ok I get it, the setoutput needs to be in a separate function. I don't really understand why THAT would cause the output of the source. And when you say two different functions, would running the set output in a callback qualify?

I will try to aggregate into one post though.
proof that an old dog can learn new tricks
Reply
#10

(This post was last modified: 01-22-2018, 08:39 AM by jreklund.)

view() makes your site display html and later on you force the page to output it as json instead ( with output() ). And therefore your browser thinks you want json and don't actually decode the site.

No, you need to use different functions or;
Add a parameter to your function and make an if/else statement
Check if it's an AJAX request if/else statement
Reply




Theme © iAndrew 2016 - Forum software by © MyBB