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?

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.

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

No problem, bye :)

Thanks, but yes, this is already solved.

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

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);
	 		}
			});
	});
commented: Thanks for sharing +14

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;
    ?>

@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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.