Question : Php logical problem: How to solve "cannot redeclare class" problem?

Hi Experts,

This is sort of a long question, so please bear with me.

The main goal is to use pChart generated graphs to display information using a single pie graph generating code for multiple times (not having different graph code for each different chart). The pChart graph code is placed in an external php file. I have tried to approach this situation from two perspectives:

1st approach:
 Load the graphs as a php generated image file only having the html <img> tag in the main code which "src" is the standalone php graph generating file. (<img src="pie_graph.php" /> This works nicely for one generated graph but there is a problem when I try to generate multiple graphs in this way. Because I need to send a variable to the standalone php file, I tried to use sessions. But as the session is being changed for the next graph, it also changes the information in the previous chart for some reason. Apparently the external ph files are not being loaded before all of the php code is first executed.

Example of the code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
	// function to calculate the number male vs. female users
	function statGender() {
		
		global $_graphs;
		
		unset($_SESSION['statistics_values']);
		unset($_SESSION['statistics_labels']);
		
		$stat_out = "";
		$males = 0;
		$females = 0;
		
		// Here we retrieve the user data
		$query_user = "SELECT * FROM user WHERE verification='yes'";
		$result_user = mysql_query($query_user);
		
		// In the while loop we calculate how many male/female users there are
		while ($user = mysql_fetch_array($result_user)) {
			
			(($user['gender'] == 'm') ? $males++ : $females++ );
		
		}
		
		// Here we set up the graph variables
		$_SESSION['statistics_values'] = array($females, $males);
		$_SESSION['statistics_labels'] = array("Naisia", "Miehiä");
		$header = "Rekisteröityneistä asiakkaista<br />naisia / miehiä";
		$type = "pie";
		
		// Here we output the header and graph
		$stat_out.= "<h4>" . $header . "</h4>";
		$stat_out.= "<img src=\"" . $_graphs . $type . "_graph.php\" />";

		return $stat_out;
		
	}
	
	// function to calculate the number male vs. female users
	function statBookingsDoneEmployeeVsCustomer() {
			
		global $_graphs;
		
		unset($_SESSION['statistics_values']);
		unset($_SESSION['statistics_labels']);		
		
		$stat_out = "";
		$user_bookings = 0;
		$employee_bookings = 0;
		
		// Here we retrieve the user data
		$query_bookings = "SELECT * FROM booking";
		$result_bookings = mysql_query($query_bookings);
		
		// In the while loop we calculate how many employee/user bookings there are
		while ($bookings = mysql_fetch_array($result_bookings)) {
			
			(($bookings['user_id'] == '0') ? $employee_bookings++ : $user_bookings++);
		
		}
		// Here we set up the graph variables
		// First we make sure that the higher value will be populated first
		if ($employee_bookings > $user_bookings) {
			$_SESSION['statistics_values'] = array($employee_bookings, $user_bookings);
			$_SESSION['statistics_labels'] = array("Työntekijä", "Asiakas");
		} else {
			$_SESSION['statistics_values'] = array($user_bookings, $employee_bookings);
			$_SESSION['statistics_labels'] = array("Asiakas", "Työntekijä");
		}
		
		$header = "Varauksista tehnyt<br />asiakas / työntekijä";
		$type = "pie";
		
		// Here we output the header and graph
		$stat_out.= "<h4>" . $header . "</h4>";
		$stat_out.= "<img src=\"" . $_graphs . $type . "_graph.php\" />";

		return $stat_out;
		
	}
	
	
	$mainfield_out.= statGender();
	$mainfield_out.= statBookingsDoneEmployeeVsCustomer();



2nd approach:
Include the graph generating code to the main code. This way I would not have any problems passing variables around but this time the problem is with the classes the pChart is using. As pData class cannot be declared more than one time, this approach was a dead end as well.

Example of the code:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
	// function to calculate the number male vs. female users
	function statGender() {
		
		global $_graphs;
		
		$stat_out = "";
		$males = 0;
		$females = 0;
		
		// Here we retrieve the user data
		$query_user = "SELECT * FROM user WHERE verification='yes'";
		$result_user = mysql_query($query_user);
		
		// In the while loop we calculate how many male/female users there are
		while ($user = mysql_fetch_array($result_user)) {
			
			(($user['gender'] == 'm') ? $males++ : $females++ );
		
		}
		
		// Here we set up the graph variables
		$values = array($females, $males);
		$labels = array("Naisia", "Miehiä");
		$header = "Rekisteröityneistä asiakkaista<br />naisia / miehiä";
		$type = "pie";
		
		// Here we output the header and graph
		$stat_out.= "<h4>" . $header . "</h4>";
		include($_graphs . $type . "_graph.php");
		$stat_out.= "<img src=\"" . $_graphs . "temp.png\" />";
		//$stat_out.= "<img src=\"" . $_graphs . $type . "_graph.php\" />";

		return $stat_out;
		
	}
	
	// function to calculate the number male vs. female users
	function statBookingsDoneEmployeeVsCustomer() {
			
		global $_graphs;	
		
		$stat_out = "";
		$user_bookings = 0;
		$employee_bookings = 0;
		
		// Here we retrieve the user data
		$query_bookings = "SELECT * FROM booking";
		$result_bookings = mysql_query($query_bookings);
		
		while ($bookings = mysql_fetch_array($result_bookings)) {
			
			(($bookings['user_id'] == '0') ? $employee_bookings++ : $user_bookings++);
		
		}
		
		// Here we set up the graph variables
		// First we make sure that the higher value will be populated first
		if ($employee_bookings > $user_bookings) {
			$values = array($employee_bookings, $user_bookings);
			$labels = array("Työntekijä", "Asiakas");
		} else {
			$values = array($user_bookings, $employee_bookings);
			$labels = array("Asiakas", "Työntekijä");
		}
		
		$header = "Varauksista tehnyt<br />asiakas / työntekijä";
		$type = "pie";
		
		// Here we output the header and graph
		$stat_out.= "<h4>" . $header . "</h4>";
		include($_graphs . $type . "_graph.php");
		$stat_out.= "<img src=\"" . $_graphs . "temp.png\" />";
		//$stat_out.= "<img src=\"" . $_graphs . $type . "_graph.php\" />";

		return $stat_out;
		
	}
	
	
	$mainfield_out.= statGender();
	$mainfield_out.= statBookingsDoneEmployeeVsCustomer();


What would be the best solution to overcome either of the previous issues? If I could choose, I'd like to use the first method, as with that I wouldn't need to generate any actual image files to the hard disk, instead the images are being generated only on the screen.

The pie graph generating code, which I have either tried to have as a standalone file or as an includable file, is the following:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
<?php

	// Standard inclusions   
//	include("../../lib/pchart/pData.php"); // for the version which uses sessions to pass variables around
//	include("../../lib/pchart/pChart.php");
	include("includes/lib/pchart/pData.php");
	include("includes/lib/pchart/pChart.php");

	
	
	
	// Dataset definition 
	$DataSet = new pData;
//	$DataSet->AddPoint($_SESSION['statistics_values'],"Serie1");	// for the version which uses sessions to pass variables around
//	$DataSet->AddPoint($_SESSION['statistics_labels'],"Serie2");
	$DataSet->AddPoint($values,"Serie1");
	$DataSet->AddPoint($labels,"Serie2");
	$DataSet->AddAllSeries();
	$DataSet->SetAbsciseLabelSerie("Serie2");
	
	// Initialise the graph
	$Test = new pChart(340,235);
	$Test->setColorPalette(0,178,31,98);
	$Test->setColorPalette(1,39,132,132);
	$Test->setColorPalette(2,200,200,200);
	$Test->setColorPalette(3,150,150,150);
	$Test->setColorPalette(4,100,100,100);

	$Test->drawFilledRectangle(0,0,340,300,250,250,250);
	
	// Draw the pie chart
	$Test->setFontProperties("includes/content/graphs/graph_fonts/tahoma.ttf",9);
	$Test->drawFlatPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),130,120,80,PIE_PERCENTAGE_LABEL,4);
	$Test->drawPieLegend(260,20,$DataSet->GetData(),$DataSet->GetDataDescription(),240,240,240);
	$Test->Render('includes/content/graphs/temp.png');
	
?>

Answer : Php logical problem: How to solve "cannot redeclare class" problem?

Fine, since the web distribution is grayed out we will create a new oab

Create a new oab, enabled web based distribution and make it as default.. This should work..
Random Solutions  
 
programming4us programming4us