diasansley Posted April 20, 2011 Share Posted April 20, 2011 hey guys here is my code for the model view and the controller using CI version 2.0 + the controller sea_con.php <?php class Sea_con extends CI_Controller{ public function index(){ $this->search('standard'); } public function json(){ $this->search('json'); } public function search($transport = null){ //load database $this->load->database(); //load posts model $this->load->model('sea', 'posts'); //load JSON lib $this->load->library('JSON', 'json'); //init posts array $posts = array(); //validate and filter the input if(isset($_POST['searchterm']) && !empty($_POST['searchterm'])){ //clean the string, just in case $searchterm = strip_tags($_POST['searchterm']); //get the results if any $posts = $this->posts->search($searchterm); } //if request came from a JSON application if($transport == 'json'){ //output the encoded string echo $this->json->encode($posts); //and exit, no reason to continue exit; } //assign post array $this->data['posts'] = $posts; $this->data ; print_r($_POST); //display the view $this->load->view('livesearch',$posts); } } ?> ----------------------------------------------------------------------------- the view livesearch.php <html> <div id="form-container-wrapper"> <div id="form-container"> <form method="post" id="ls-form" action="<?php site_url('livesearch'); ?>"> <p> <label for="searchterm" class="heading">Start typing in the bessage box. Try <strong>CodeIgniter</strong>, for example.</label> </p> <p> <input type="text" name="searchterm" id="searchterm" value="" /> <input type="submit" name="Submit" id="submit" value="Submit" /> </p> </form> </div><!-- form-container --> </div><!-- form-container-wrapper --> <div id="search-results-wrapper"> <div id="search-results"> <?php if($posts): ?> <h2>Search results:</h2> <?php $count = 1; foreach($posts as $key => $post) : ?> <h3><?php echo $count; ?>. <?php echo $post['title']; ?></h3> <p><?php echo $post['body']; ?></p> <?php $count++; endforeach; ?> <?php else: ?> <h2>Search results:</h2> <p>Your query did not match any posts.</p> <?php endif; ?> </div><!-- end search-results --> </div><!-- end search-results-wrapper --> <!-- footer--> </html> ----------------------------------------------------------------------------------- the model sea.php <?php CLASS Sea EXTENDS CI_Model{ PUBLIC FUNCTION search($term) { $term = $this->db->escape($term); $sql = "select * from posts where title like $term "; $res = $this->db->query($sql); $items = ARRAY(); IF ($res->num_rows() > 0) { $count = 0; FOREACH ($res->result() AS $item) { FOREACH ($item AS $key => $value) { $items[$count][$key] = $value; } $count++; } } RETURN $items; } } ?> ----------------------------------------------------------------------- i get a variable posts undefined in my view error also i get the post data but my ajax functionality dosent seems to work. Thanks Link to comment https://forums.phpfreaks.com/topic/234213-cant-get-ajax-to-work/ Share on other sites More sharing options...
ram4nd Posted April 20, 2011 Share Posted April 20, 2011 Now you might get some actual results: $sql = "select * from posts where title like '%$term%'"; Link to comment https://forums.phpfreaks.com/topic/234213-cant-get-ajax-to-work/#findComment-1203899 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.