billsinc

Multidimensional Dimensional Array from MySQL

Recommended Posts

I'm using the example code to try to create a multidimensional array from a MySQL table. I'm still very new to PHP so I know I'm not constructing or parsing the array properly. Here's my code:

 

 

 






<?php

include("Includes/FusionCharts.php");

include("Includes/DBConn.php");

$IP_Addr = $_SERVER["REMOTE_ADDR"]; 

?>

<?php

// Connect to the DB

$link = connectToDB();



//$strXML will be used to store the entire XML document generated

//Generate the chart element

$strXML = "";



//Initialize  element - necessary to generate a multi-series chart

$strCategories = "";



//Initiate  elements

$strDataGross = "";

$strDataOwn = "";



// 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());



// Fetch all gross revenue records by ip

$strQuery2 = "SELECT SUM(element_7) 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());



// Fetch all owner revenue records by ip

$strQuery3 = "SELECT SUM(element_9) as TotOwnGrossVal FROM ap_form_1 WHERE ip_address ='$IP_Addr' GROUP BY ip_address, element_1, element_2";

$result[3] = mysql_query($strQuery3) or die(mysql_error());





//Iterate through the data  

foreach ($result as $resultSub) {

       //Append  to strCategories

       $strCategories .= "";

       //Add  to both the datasets

       $strDataGross .= "";

       $strDataOwn .= "";

}



//Close  element

$strCategories .= "";



//Close  elements

$strDataGross .= "";

$strDataOwn .= "";



//Assemble the entire XML now

$strXML .= $strCategories . $strDataGross . $strDataOwn . "";





//Create the chart with data from $strXML

echo renderChart("charts/Column3D.swf", "", $strXML, "PropertyValue", "100%", 400, false, false);





mysql_close($link);

?>

Share this post


Link to post
Share on other sites
$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());

You need to use mysql_result first and push the results of the query into an array.

$result_num=mysql_numrows($result[1]);

$i=0;

while ($i < $result_num)

 {

array_push($someVariable[1],mysql_result($result_num,$i,"<fieldname>"));

$i++;

 }

Share this post


Link to post
Share on other sites

Thank you, that helped and I think I'm almost there. I've got the two categories to display properly, but I can't populate the second one. I know my syntax is wrong for the IF statment, but this is what I have:

 

	$result = mysql_query($strQuery) or die(mysql_error());



// Add Categories

$FC->addCategory("Property Gross Value");

$FC->addCategory("Owner Share");



if ($result) {

	$FC->addDatasetsFromDatabase($result, "PurDate", "TotGrossVal");

	$FC->addDatasetsFromDatabase($result, "PurDate", "TotOwnShare");

}

Share this post


Link to post
Share on other sites

Hi,

 
addDatasetsFromDatabase(resource $query_result, string $ctrlField, string $valueField[, array $datasetParamArray, string $link]) :
 
FC->addDatasetsFromDatabase() fetches all datas from the database with the control break field specified at the second parameter, also this $ctrlField will be the seriesName for the chart, and $valueField will be the <set>'s value inside that datasets.
Also could you please make sure that you have more than one record in the $ctrlField?
 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now