Jump to content

Create Multidimensional Array from MySQL Query


billsinc

Recommended Posts

I'm try to create a multidimensional array from a MySQL query for use with Fusion Charts.  I've managed to piece together code from some of their examples for some of the simpler things but I can't seem to create this array.  My code is as follows:

<?php
// Connect to the DB
$link = connectToDB();

//$strXML will be used to store the entire XML document generated
//Generate the chart element
$strXML = "<chart caption='Net Income by Property' subCaption='' animation='1' showBorder='1' decimals='2' numberPrefix='$' yAxisName='Revenue'>";

//Initialize <categories> element - necessary to generate a multi-series chart
$strCategories = "<categories>";

//Initiate <dataset> elements
$strDataGross = "<dataset seriesName='Gross Value'>";
$strDataOwn = "<dataset seriesName='Owner Value'>";

// Fetch properties by ip
$strQuery1 = "SELECT element_2 FROM ap_form_1 WHERE ip_address ='$IP_Addr' GROUP BY ip_address, element_1, element_2";
$result[1] = mysql_query($strQuery1) or die(mysql_error());
$result_num[1]=mysql_numrows($result[1]);
$i=0;

	while ($i < $result_num[1]) {
  			array_push($result[1],mysql_result($result_num[1],$i,"element_2"));
  			$i++;
		}

// Fetch all gross revenue records by ip
$strQuery2 = "SELECT SUM(element_7) as sumelement7 FROM ap_form_1 WHERE ip_address ='$IP_Addr' GROUP BY ip_address, element_1, element_2";
$result[2] = mysql_query($strQuery2) or die(mysql_error());
$result_num[2]=mysql_numrows($result[2]);
$i=0;

	while ($i < $result_num[2]) {
  			array_push($result[2],mysql_result($result_num[2],$i,"sumelement7"));
  			$i++;
		}

// Fetch all own revenue records by ip
$strQuery[3] = "SELECT SUM(element_9) as sumelement9 FROM ap_form_1 WHERE ip_address ='$IP_Addr' GROUP BY ip_address, element_1, element_2";
$result[3] = mysql_query($strQuery[3]) or die(mysql_error());
$result_num[3] = mysql_numrows($result[3]);
$i=0;

	while ($i < $result_num[3]) {
  			array_push($result[3],mysql_result($result_num[3],$i,"sumelement9"));
  			$i++;
		}


//Iterate through the data  
foreach ($result as $resultSub) {
        //Append <category name='...' /> to strCategories
        $strCategories .= "<category name='" . $resultSub[1] . "' />";
        //Add <set value='...' /> to both the datasets
        $strDataGross .= "<set value='" . $resultSub[2] . "' />";
        $strDataOwn .= "<set value='" . $resultSub[3] . "' />";
}

//Close <categories> element
$strCategories .= "</categories>";

//Close <dataset> elements
$strDataGross .= "</dataset>";
$strDataOwn .= "</dataset>";

//Assemble the entire XML now
$strXML .= $strCategories . $strDataGross . $strDataOwn . "</chart>";


//Create the chart with data from $strXML
echo renderChart("charts/Column3D.swf", "", $strXML, "PropertyValue", "100%", 400, false, false);


mysql_close($link);
?>

 

Many thanks in advance!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.