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 = ?

$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">
            <?php foreach ($chart1->result() as $row) { ?>
        	    <th><?php echo $row->Y; ?></th>
		    <?php } ?>

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


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];
			url: sURL + "utility/ajaxGetMuni",
			type: "POST",
			data: {r: data[1]},
			dataType: 'json', 				
			success: function(json){
				if  (!jQuery.isEmptyObject( json.MunicipalityName )) {
					// <------- 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.
Who is Participating?
Julian HansenConnect With a Mentor Commented:
So you want to regenerate the chart based on the option selected?

I would generate the chart server side - you already have the code to do that - return the html to the AJAX call and simply replace the html on the page with the generated html.

Otherwise you are duplcating your rendering of the chart server and client side which is double work and a maintenance headache.
DanielAttardAuthor Commented:
Thanks for the help julianH.  You're comment get me onto the right track.  Problem solved.  Have a great weekend!
Julian HansenCommented:
You are welcome - thanks for the points and a good weekend to you to.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.