Welcome Guest, Not a member yet? Register   Sign In
passing a string to a view
#1

I have a function in my controller
public function org_chart_management()
{
   $this->load->dbutil();
   $query = $this->db->query("SELECT * FROM employees");
 //echo $this->dbutil->csv_from_result($query);  //write the string
   $string2 = $this->dbutil->csv_from_result($query);  //write the string
   $this->load->view('configure_chart.php',$string2);
  // echo '<script src="/views/configure"></script>';
  // echo $string2;

//    echo '<script type="text/javascript">$string2</script>';

}

And here is the view called configure_chart.php

<!DOCTYPE html>
<html>
<head>
   <title>Substantiator</title>
  <meta charset="utf-8" />
   <meta name="viewport" content="width=device-width, initial-scale=1.0">

   </head>
<body>
<html>
<head>
   <br><br><br><br><br><br>
   <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   <script type="text/javascript">
       google.charts.load('current', {packages:["orgchart"]});
       google.charts.setOnLoadCallback(drawChart);

       function drawChart() {

           var data = new google.visualization.DataTable();
           data.addColumn('string', 'Name');
           data.addColumn('string', 'Manager');
           data.addColumn('string', 'ToolTip');

           // For each orgchart box, provide the name, manager, and tooltip to show.
           data.addRows([
               [{v:'Mike', f:'Mike<div style="color:red; font-style:italic">President</div>'},
                   '', 'The President'],
               [{v:'Jim', f:'Jim<div style="color:red; font-style:italic">Vice President</div>'},
                   'Mike', 'VP'],
               ['Alice', 'Mike', ''],
               ['Bob', 'Jim', 'Bob Sponge'],
               ['Carol', 'Bob', '']
           ]);

           // Create the chart.
           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>
</head>
<body>

</body>


<!--Div that will hold the pie chart-->
<br><br><br>

<body>
<div id="chart_div"></div>
</body>

</body>
</html>


when I run org_chart_management() I get the following screen. This chart that you see if from hard coded sample data. I really want to pass $string2 to configure_chart.php. I tried doing echo $string2 in the controller, but that just prints the $string2 to the view. Can someone tell me how to get the $string2 into the view?

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

PHP Code:
$data['string2'] = $this->dbutil->csv_from_result($query);
$this->load->view('configure_chart'$data); 

The manual are always a good start to look at.
You need to pass an array with an index, and later on access that index as $string2 in your view.
https://www.codeigniter.com/user_guide/g...o-the-view
Reply
#3

You can also use an un-documentented CI method.

PHP Code:
$data['string2'] = $this->dbutil->csv_from_result($query);
$this->load->vars($data); 

That way will also make the variables available to all of your views.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

I think it may have worked. Not really sure since I can't debug javascript code in a View (xdebug/phpStorm). What I'd like to do is take the javascript out of view configure_chart and move it to a javascript file. Notice I left a few lines to register the callback. I know I can get the javascript code in the view file to run by doing
$this->load->view('configure_chart', $data);

But how can I get the drawChart() in javascript_funcs() to run?

Here is the way the View is now

<html>
<head>
<br><br><br><br><br><br>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {packages:["orgchart"]});
google.charts.setOnLoadCallback(drawChart);

</script>
</head>

<!--Div that will hold the pie chart-->
<br><br><br>

<body>
<div id="chart_div"></div>
</body>

I moved the drawChart into my javascript_funcs file. How do I invoke drawChart when it is in its own javascript file? And does string passing still work?
proof that an old dog can learn new tricks
Reply
#5

I also tried using window.alert($data['string2']); in the view (since my degugger won't work). It is not working.
proof that an old dog can learn new tricks
Reply
#6

You need to access it like this in the view:
PHP Code:
<?php echo $string2?>
Reply
#7

Well I can print it to the screen with my <?php echo string2; ?> from within my view!  Smile

Here is part of the view:
<head>
   <br><br><br><br>
   <?php echo $string2; ?>
   <br><br><br><br>
   <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   <script type="text/javascript">
       google.charts.load('current', {packages:["orgchart"]});
       google.charts.setOnLoadCallback(drawChart);

       function drawChart() {

           var data = new google.visualization.DataTable();
           data.addColumn('string', 'Name');
           data.addColumn('string', 'Manager');
           data.addColumn('string', 'ToolTip');
          // echo xhttp.responseText;
           // For each orgchart box, provide the name, manager, and tooltip to show.
          data.addRows(<?php $string2; ?>   ???????
 
How can I get $string2 into the "data" structure?
proof that an old dog can learn new tricks
Reply
#8

You echo it out there too...
I don't know the requirements for your javascript, but looks like an array, so depending on how $string2 looks, you may need to make a foreach and generate a new string/javascript array so it looks like your first post.
Reply
#9

I did find a google charts function called arrayToDataTable(). Using this would allow me to populate the dataTable easily. Perhaps on the

$this->db->select('employee,manager,employee_title');
$this->db->where('email', $email);
$this->db->where('campaign', $campaign);
$query=$this->db->get('employees');
$data['string2'] = $this->dbutil->xml_from_result($query); //write the string

but if I used split instead and tried sending it over as an array by doing something like this in the controller function

$string2=$query.split

It would be easier to populate the dataTable. Any idea if I can send an array (not just a string) to my View?
proof that an old dog can learn new tricks
Reply
#10

(01-18-2018, 03:47 PM)richb201 Wrote: Any idea if I can send an array (not just a string) to my View?

Everything you have asked so far is in the manual. You should always start there.
Views - Creating Loops
Reply




Theme © iAndrew 2016 - Forum software by © MyBB