I am trying to set up a simple hit counter within expression engine using javascript and php. If a user clicks a certain link, I want it to trigger a javascript function which updates a value within my expression engine database. My setup is this:
I have a table called exp_custom_stats. Within that table are several rows of stats. For this example we'll use stat_downloads as the row that needs updating, with stat_name as the column header for stat_downloads, and stat_count as the column header for the value.
All I want to do is connect to the database, retrieve the stat_count for stat_downloads, and store it as a variable. Then on javascript click of a link, I want to increment that variable by one, and submit it back to the database as an update. The rough example would be below, but how do I connect to the DB in expression engine through a template?
DB Connection:
<开发者_Go百科?php
SELECT * FROM exp_custom_stats WHERE stat_name = 'stat_downloads';
$stat_val = 'stat_count';
$stat_val++;
echo "$stat_val";
?>
DB Update:
<script type="text/javascript">
$(document).ready(function() {
$('.stat_increment').click(function(){
<?php
INSERT INTO exp_custom_stats
VALUES ($stat_value)
WHERE stat_name = 'stat_downloads';
?>
});
});
</script>
I figured out a way to do this using a combination of AJAX, PHP, and the External Entries add-on.
精彩评论