php Include Error

doctorbill
doctorbill used Ask the Experts™
on
I have included the following 2 pages in my index page:

include:
2dSupportDatabase_Thumb.php
This page has this line: include("includes/fusioncharts.php");

include:
2dSupportDatabase_MB_Thumb.php
This page also has this line: include("includes/fusioncharts.php");

I get the following:
Fatal error: Cannot redeclare class FusionCharts in C:\xampp\htdocs\Development\Inventas\Sites\Charts\MyPHPDatabaseExamples\includes\fusioncharts.php on line 3

How can I avoid this error and get both includes showing on the index page
fusioncharts.php
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Hi there,

Use include_once instead of include and you'll avoid the problem.

You might also want to consider using require_once instead
Commented:
Change include to include_once

Commented:
Ohhhh, Chris beat me by barely a couple of seconds.
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Author

Commented:
Will try

Author

Commented:
I have now edited the following files:
2dSupportDatabase_Thumb.php
This page has this line: include_once("includes/fusioncharts.php");

include:
2dSupportDatabase_MB_Thumb.php
This page also has this line: include_once("includes/fusioncharts.php");

Thing is, I cannot get this line to show the include (line in bold). It shows up as an independant file when referenced by the file name from a browser:

<div class = "pageheading"><h3>Metrics by Ticket Times</h3></div>
        <div style="display:inline-block; float:left; padding:25px"></div><a href = "chart_sample_BV_inc_AllMetrics_Times_large.php" target="_blank" ><div id="chart-container-7" style="display:inline-block; float:left"><?php include "Charts/chart_sample_BV_inc_AllMetrics_Times.html" ?></div></a>
        <a href = "chart_sample_MB_inc_AllMetrics_Times_large.php" target="_blank" ><div id="chart-container-8" style="display:inline-block; float:left"><?php include "Charts/chart_sample_MB_inc_AllMetrics_Times.html" ?></div></a>
        <div style="display:inline-block; float:left"></div><a href = "Charts/MyPHPDatabaseExamples/2dSupportDatabase.php" target="_blank" ><div id="chart-1"><?php include "Charts/MyPHPDatabaseExamples/2dSupportDatabase_Thumb.php" ?></div></a>
       <div style="display:inline-block; float:left"></div><a href = "" target="_blank" ><div id="chart-2"><?php include "Charts/MyPHPDatabaseExamples/2dSupportDatabase_MB_Thumb.php" ?></div></a>
        <br /><div style="height: 10px;"></div><br /><br />
        <div style = "margin-left:50px; id=footer">
        <p><r>Generated for Inventas</r></p>
            <span3><p>Inventas Ltd cannot guarentee anything, especially if Russell did it. You're lucky the building isn't on fire or your car isn't ruined. Make sure you pay your bill or we send the boys round.</p></span3>
          </div>
        </body>
</html>

Author

Commented:
By "It shows up as an independent file when referenced by the file name from a browser" I mean the page shows the file content correctly

Commented:
Are you saying that instead of this:
<?php include "Charts/MyPHPDatabaseExamples/2dSupportDatabase_MB_Thumb.php" ?>
...actually executing, it just shows that PHP code just like that in the browser / HTML source?

Author

Commented:
If I use this link:

http://localhost:888/inventassites/Charts/MyPHPDatabaseExamples/2dSupportDatabase_MB_Thumb.php

I get the correct display - see the attached image of the display
Capture.PNG

Author

Commented:
I will try it as an include on a new test page

Author

Commented:
Ah ha - getting somewhere. This does not work

Author

Commented:
It works on a new test page as an include:

  <body>
        <div style="display:inline-block; float:left"></div><a href = "" target="_blank" ><div><?php include "2dSupportDatabase_MB_Thumb.php" ?></div></a>
        <br /><div style="height: 10px;"></div><br /><br />
        <div style = "margin-left:50px; id=footer">
        <p><r>Generated for Inventas</r></p>
            <span3><p>Inventas Ltd cannot guarentee anything, especially if Russell did it. You're lucky the building isn't on fire or your car isn't ruined. Make sure you pay your bill or we send the boys round.</p></span3>
          </div>
        </body>

Author

Commented:
So there must be something on the previous page that is causing it to break

Author

Commented:
The include statements are referencing the attached files - I suppose the problem is that some of the variable names in the 2 files are the same
2dSupportDatabase_MB_Thumb.php
2dSupportDatabase_Thumb.php
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
The 2 files you're trying to include are full HTML pages, so you appear to be trying to include 2 full html pages inside of a DIV. Pretty sure that won't work. Normally, when you do an include, you're just including a snippet (a part of a page or script).

I would remove all the <html> page  tags from your included file and just run the DB stuff and generate a partial html page (i.e. the bit that includes the chart).

