Feedback
This page demonstrates how to store the feedback into database with xajax in Code Igniter. One each callback, all feedback is queried from database and sent back with pagination. Please do not abuse the database, thank you.
| Title | Name | Post Date/Time | Comments |
|---|---|---|---|
| dfsdgsdgsdgsdg | sdgsdgsdg | 2008-07-08 16:15:57 | gdsgsdg |
| jona | kela | 2008-07-08 14:22:48 | blabla |
| asda | sdas | 2008-07-08 14:08:17 | ds |
| ha | hu | 2008-07-08 11:46:13 | hi |
| teste | nuno | 2008-07-08 10:40:40 | yada yada yada |
Model: feedback_model.php
<?php
class feedback_model extends Model
{
function feedback_model()
{
parent::Model();
}
function get_row_count()
{
return ($this->db->count_all('feedback'));
}
function get_feedbacks($per_page, $offset)
{
$this->db->use_table('feedback');
$this->db->orderby('post_dt', 'desc');
$this->db->limit($per_page);
if (isset($offset))
{
$this->db->offset($offset);
}
$query = $this->db->get();
return ($query);
}
function insert_feedback($title, $name, $comments)
{
$data = array(
'title' => $title,
'name' => $name,
'comments' => $comments,
'post_dt' => date("Y-m-d G:i:s")
);
$this->db->insert('feedback', $data);
}
}
?>
Controller: feedback.php
<?php
class feedback extends Controller
{
var $ROW_PER_PAGE = 5;
var $TITLE_WORD_WRAP = 25;
function feedback()
{
parent::Controller();
$this->load->helper(array('text', 'typography'));
$this->load->library('pagination');
$this->load->model('feedback_model', 'feedback_model', true);
}
function index()
{
// XAJAX
$this->load->library('xajax');
$this->xajax->registerFunction(array("process_form_data", &$this, "process_form_data"));
$this->xajax->processRequests();
$data['xajax_js'] = $this->xajax->getJavascript(null, '/js/xajax.js');
// load views
$data['head_title'] = WEBSITE_NAME . " - Examples: Feedback";
$data['highlighted_controller'] = highlight_file("feedback.php", TRUE);
// get offset
$offset = $this->uri->segment(3, '');
// all_feedbacks
$data['all_feedbacks'] = $this->_get_feedbacks($this->ROW_PER_PAGE, $offset);
// load example MVC
$example = new example('feedback', true);
$data['highlighted_model'] = $example->model;
$data['highlighted_view'] = $example->view;
$data['subheader'] = $this->load->view('examples_subheader', $data, true);
$data['sidebar'] = $this->load->view('examples_sidebar', $data, true);
$data['body_content'] = $this->load->view('feedback_tpl', $data, true);
$this->load->view('site_layout', $data);
}
function process_form_data($form_data)
{
// get offset
$offset = $this->uri->segment(3, '');
$objResponse = new xajaxResponse();
$insert_ok = $this->_insert_feedback($form_data['txt_title'], $form_data['txt_name'], $form_data['txt_comments']);
if ($insert_ok)
{
$result = "Add feedback successfully!";
}
else
{
$result = "Fail to add feedback!";
}
$result .= "<br/><br/>" . $this->_get_feedbacks($this->ROW_PER_PAGE, $offset);
$objResponse->addAssign("div_result", "innerHTML", $result);
return $objResponse;
}
function _insert_feedback($title, $name, $comments)
{
if (isset($title) && !empty($title))
{
$this->feedback_model->insert_feedback($title, $name, $comments);
return (true);
}
return (false);
}
function _get_feedbacks($per_page, $offset)
{
$result = "";
$query = $this->feedback_model->get_feedbacks($per_page, $offset);
if ($query->row() > 0)
{
$result .= "<table class='comments'>";
$result .= "<thead>";
$result .= "<tr>";
$result .= "<th width='10%'>Title</th>";
$result .= "<th width='10%'>Name</th>";
$result .= "<th width='15%'>Post Date/Time</th>";
$result .= "<th>Comments</th>";
$result .= "</tr>";
$result .= "</thead>";
$result .= "<tbody>";
foreach ($query->result() as $row)
{
$result .= "<tr>";
$result .= "<td>" . word_wrap($row->title, $this->TITLE_WORD_WRAP) . "</td>";
$result .= "<td>" . word_wrap($row->name, $this->TITLE_WORD_WRAP) . "</td>";
$result .= "<td>" . $row->post_dt . "</td>";
$result .= "<td>" . nl2br_except_pre(word_wrap($row->comments)) . "</td>";
$result .= "</tr>";
}
$result .= "</tbody>";
$result .= "</table>";
// set pagination
$config['base_url'] = '/feedback/index/';
$config['total_rows'] = $this->feedback_model->get_row_count();
$config['per_page'] = $per_page;
$this->pagination->initialize($config);
$result .= $this->pagination->create_links();
}
return ($result);
}
}
?>
View: feedback_tpl.php
<style>
table.comments
{
color: inherit;
background: inherit; /* Don't make tables transparent */
border-collapse: collapse;
border-spacing: 0;
empty-cells: show;
margin: 0.5em 2.5em;
padding: 0;
border: 1px solid #999999;
}
table.comments th
{
font-weight: bold;
color: inherit;
background: #F0F0F0;
text-align: left;
vertical-align: left;
margin: 0;
padding: 0.25em 0.5em;
border: 1px solid #999999;
}
table.comments td
{
text-align: left;
vertical-align: top;
margin: 0;
padding: 0.25em 0.5em;
border: 1px solid #999999;
}
</style>
<script type="text/javascript">
<!--
xajax.loadingFunction = function(){xajax.$('loading_message').style.display='block';};
xajax.doneLoadingFunction = function(){xajax.$('loading_message').style.display='none';};
// -->
</script>
<script type="text/javascript">
function do_submit()
{
if (xajax.$('txt_title').value == '' || xajax.$('txt_name').value == '' || xajax.$('txt_comments').value == '')
{
alert("Please enter title, name, and comments!");
}
else
{
xajax_process_form_data(decodeURIComponent(xajax.getFormValues('form_feedback')));
xajax.$('txt_title').value = '';
xajax.$('txt_name').value = '';
xajax.$('txt_comments').value = '';
}
}
</script>
<div id="loading_message" class="loadingMessage">Loading ...</div>
<h1 id="introduction">Feedback</h1>
<p>
This page demonstrates how to store the feedback into database with <?=XAJAX_LINK_POPUP?> in <?=CODE_IGNITER_LINK_POPUP?>. One each callback, all feedback is queried from database and sent back with <a href="http://www.codeigniter.com/user_guide/libraries/pagination.html" target="_new">pagination</a>. Please do not abuse the database, thank you.
</p>
<form id="form_feedback" name="form_feedback">
<table>
<tr>
<td valign="top">* Title: </td>
<td><input type="text" id="txt_title" name="txt_title" size="40" maxlength="100" /></td>
</tr>
<tr>
<td valign="top">* Name: </td>
<td><input type="text" id="txt_name" name="txt_name" size="40" maxlength="100" /></td>
</tr>
<tr>
<td valign="top">* Comments: </td>
<td><textarea id="txt_comments" name="txt_comments" rows="6" cols="40" maxlength="255"></textarea></td>
</tr>
<tr>
<td> </td>
<td><input type="button" id="btn_submit" value="Submit" onclick="do_submit()" /></td>
</tr>
</table>
</form>
<br/>
<div id="div_result"><?=$all_feedbacks?></div>
<h1 id="introduction">Model: feedback_model.php</h1>
<p><?=$highlighted_model?></p>
<h1 id="introduction">Controller: feedback.php</h1>
<p><?=$highlighted_controller?></p>
<h1 id="introduction">View: feedback_tpl.php</h1>
<p><?=$highlighted_view?></p>