I am new user of CodeIgniter and I am trying to build an application that there are lots of jQuery dynamical content.
Below I provide a code that I am using in order to be precise. The code below is running and I cannot see any problem. However, since I will be using pieces of code like this in several different parts of my application, I would like to know if this is a good design. If it is not, please let me know how to proceed.
The code below basically describe a search form in which the results are dynamically included.
This is my view:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("form#searchForm").submit(function() {
var theCity = $("select#chooseCity").val();
$.post("welcome/searchPeopleResults/", {theCity: theCity}, function(data) {
$("div#searchResults").html(data);
});
return false
});
});
</script>
</head>
<body>
<FORM id="searchForm">
<h2>Selecione uma cidade: </h2>
<select id="chooseCity">
<?php
$theCitiesOptionsHTML = "cityOptions.html"; <!-- A large list of cities -->
require($thePathDataFiles.$theCitiesOptionsHTML);
?>
</select>
</FORM>
<div id="searchResults">
<!-- Search results should arise here -->
</div>
</body>
</html>
This is my controller:
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('searchPeople');
$this->load->view('css/format');
}
public function searchPeopleResults(){
$theCity=$this->input->post('theCity');
$this->load->model('MSearchPeople');
$data=$this->MSearchPeople->provideSearchPeopleResults($theCity);
echo $data;
}
}
This is my model:
Class MSearchPeople extends CI_Model {
function provideSearchPeopleResults($theCity) {
// Here I got my database query ($theUsername, $theFirstName, $theLastName, $theSummary) and call the function presented below "writeHtmlSearchPeople".
return $theHtml;
}
}
Note that that after querying the database, the model above generates the necessary pieces of HTML code to be returned to the scope of the $.ṕost()
function.
Precisely, inside the function provideSearchPeopleResults($theCity)
, I call another function that generates the html code for me. Something like that:
<?php
function writeHtmlSearchPeople($theUsername, $theFirstName, $theLastName, $theSummary) {
$theCompleteName = $theFirstName . " " . $theLastName;
$theFigureName = base_url() . "application/views/photos/" . $theUsername;
?>
<table border = "0" width = "75%">
<tr>
<td width = "20%">
<img src="<?php echo $theFigureName ?>" alt=" " width= "150">
</td>
<td width = "80%">
<a class="supplierPage" data-username="<?php echo $theUsername ?>" href = "" > "<?php echo $theCompleteName ?>"</a> "<?php echo $theSummary ?>"
</td>
</tr>
</table>
<?php
}
?>
I am particularly worried with the fact that I am generating HTML code from my model (note that this HTML code is just being returned as data to the scope of the $post()
function). However, I am not sure if there is a better practice for this!
Furthermore, I would like to implement explicitly the MVC design. Although views and models are in different files, they are not connected by the controller.