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.
DanielAttardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DanielAttardAuthor Commented:
Thanks for the help julianH.  You're comment get me onto the right track.  Problem solved.  Have a great weekend!
0
Julian HansenCommented:
You are welcome - thanks for the points and a good weekend to you to.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.