php Include Error

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
doctorbillTechAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Hi there,

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

You might also want to consider using require_once instead

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gr8gonzoConsultantCommented:
Change include to include_once
gr8gonzoConsultantCommented:
Ohhhh, Chris beat me by barely a couple of seconds.
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

doctorbillTechAuthor Commented:
Will try
doctorbillTechAuthor 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>
doctorbillTechAuthor 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
gr8gonzoConsultantCommented:
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?
doctorbillTechAuthor 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
doctorbillTechAuthor Commented:
I will try it as an include on a new test page
doctorbillTechAuthor Commented:
Ah ha - getting somewhere. This does not work
doctorbillTechAuthor 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>
doctorbillTechAuthor Commented:
So there must be something on the previous page that is causing it to break
doctorbillTechAuthor 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
Chris StanyonWebDevCommented:
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).
doctorbillTechAuthor 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>
doctorbillTechAuthor Commented:
I tried removing some html elements but no joy
Chris StanyonWebDevCommented:
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

doctorbillTechAuthor 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
Chris StanyonWebDevCommented:
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.
doctorbillTechAuthor 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
Chris StanyonWebDevCommented:
Excellent. Glad we got there :)
doctorbillTechAuthor 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.