0

Hello,

I've just started learning codeigniter and struggling with ajax calls.

Found a few tutorials but can't understand how to do what I want.

I have a home_view with links, and when I click on a link I want it to fetch data from the database with the given id.

Then display a list of the records recieved from the database.

For example:

If the user clicks the Bread Rolls link, it should find all the records that belong to Bread Rolls in the products table and display them in the productsList div, in the categories_view file.

The jquery code is in the header_view file.

I am attaching my view, controller and model files.

Can someone please help?

Attachments
<div id="category">
	<ul>
<?php
	foreach($cats as $cat)
	{
		echo '<li><a href="#" id="' . $cat['id'] . '" class="cat">' . $cat['name'] . "</a></li>";
	}
?>
	</ul>
</div>
<div class="clear"></div>
<div id="productList"></div><!-- Will be filled with an ajax call -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The Millery</title>
<?php $this->load->helper('url'); ?>
<link href="<?php echo base_url(); ?>css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
	$('.cat').click(function() {
		var catId = $(this).attr('id');
	
		if (catId != ""){
        $.get('/index.php/home/productsList'+catId, function(data){
            $(this).parents('div').append(data);
        })
    }//end if

	
		
 });
});
</script>
</head>
<body>
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class Home extends CI_Controller
{
 function __construct()
 {
   parent::__construct();
 }

 function index()
 {
   if($this->session->userdata('logged_in'))
   {
			$this->load->model('m_categories');
			$this->load->model('m_products');
     	$session_data = $this->session->userdata('logged_in');
     	$data['username'] = $session_data['username'];
		 
		 	$this->load->view('header_view');
     	$this->load->view('home_view', $data);
		 	
			$this->getCategories();
   }
   else
   {
     //If no session, redirect to login page
     redirect('login', 'refresh');
   }
 }

 function logout()
 {
   $this->session->unset_userdata('logged_in');
   session_destroy();
   redirect('home', 'refresh');
 }
 
	function getCategories()
 	{
		$data['cats'] = array();
 		$result = $this->m_categories->getCategories();
	 
		if($result)
		{
     	foreach($result as $row)
			{
				$id = $row->id;
				$name = $row->name;
				
				array_push($data['cats'], array('id'=>$id, 'name'=>$name));
			}
		
			$this->load->view('categories_view', $data);
		}
 }
 
 function getProducts($catId)
 {
	 	$data['products'] = array();
	 	$result = $this->m_products->getProducts($catId);
	 
		if($result)
		{
     	foreach($result as $row)
			{
				$id = $row->id;
				$name = $row->name;
				
				array_push($data['products'], array('id'=>$id, 'name'=>$name));
			}
		}
		$this->load->view('productList_view',$data);
 }
 
 function productsList()
 {
    $this->load->model('m_products','', TRUE);
    $data['products'] = $this->m_products->getProducts($this->params['catId']);
    $this->load->view('productList', $data);
}

}
<h1>Home</h1>
<h2>Welcome <?php echo $username; ?>!</h2>
<a href="home/logout">Logout</a>
<div id="content">
</div>
<?php
Class M_Products extends CI_Model
{
 function getProducts($catId)
 {
   	$this -> db -> select('*');
   	$this -> db -> from('products');
		$this -> db -> where('catId = $catId');
		
		echo "Here $catId<br />";

   	$query = $this -> db -> get();

   	if($query -> num_rows() >= 1)
   	{
     	return $query->result();
   	}
   	else
   	{
   	  return false;
   	}
 	}
}
4
Contributors
7
Replies
15
Views
5 Years
Discussion Span
Last Post by cereal
0

This is old but it still can be useful:

in line 15 of header_view.php file add a slash to the url:

'/index.php/home/productsList/'+catId

in line 78 of home.php (the controller) there is a mistake, where did you get $this->params? Refer to the URI Class, you should solve:

$data['products'] = $this->m_products->getProducts($this->uri->segment(3));

bye.

0

Thanks, but yes, this is already solved. :) Thank you very much for taking the time to answer.

0

Thanks, but yes, this is already solved.

Will you mark it solved then please (and perhaps include you solution to help others).

Edited by pritaeas: n/a

1

My solution to this question:
In the jquery script I am getting the category id from the link and calling my controller's function that calls the model to get the products. Then displays the result in a div with id productsList.

$('.cat').click(function(e) {
		e.preventDefault(); 
		var id = $(this).attr('id');
		$("#productList").show();
		
		$.ajax({
			 type: "POST",
			 url: "/orders/index.php/processOrder/getProducts",
			 data: {catId: id},
			 success: function(data){
	  	 $("#productList").html(data);
	 		}
			});
	});
Votes + Comments
Thanks for sharing
0

I m beginner in codeignitor.my problem is when i fetch data from database all other data has fetched but image are not displayed.please proveide me code for how to fetch image from database.

model file code

<?php
    class Car_model extends CI_Model
    {
        function getAllRecords()
        {
            $this->load->database();
            $q = $this->db->get('cardetails');
            if($q->num_rows() > 0)
            {
                return $q->result();
            }
            return array();
        }
    }
?>

controller file code

<?php 
class Search extends CI_Controller {
    function searchname()
{
    $this->load->model("car_model");
    $data['records'] = $this->car_model->getAllRecords();
        $this->load->view('header_view');
        $this->load->view('search',$data);
        $this->load->view('footer_view');
}

}
?>

view file code

<?php
        foreach($records as $row):
        ?>
 <tr>
 <td><img height="100" width="200" src="http://localhost/ci_user/images.'<?php=$row->mco_carimage?>.'
 "/></td>
       <td align="center"><?=$row->mco_carname ?></td>
        <td align="center"><?=$row->mco_totalkm ?></td>
        <td align="center"><?=$row->mco_price ?> Rs</td>
        <td align="center"><?=$row->mco_insurance ?></td>
        <td align="center"><?=$row->mco_fualtype ?></td>
</tr> 
  <?php

        endforeach;
    ?>
0

@sopu

There are two small errors here:

<img height="100" width="200" src="http://localhost/ci_user/images.'<?php=$row->mco_carimage?>.'

"/>

Remove the single quotes around the PHP statement '<?php ... ?>' and add a slash / to the path, right after the directory name images:

http://localhost/ci_user/images/ 

The ending code would be:

<img height="100" width="200" src="http://localhost/ci_user/images/<?php echo $row->mco_carimage; ?>" />

This works if mco_carimage will print the filename and the extension of the file. And please, next time open a new thread, bye.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.