Link to home
Start Free TrialLog in
Avatar of DanielAttard
DanielAttardFlag for Canada

asked on

Process JSON response to update a table / chart

I currently have a table/chart which is hard-coded to display results for a particular municipality.  In my case, the municipality is 'London'.  Here is the code which generates the query results, which then becomes the data source for the table:

$sql1 = "SELECT
	SaleYear AS Y,
	NewSaleType AS T,
	count(*) AS C,
	tblsales.Municipality AS M,
	format((sum(SalePrice) / sum(Quantity1)),0) AS R
FROM	tblsales
WHERE	tblsales.SaleYear > 2007
AND tblsales.Quantity1 > 2000
AND (tblsales.NewSaleType = 'Industrial')
AND tblsales.Municipality = ?
GROUP BY T,M,Y
ORDER BY	 T,M,Y ";

$chart1 = $this->db->query($sql1, array('London') );

Open in new window


The database object, $chart1 , is used in a foreach loop to display the results in a table.  Here is the code which generates the table:

<table class="chart" data-legend-position="nw" data-hide-table="false" data-type="lines" data-stack="true" data-tooltip-pattern="The Average %2 per sf was $%1 in %3">
    <thead>
        <tr>
            <th></th>
            <?php foreach ($chart1->result() as $row) { ?>
        	    <th><?php echo $row->Y; ?></th>
		    <?php } ?>
        </tr>
    </thead>

    <tbody>
       <tr>
            <th>Rate Per SF</th>
            <?php foreach ($chart1->result() as $row) { ?>
        	    <td><?php echo $row->R; ?></td>
			<?php } ?>
        </tr>

    </tbody>
</table>                      

Open in new window


Up to this point everything is working fine, except for the fact we are currently hard-coded to one specific value.  I need to substitute this string:  array('London')  and allow for some asynchronous javascript magic to take place.  I want to use an autocomplete field and whenever a municipality is selected, this should automatically recreate the table based on the value of the selected municipality.

Here is the ajax request that I have so far.  When the ajax requests returns with success, I am able to insert the data that is passed back into my #response element (for example <div id="response"></div>).  But this alone does not refresh the table.  

$("#AverageSalePricesCity").result(function(event, data, formatted) {
	if (data){
		//var r = data[1];
		$.ajax({ 
			url: sURL + "utility/ajaxGetMuni",
			type: "POST",
			data: {r: data[1]},
			dataType: 'json', 				
			success: function(json){
				if  (!jQuery.isEmptyObject( json.MunicipalityName )) {
					//YES 
					$('#response').html(json.MunicipalitiesCounty);
					// <------- WHAT NEEDS TO BE DONE HERE TO UPDATE $chart1 ???
				} else { 
					// NO
				}
			}
		});		  
	}
});

Open in new window


I tried to replace array('London') with $this->input->post('AverageSalePricesCity'), but that didn't seem to work, although maybe I did not code it correctly.  Am I on the right track here?  I seem to have all the necessary elements to make this table/chart dynamic, but I can't seem to figure it out.  I think I need to be able to re-load the div with my updated table data.

Thanks for any help you can offer.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DanielAttard

ASKER

Thanks for the help julianH.  You're comment get me onto the right track.  Problem solved.  Have a great weekend!
You are welcome - thanks for the points and a good weekend to you to.