Jump to content

Recommended Posts

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.