Author

Commented:
This is my Index page with the includes:

 <div style="display:inline-block; float:left"></div><a href = "Charts/MyPHPDatabaseExamples/2dSupportDatabase.php" target="_blank" ><div id="chart-1"><?php include "Charts/MyPHPDatabaseExamples/2dSupportDatabase_Thumb.php" ?></div></a>
<div style="display:inline-block; float:left"></div><a href = "" target="_blank" ><div id="chart-2"><?php include "Charts/MyPHPDatabaseExamples/2dSupportDatabase_MB_Thumb.php" ?></div></a>

The first one works and shows the page as a small window on the page
The second link does not work

If I swap the order of the include links, again only the first one works,:

 <div style="display:inline-block; float:left"></div><a href = "Charts/MyPHPDatabaseExamples/2dSupportDatabase.php" target="_blank" ><div id="chart-1"><?php include "Charts/MyPHPDatabaseExamples/2dSupportDatabase_MB_Thumb.php" ?></div></a>

<div style="display:inline-block; float:left"></div><a href = "" target="_blank" ><div id="chart-2"><?php include "Charts/MyPHPDatabaseExamples/2dSupportDatabase_Thumb.php" ?></div></a>

Author

Commented:
I tried removing some html elements but no joy
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
OK. LIke I said, you're including full HTML pages within your DIV tags, so that needs to be sorted. Strip out all the <html>, <head>, <body> tags etc. Your include files should just be redering the DIV. You'll probably want to include the JS and CSS in the index file, and then include your 2 PHP files.

Something else to look at - in both of your include files you have a div with an ID of chart-1. This is used to render the chart. An ID needs to be unique in a document, so you will want to change one of those (set it to chart-2 for example) and then change the calling code:

$columnChart = new FusionCharts("pie3D", "myFirstChart" , 400, 250, "chart-2", "json", $jsonEncodedData);
...
<div id="chart-2"><!-- Fusion Charts will render here--></div>

Open in new window

Author

Commented:
Will try to remove all the html code - I have already tried this but maybe I missed some
I also renamed the charts to chart-1 and chart-2
Will get back to you

I appreciate that you and gr8gonzo have already answered the original question so I may just close this and continue my research as the initial include error is now gone
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
If it helps you to visualise what's going on, think about it like this. When you do an include, it's exactly the same as if you'd typed that code directly into your page. If you think about it like that, you'll be able to see exactly what your final page will render like and you'll probably get a clearer indication of why things aren't working the way you expect.

If I were you, I would create my index page to include the fusion charts js, and your 2 PHP files.

<html>
  <head>
    <title>FusionCharts XT - Column 2D Chart - Data from a database</title>
    <link  rel="stylesheet" type="text/css" href="css/style.css" />
    <script src="js/fusioncharts.js"></script>
    <script src="js/themes/fusioncharts.theme.carbon.js"></script>
  </head>
   <body>
    <div>
      <?php include "Charts/MyPHPDatabaseExamples/2dSupportDatabase_Thumb.php" ?>
    </div>
    <div>
      <?php include "Charts/MyPHPDatabaseExamples/2dSupportDatabase_MB_Thumb.php" ?>
    </div>
  </body>
</html>

Open in new window

Then in each of your 2 PHP files, just include the code needed to generate the Chart:

<?php
require_once "includes/fusioncharts.php";
require_once "db_connection.php";

$strQuery = "SELECT sup_contact, sup_timespent FROM support WHERE sup_contact='Michael Brooks'";
$result = $dbhandle->query($strQuery) or exit("Error code ({$dbhandle->errno}): {$dbhandle->error}");
...

$columnChart = new FusionCharts("pie3D", "myFirstChart" , 400, 250, "chart-1", "json", $jsonEncodedData);
$columnChart->render();
?>
<div id="chart-1"><!-- Fusion Charts will render here--></div>

Open in new window

Do the same for the other PHP file but change the ID of the DIV that holds the chart.

Author

Commented:
Removed all the html and other tags
Also edited these lines and renamed the chart on 1 of the files. "MyFirstChart2" also needed to be renamed:
$columnChart = new FusionCharts("pie3D", "myFirstChart2" , 400, 250, "chart-2", "json", $jsonEncodedData);

          // Render the chart
          $columnChart->render();

          // Close the database connection
          $dbhandle->close();
      }
    ?>
    <div id="chart-2"><!-- Fusion Charts will render here--></div>

Now all working - thanks a million
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Excellent. Glad we got there :)

Author

Commented:
Thanks again for your help
I know that the original question was answered by both (the error message) - I should have raised another question on the include but I was unsure if the issue was unrelated
I have reordered the code as suggested to make things clearer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial