[eluser]Corey Freeman[/eluser]
I found this tutorial:
http://ad1987.blogspot.com/2009/02/reddi...l-and.html and tried adapting it to codeigniter. Of course, I got nothing...
VIEW
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="UTF-8">
<head>
<title><?=$title ?> | ReveCloud - Aspiration Sharing</title>
<meta name="author" content="Minus the Pie Media" />
<meta name="description" content="goal setting, dream sharing, goal social network, challenge setting" />
<meta name="description" content="A website that allows you to share your goals and get encouragement to pursue your dreams." />
<link rel="stylesheet" type="text/css" href="<?=base_url() ?>style.css" />
[removed][removed]
[removed]
$(function(){
$("a.vote_up").click(function(){
//get the id
the_id = $(this).attr('id');
// show the spinner
$(this).parent().html("<img src='images/spinner.gif'/>");
//fadeout the vote-count
$("span#votes_count"+the_id).fadeOut("fast");
//the main ajax request
$.ajax({
type: "POST",
data: "action=vote_up&id;="+$(this).attr("id"),
url: "http://localhost:8888/dreams/index.php/votes",
success: function(msg)
{
$("span#votes_count"+the_id).html(msg);
//fadein the vote count
$("span#votes_count"+the_id).fadeIn();
//remove the spinner
$("span#vote_buttons"+the_id).remove();
}
});
});
});
[removed]
</head>
<body>
<div id="wrapper">
<div id="header">
<div id="logo">
<?php if ($this->session->userdata('is_logged_in') != TRUE) { ?>
<a href="<?=base_url() ?>"><img src="<?php echo base_url(); ?>images/logo.png" alt="Project 27" /></a>
<?php }
else { ?>
<a href="<?=base_url() ?>/index.php/dashboard"><img src="<?php echo base_url(); ?>images/logo.png" alt="Project 27" /></a>
<?php } ?>
</div>
<div id="user-info">
<?php foreach($query->result() as $row): ?>
<strong><a >id; ?>"><?=$row->username; ?></a></strong>
<?php endforeach; ?>
</div>
</div>
<div id="container">
<div id="profile-info">
<?php foreach($query->result() as $row): ?>
<img >avatar; ?>" alt="<?=$row->username; ?>" />
<p><h3><?=$row->username; ?></h3>
This is where the description goes.</p>
<?php endforeach; ?>
</div>
<table id="goals">
<tbody>
<?php foreach($goals->result() as $row):
$effective_vote = $row->votes;
?>
<tr>
<td><?=$row->id ?></td>
<td><?=$row->image ?></td>
<td><?=$row->goal ?></td>
<td>
<span class='votes_count' id='votes_count<?=$row->id; ?>'><?php echo $effective_vote." votes"; ?></span>
<span class='vote_buttons' id='vote_buttons<?=$row->id; ?>'>
<a href='[removed];' class='vote_up'>id; ?>'></a>
</span>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php $this->load->view('footer'); ?>
Votes Controller
Code:
<?php
class Votes extends Controller {
function Votes() {
parent::Controller();
}
function index() {
$id = $_POST['id'];
$action = $_POST['action'];
$cur_votes = getAllVotes($id);
$votes_up = $cur_votes[0]+1;
$data = array(
'votes' => $votes_up
);
$r = $this->db->update('goals', $data);
if($r) //voting done
{
$effectiveVote = getEffectiveVotes($id);
echo $effectiveVote." votes";
}
elseif(!$r) //voting failed
{
echo "Failed!";
}
}
function getAllVotes($id) {
$votes = array();
$this->db->where('id', $id);
$this->db->get('goals');
if($query->num_rows == 1) {
$votes[0] = $row['votes'];
}
return $votes;
}
function getEffectiveVotes($id) {
$votes = getAllVotes($id);
$effectiveVote = $votes[0];
return $effectiveVote;
}
}
From what I understand, the ajax runs the PHP stuff in the browser, then the javascript makes it look all pretty. I assume my problem lies with getting the ajax to run the controller? I adapted it from the tutorial to have only a vote-up function...so I may have messed up there too...
help?