I found this type of error:
Type: ArgumentCountError
Message: Too few arguments to function Shoppycart::add_to_cart(), 0
passed in C:\xampp\htdocs\webmodify\system\core\CodeIgniter.php
on line 533 and exactly 1 expectedFilename:
C:\xampp\htdocs\webmodify\application\controllers\Shoppycart.phpLine Number: 18
Backtrace:
File: C:\xampp\htdocs\webmodify\index.php Line: 315 Function:
require_once
Shoppycart.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Shoppycart extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Cart_model');
}
public function index(){
redirect('test');
}
// Add an item to the cart
public function add_to_cart($product_id) {
// Fetch product details from the database using the $product_id
$product = $this->Cart_model->get_product_by_id($product_id);
if ($product) {
// Prepare cart item data
$cart_item = array(
'id' => $product->id,
'qty' => 1, // You can adjust the quantity as needed
'price' => $product->price,
'name' => $product->name,
'options' => array('image' => $product->image_url)
);
// Add the item to the cart
$this->cart->insert($cart_item);
// Redirect to the cart page or a success message
// redirect('Shoppycart/view_cart');
// redirect(base_url()."Shoppycart/view_cart/(:any)");
redirect('Shoppycart/view_cart');
} else {
// Handle the case when the product is not found
echo "Product not found!";
}
}
public function view_cart(){
$this->load->view('product/cart_view');
}
Cart_model.php
<?php
class Cart_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->database();
}
// Create a new cart item
public function get_product_by_id($product_id) {
$this->db->where('id', $product_id);
return $this->db->get('products')->row();
}
cart_view.php
<html>
<head>
<title>My Form</title>
</head>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/alpine.min.js" defer></script>
<body>
<?php echo form_open('Shoppycart/add_to_cart/'); ?>
<div class="w-full overflow-x-auto">
<table class="min-w-full">
<thead>
<tr>
<th class="py-2 px-4">QTY</th>
<th class="py-2 px-4">Item Description</th>
<th class="py-2 px-4 text-right">Item Price</th>
<th class="py-2 px-4 text-right">Sub-Total</th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
<?php foreach ($this->cart->contents() as $items): ?>
<?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>
<tr>
<td class="py-2 px-4">
<?php echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5', 'class' => 'w-full px-2 py-1 border rounded')); ?>
</td>
<td class="py-2 px-4">
<div>
<img class="object-scale-down h-48 w-96" src="<?php echo base_url($items['options']['image']); ?>" alt="<?php echo $items['name']; ?>">
</div>
<?php if ($this->cart->has_options($items['rowid']) == TRUE): ?>
<p>
<?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>
<strong><?php echo $option_name; ?>:</strong> <?php echo $option_value; ?><br />
<?php endforeach; ?>
</p>
<?php endif; ?>
</td>
<td class="py-2 px-4 text-right"><?php echo $this->cart->format_number($items['price']); ?></td>
<td class="py-2 px-4 text-right">$<?php echo $this->cart->format_number($items['subtotal']); ?></td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
<tr>
<td colspan="2"></td>
<td class="py-2 px-4 text-right"><strong>Total</strong></td>
<td class="py-2 px-4 text-right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
</tbody>
</table>
<p class="text-center">
<a href="<?php echo site_url('Shoppycart/add_to_cart/' . $items['id']); ?>">Add to Cart</a>
</p>
</div>
</body>
</html>
routes.php
$route['default_controller'] = 'Shoppycart';
$route['Shoppycart/add_to_cart/(:num)'] = 'Shoppycart/add_to_cart/$1';
Retrieve and display cart items and also add an item to the cart
Is this happening after you click the “Add to cart” link? What does the URL in the browser look like after you click that link? What was in the
<a href
link – did you check the rendered HTML source code? Did it contain a valid URL with an ID? It’s not clear what, if any, debugging you’ve done. All we have is some source code and an error message. There’s not really even an explanation of what events led to the error – hence my question. See also How to AskP.S. As an aside, why do you have
<?php echo form_open('Shoppycart/add_to_cart/'); ?>
? You don’t seem to need or be using a form here, and I don’t see anywhere that you closed the form either. This line is probably unnecessary.When I went to this URL: localhost/webmodify/Shoppycart/add_to_cart I found this type of error: An uncaught Exception was encountered Type: ArgumentCountError Message: Too few arguments to function Shoppycart::add_to_cart(), 0 passed in C:\xampp\htdocs\webmodify\system\core\CodeIgniter.php on line 533 and exactly 1 expected Filename: C:\xampp\htdocs\webmodify\application\controllers\Shoppycart.php