Facebook  Twitter  Google +  Linkedin

Codeigniter4 Howto fetch data by ID with button click from database using JQuery Ajax

Learning has never stopped. That is why I wanted to share this little piece of code if it could be helpful to another codeigniter lover or developer.

Let's say you want to display products from their particular categories.

So it will start with a point where you have created buttons that show names of categories and every time a user clicks on a particular category button, products will display from it.

 Categories

Model

//show all categories
public function getAll()
{
  $builder = $this->db->table('product_category');
  $results = $builder->get()->getResult();
  return $results;
}

Controller

  
public function index()
{
  //show category and products
  $data['productCategory'] = $productCategoryModel->getAll();
  return view('products/all', $this->data);
}

Views

NOTE: This view file all.php is the same to display also products but in a separate div with id=”products”. Failed to get code for view file here. so i have provided a screenshot

 view file code

PRODUCTS

Model

//Get products in a particular category
    public function getCatProducts($id)
    {
        $builder = $this->db->table('product');
        $builder->where('category_id', $id);
        $builder->join('product_category', 'product.category_id=product_category.cat_id');
        $results = $builder->get()->getResult();
        return $results;

    }

Controller

public function action()
	{
        if($this->request->getVar('action'))
		{
			$action = $this->request->getVar('action');
            if($action == 'get_products')
			{
                $productModel = new ProductModel();
				$myproducts = $productModel->getCatProducts($this->request->getVar('cat_id'));

				echo json_encode($myproducts);                
			}
        }
    }

NOTE: “all.php” code is already up. Just ensure you have the same controller file to handle categories and products queries.
The last piece of code needed is this for JQUERY AJAX

JQUERY AJAX (in all.php) view file.




    function showproducts(cat_id)
    {
        //console.log(cat_id);
        $.ajax({
                method:"GET",
                url:""+cat_id,
                dataType:"json",
                processData: false,
                contentType: false,
                cache: false,
                headers: {'X-Requested-With': 'XMLHttpRequest'},
                success:function(data)
				{
                    //console.log(data);
                    //alert(data);
                    var html = '
Products
';
					
                    for(var count = 0; count < data.length; count++)
                    {
                        html += '

'+data[count].product_name+'

';
                    }
			
                    $('#products').html(html);
                },
				error: function(xhr, status, error){
				var errorMessage = xhr.status + ': ' + xhr.statusText
				alert('Error - ' + error);
				}
            });
    }

 

 

That should be able to help display your product with button click into another defined Div.

NOTE:
Ensure jquery is loaded

Enjoy codeigniter

Error::

The IP xxx.xxx.xx.xxx is exposing a Open Memcached Server service to the Internet. Such configuration could lead to Open Memcached Server amplification attacks.

There are two quick solutions to this problem and that is either configuring your Memcached to listen to localhost or completely stopping it.

Option 1: Configure Memcached Server listen to localhost

When running Zimbra mail server, memcached is a very important module because it helps proxy service achieve caching of upstream routes to mailstores on a per end-client basis - significantly reduces the route lookup time thus improving the total time required to process the request and boost performance.

Since now Memcached is necessary, let's configure our server memcached daemon to listen through localhost / 127.0.0.1 IP Address instead of public.

Proxy Server not running on Zimbra Mail Server.

Common error

Starting proxy...failed.
nginx start failed. reason: No available memcached servers could be contacted

NOTE: proxy server tends to stop working after configuration and restart of Memcached. Therefore run the following command

Solution

[zimbra@server1 ~]$   zmprov ms `zmhostname` +zimbraServiceEnabled memcached

 

[zimbra@server1 ~]$   zmproxyctl restart

 

Howto remove .php, .html extensions on a web page using .htaccess file

Many times you will want to have user-friendly URLs on your site. Instead of https://www.mydomain.com/index.html you will want to have https://www.mydomain.com/index . The second URL looks much better. Also, from the SEO point of view it is better to don’t use file extensions. For my tutorial purpose I am using a domain url https://www.bbscargoltd.comt/about.php which in conclusion i will be able to access it via https://www.bbscargoltd.com/about 

Most of the times when uploading a bigger image file or a publication into a wordpress or Joomla or Drupal website, you get an error like “this file or image exceeds the maximum upload size for this website “

How to Increase the Max Upload Size Limit
Below are some of ways to increase your Max Upload Size to MB of your choice.