Adding products to my cart is working fine, but after adding product to cart its not able to view it in my view page without refreshing view page,
I tried redirect() method still no use.
How do i view my page after adding products to cart.
Any help??
my controller:
<?php
class Cart extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function addtocart($cal_id, $f_id) {
if (empty($cal_id)) {
$this->load->view('Cart_view');
}
$this->load->model('cart_model');
$product = $this->cart_model->cart_contents($cal_id, $f_id);
foreach ($product as $row) {
$name = $row->title;
$fees = $row->fees;
}
$product = array(
'id' => $cal_id,
'qty' => 1,
'price' => $fees,
'name' => $name,
);
print_r($product);
$this->cart->insert($product);
$this->load->view('Cart_view');
}
public function destroy() {
$this->cart->destroy();
}
}
?>
my model:
<?php
class cart_model extends CI_Controller
{
public function __construct() {
parent::__construct();
}
public function cart_contents($cal_id,$f_id)
{
$product=$this->db->select('*')
->from('calander')
->join('fees','calander.cal_id=fees.cal_id')
->where('fees.cal_id',$cal_id)
->where('fees.f_id',$f_id)
->get();
return $product->result();
}
}
?>
my view
<html>
<head><title></title></head>
<body>
<?php
if ($cart = $this->cart->contents()) {
?><table>
<tr>
<td>
<h3><u>Cart page</u></h3>
</td>
</tr>
<tr>
<th>course</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<?php
foreach ($cart as $contents) {
?><tr>
<td><?php echo $contents['name']; ?></td>
<td><input type="text" value="<?php echo $contents['qty']; ?>"></td>
<td><?php echo $contents['price']; ?></td>
</tr>
<?php } ?>
</table> <?php
// redirect(base_url('/index.php/Cart/Cart_view'));
}
?>
</body>
</html>
I dont understand what you are trying to do. When you load your view $this->load->view('Cart_view');, it is required to pass the data array, containing all the variables into the view like this $this->load->view('Cart_view',$data); . Without data array, view cannot display data. When ever you write code atleast, make variables names in such a way that other people will be able to understand what that variable stands for. Without enough data, i am taking the liberty of modifying your code
Controller
public function addtocart($cal_id, $f_id) {
$data=array(
'view'=>''
);
if (empty($cal_id)) {
$this->load->view('Cart_view',$data);
} else {
$this->load->model('cart_model');
$product = $this->cart_model->cart_contents($cal_id, $f_id);
$data['view']='';
foreach($product as $row):
$data['view'].=' <tr>
<td>'.$row->title.'</td>
<td>'.$row->qty.'</td>
<td>'.$row->price.'</td>
</tr>';
endforeach;
$this->load->view('Cart_view',$data);
}
}
MODEL
public function cart_contents($cal_id,$f_id){
$this->db->select('*')
$this->db->from('calander')
$this->db->join('fees','calander.cal_id=fees.cal_id')
$this->db->where('fees.cal_id',$cal_id)
$this->db->where('fees.f_id',$f_id);
$query = $this->db->get();
return $query->result();
}
view
<html>
<head><title></title></head>
<body>
<table>
<tr>
<td>
<h3><u>Cart page</u></h3>
</td>
</tr>
<tr>
<th>course</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<?php if($count>0){
echo $view;
}?>
</table>
</body>
</html>
Related
I am unable to explode comma separated data array from the database in codeigniter framework. I want to echo the array into multiple rows with string value like:
---------------
Product Name
---------------
Product 1
Product 2
Product 3
But i am getting array to string conversion error.
<?php foreach ($res as $key => $value) { ?>
<tr class="border-bottom">
<td>
<?php $prodArray = $value->product;
echo explode(',',$prodArray) ?>
</td>
</tr>
<?php } ?>
So, how can i explode and fetch the data in codeigniter
VIEW FILE
<table>
<th>
<td>S. NO</td>
<td>Product Name</td>
</th>
<tbody>
<?php
$no = 1;
foreach($products as $product){
?>
<tr>
<td><?php echo $no;?></td>
<td><?php echo $product->product_name;?></td> //update with your column name
</tr>
<?php
$no++;}
?>
</tbody>
</table>
MODEL FILE
public function get_products()
{
$this->db->select('*');
$this->db->from('products_table'); //update with your table name
return $this->db->get()->result_object(); // sucess result or handle exceptiom here
}
CONTROLLER FILE
public function product()
{
$this->load->model('product'); // can be loaded in the parent::__construct(); at the begining of the controller
$this->products = $this->product->get_products();
$this->load->view('products_view');
}
<?php foreach ($res as $key => $value) {
$prodArray = $value->product;
if($prodArray){ foreach($prodArray as $product) {
?>
<tr class="border-bottom">
<td>
<?php echo $product; ?>
</td>
</tr>
<?php } } } ?>
I have made an admin page and user page, and i want to show the list of users who have registered in database when the admin logs in.
For that i've created a model as follows,
public function regi_users(){
$q = $this->db->query("SELECT username FROM public");
return $q;
}
and this i am accessing through a view i have created, to which, when admin logs in, he is redirected, as follows,
account.php
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
$this->load->model('loginmodel');
$qresult = $this->loginmodel->regi_user();
foreach ($qresult as $row) {
echo $row->username;
}
?>
</body>
but when my admin logs in, he's shown the following error,
Fatal error: Call to undefined method LoginModel::regi_user() in
E:\wamp64\www\ci\application\controllers\account.php on line 11
what am i doing wrong here? I apologize if it is a silly question but i am kind of new to php
Thank you for your suggestions
Controller
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('loginmodel');
}
public function FunctionName($value='')
{
$this->data["users"] = $this->loginmodel->regi_user();
$this->load->view('account',$this->data);
}
}
Model
class Loginmodel extends CI_Model{
function __construct() {
parent::__construct();
}
public function regi_user() {
$query = $this->db->get('table_name')->result();
return $query;
}
}
View
<table class="table">
<thead>
<tr>
<th>Firstname</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $row) { ?>
<tr>
<td><?php echo $row->username; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Your query should be like this
public function regi_users(){
return $this->db->select('username')->from('table_name')->get()->result_array();
}
controller
class User extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('YourModelName');
}
public function fetchUser()
{
$data['user']=$this->YourModelName->fetchUsers();
$this->load->view('yourViewClass',$data);
}
}
Model
class YourModelName extends CI_Model
{
function __construct()
{
$this->load->database();
}
public function fetchUsers()
{
$query = $this->db->select('*')->from('table_name');
$query=$this->db->get();
if($query->num_rows()>0)
{
$result=$query->result();
return $result;
}else{
return 0;
}
}
}
view
<table>
<thead>
<tr>
<th>Firstname</th>
</tr>
</thead>
<tbody>
<?php foreach ($user as $row) { ?>
<tr>
<td><?php echo $row->username; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Model name should be LoginModel.php
I am using Codeigniter 3 and trying CRUD operation. I have created the basic crud operation and am showing the data in a table however I have linked a paragraph tag in the controller below the table to the form controller, If I want to enter another data
The issue is when I click on the link to enter another data it redirect me the original form in controller but when I enter the data and submit it, The data is shown below the table in the paragraph tag.
I am not able understand why this is happening as the controller is the same
I had faced a similar issue before when redirecting in controller.I had redirected the page after submission to show_form() controller which was basically redirecting the page to $this->load->view('learn/view_form');
in which I have kept a condition that if No data is present click to enter. Now when it redirects to show_form() controller it goes into else condition even if the data is present
CONTROLLER
<?php
defined('BASEPATH') OR exit("No direct script access allowed");
class Learning extends CI_Controller{
public function __construct(){
parent::__construct();
$this ->load->helper("url");
$this->load->model("tatti_test");
$this->load->database();
$this->load->helper();
}
//functions should be passed here
//creating a function
function start_learn() {
//this varible
$this->load->view('learn/start_learn');
}
function start_crud(){
$this->load->view('learn/form');
}
function show_form(){
$this->load->view("learn/view_form");
}
function insert_form(){
$name = $this->input->post("u_name");
$email = $this->input->post("u_email");
$mobile = $this->input->post("u_mobile");
//File Uploading
$config['upload_path']="./assets/images/";
$config["allowed_types"]="gif|jpg|png";
$config['encrypt_name']=true;
$this->load->library("upload",$config);
if(!$this->upload->do_upload("u_file")){
$file='noimage.png';
}
else {
$filework = $this->upload->data();
$file =$filework['file_name'];
}
$data = array(
"name"=>$name,"email"=>$email,"mobile"=>$mobile,"file_name"=>$file
);
$this->tatti_test->insert_tatti($data);
redirect("learning/view_form");
}
function view_form(){
$data['returned_data']=$this->tatti_test->show_form();
$this->load->view("learn/view_form",$data);
}
function delete_entry(){
$id=$this->uri->segment(3);
$data=$this->tatti_test->for_unlink($id);
$filepath="./assets/images/".$data['file_name'];
unlink($filepath);
$this->tatti_test->delete_entry($id);
redirect('learning/view_form');
}
function time_to_update(){
$id=$this->uri->segment(3);
$data['fetched_update_entry']=$this->tatti_test->update_entry($id);
$this->load->view("learn/update.php",$data); //bus associative array hi leta hai
}
function up_db(){
$name =$this->input->post('up_name');
$email = $this->input->post('up_email');
$mobile = $this->input->post('up_mobile');
$file = $this->input->post('up_file');
$id = $this->input->post('up_id');
//File Uploading
$config['upload_path']="./assets/images/";
$config["allowed_types"]="gif|jpg|png";
$config['encrypt_name']=true;
$this->load->library("upload",$config);
if(!$this->upload->do_upload("up_file")){
$data= $this->tatti_test->remove_prev($id);
$file=$data['file_name'];
}
else {
$data= $this->tatti_test->remove_prev($id);
$path="./assets/images/".$data['file_name'];
unlink($path);
$filework = $this->upload->data();
$file =$filework['file_name'];
}
$data = array(
"name"=>$name,"email"=>$email,"mobile"=>$mobile,"file_name"=>$file
);
$this->tatti_test->up_nw($data,$id);
redirect('learning/view_form');
}
} /*this accesses command from main ci controller */
?>
VIEW
<?php $this->load->view("common/header.php");
if ($returned_data != 0){ ?>
<table border='1'>
<tr>
<th>Sr No</th>
<th>Name</th>
<th>Password</th>
<th>Mobile</th>
<th>Email</th>
<th>Final Name</th>
<th>Delete</th>
<th>View</th>
</tr>
<?php $i=0; foreach ($returned_data as $key=>$d){
?>
<tr>
<td>
<?php echo ++$i; ?>
</td>
<td>
<?php echo $d['name'];?>
</td>
<td>
<?php echo $d['mobile'];?>
</td>
<td>
<?php echo $d['email'];?>
</td>
<td>
<?php echo $d['file_name'];?>
</td>
<td>
<img src="<?php echo base_url().'/assets/images/'.$d['file_name'];?>" width="100px" ; height="100px" />
</td>
<td>Edit</td>
<td>Delete</td>
</tr>
</table>
<p>Add another entry
<?php echo anchor("learning/start_crud"," here "); ?>
</p>
<?php } ?>
<?php } else { ?>
<p>No data to show please click
<?php echo anchor("learning/start_crud"," here "); ?>to enter</p>
<?php } ?>
<?php $this->load->view("common/footer.php");
MODEL
<?php
class Tatti_test extends CI_Model{
function insert_tatti($insert_data){
$this->db->insert("f_form",$insert_data);
}
function show_form(){
$query = $this->db->get("f_form");
$response=[];
if ($query->num_rows() > 0){
$response = $query->result_array();
}
else {
$response = 0;
}
return $response;
}
function for_unlink($id){
$this->db->where("id",$id);
$query = $this->db->get("f_form");
$response=[];
foreach ($query->result_array() as $rows){
return $response = $rows;
}
}
function delete_entry($id){
$this->db->where("id",$id);
$this->db->delete("f_form");
}
function update_entry($id){
$this->db->where("id",$id);
$query = $this->db->get("f_form");
$response = [];
if($query->num_rows() > 0 ){
foreach($query->result_array() as $rows);
$response = $rows;
}
return $response;
}
function up_nw($introduced_data,$id){
$this->db->set($introduced_data);
$this->db->where('id',$id);
$this->db->update('f_form');
}
function remove_prev($id){
$this->db->where('id',$id);
$query = $this->db->get('f_form');
$response = [];
foreach($query->result_array() as $rows){
$response=$rows;
}
return $response;
}
}
?>
This is how the data is showing when clicked on the link below table
enter image description here
You're html formatting is messed up. You should have the closing </table> outside your foreach loop or premature <table> closure.
Also moved the Add another entry link outside the foreach loop. So it only appears once, and your document format not messed up.
You can use this fixed view instead:
<?php $this->load->view("common/header.php");
if ($returned_data != 0){ ?>
<table border='1'>
<tr>
<th>Sr No</th>
<th>Name</th>
<th>Password</th>
<th>Mobile</th>
<th>Email</th>
<th>Final Name</th>
<th>Delete</th>
<th>View</th>
</tr>
<?php $i=0; foreach ($returned_data as $key=>$d){
?>
<tr>
<td>
<?php echo ++$i; ?>
</td>
<td>
<?php echo $d['name'];?>
</td>
<td>
<?php echo $d['mobile'];?>
</td>
<td>
<?php echo $d['email'];?>
</td>
<td>
<?php echo $d['file_name'];?>
</td>
<td>
<img src="<?php echo base_url().'/assets/images/'.$d['file_name'];?>" width="100px" ; height="100px" />
</td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php } ?>
</table>
<p>Add another entry
<?php echo anchor("learning/start_crud"," here "); ?>
</p>
<?php } else { ?>
<p>No data to show please click
<?php echo anchor("learning/start_crud"," here "); ?>to enter</p>
<?php } ?>
<?php $this->load->view("common/footer.php");
I realize that you are trying to do CURD,
first of all, try this to improve your code and fill the missing library of codeigniter: https://github.com/avenirer/CodeIgniter-MY_Model
For your code:
No data passed to the view in show_form(),
You should check the form submission and the conditions to load the view,
simple thing to do is follow the best practice using ready scripts,
Hope this will be useful,
I am new in codeigniter.In my view page I am showing the data from database in a table i have two buttons in table to edit and delete the row..I want to delete a specific row from database through id.
## view
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>last name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php for ($i = 0; $i < count($records); ++$i) { ?>
<tr>
<td><?php echo ($i+1); ?></td>
<td><?php echo $records[$i]->fname; ?></td>
<td><?php echo $records[$i]->lname; ?></td>
<td><?php echo $records[$i]->email; ?></td>
<td><button name="edit">Edit</button></td>
<td><button name="delete">Delete</button></td>
</tr>
<?php } ?>
</tbody>
----------
## Controller ##
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message',$data);
}
public function register()
{
$this->load->view("register");
}
public function receive()
{
$fname= $this->input->post("fname");
$this->load->model('Model');
$this->Model->insertdata($fname);
// echo $this->input->post("fname");
// echo $this->input->post("lname");
// echo $this->input->post("password");
// echo $this->input->post("Email");
}
public function all_user(){
$this->load->model('Model');
$data['records'] = $this->Model->get_all_users();
$this->load->view("store_data",$data);
}
public function row_delete(){
$this->load->model('Model');
$this->mod1->row_delete($id);
redirect($_SERVER['HTTP_REFERER']);
}
}
----------
## Model ##
class Model extends CI_Model{
public function insertdata($fname)
{
$data = array
('fname'=> $this->input->post("fname"),
'lname' => $this->input->post("lname"),
'email' => $this->input->post("email"),
'password' => $this->input->post("password"),
);
$this->db->insert('register',$data);
}
public function get_all_users()
{
$query= $this->db->get('register');
return $query->result();
}
public function delete_row()
{$this->db->where('id', $id);
$this->db->delete('');
}
}
Try this in ur code
//view
<tbody>
<?php for ($i = 0; $i < count($records); ++$i) { ?>
<tr>
<td><?php echo ($i+1); ?></td>
<td><?php echo $records[$i]->fname; ?></td>
<td><?php echo $records[$i]->lname; ?></td>
<td><?php echo $records[$i]->email; ?></td>
<td>Delete</td> <td>Edit</td>
</tr>
<?php } ?>
</tbody>
make sure u pass the record id to the view and base_url is configured on config file
//controller
public function row_delete(){
if(isset($_GET['id'])){
$id=$_GET['id'];
$this->load->model('Model');
$this->mod1->row_delete($id);
redirect($_SERVER['HTTP_REFERER']);}
}
//model
public function delete_row()
{
$this->db->where('id', $id);
$this->db->delete('register');
}
reference links here and here
Just pass the parameters to the where function and table name to delete function:
public function delete_row()
{
// Set where
$this->db->where('id', $id);
// Run query
return $this->db->delete('register');
}
Cheers
I am new to codeigniter. I followed a video tutorial and successfully created a login registration system. After registration or login the users reach a page called members.php.
members.php is a view.
my controller is main.php and model is model-users.php.
I want members.php to show/have content from the database so that the users can read it once the login or register?
I followed a few resources on the web that said not to call controller from view. How can I accomplice the above without doing so?
Thanks
I think the CodeIgniter documentation is actually very clear, and very easy to follow. Nevertheless, here's an example of how you might structure your app.
Controller:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Members extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('members_model');
}
function index() {
$data['members'] = $this->members_model->get_members();
$this->load->view('header', $data);
$this->load->view('members');
$this->load->view('footer');
}
}
Model:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Members_model extends CI_Model {
function get_members() {
$query = $this->db->get('members');
return $query->result_array();
}
}
View:
<?php
print_r($members);
To access the above page, you would visit http://yoursite.com/index.php/members.
You can use a .htaccess file to remove the index.php segment from the URI. The index() method is automatically called when you run a controller.
I don't have any example of the code, but I write one simple example of HTML when you can show name and surname of members.
Controller:
function members(){
$members = $this->model-user->get_list(); //array of members
$this->load->view('members.php', $members); // load members in view
}
View members.php
<html>
<body>
<table>
<tr>
<th>Name</th>
<th>Surname</th>
</tr>
<?php foreach($members as $m): ?>
<tr>
<tr><?php echo $m['name']; ?>
<tr><?php echo $m['surname']; ?>
</tr>
<?php endforeach; ?>
<table>
</body>
</html>
Model:
function get_list() {
// query in database
return $query = $this->db->get('members'); //table name members
}
This is the simple example in a short form. I hope that you will understand it.
I think i know what you mean, this is definitely not the right way to go about this but if you really must you could call a model from a view like this.
In the view
<div>
<?php
#You could load the model here or autoload it in config->autoload->models(i believe)
$data = $this->model_name->model_function();?>
foreach($data as $row):
echo '<pre>';
print_r($row);
echo '</pre>';
endforeach;
?>
</div>
here is the example of my codes:
views:
<?php foreach($ecms as $i){ ?>
<tr>
<td><?php echo $i->accnum; ?></td>
<td><?php echo $i->crdnum; ?></td>
<td><?php echo $i->fname; ?></td>
<td><?php echo $i->bday; ?></td>
</tr>
<?php } ?>
model:
function masterHide()
{
$sql =" select * from ecms";
$query = $this->db->query($sql);
return($query->num_rows() > 0) ? $query->result(): NULL;
}
controller:
function search() {
// $this->MasterListAccess();
$this->load->model('navi_model');
$query = $this->navi_model->masterHide();
//check results or returned value of model
//print_r($query);
$data['ecms'] = $query;
$data['main_content'] = 'search';
$this->load->view('includes/template',$data);
}
At first connect the database in your project file and connect the model page in your view page and try this code.
Go to the view page and try this code
View
20
21
<?php
$get_data=$this->MyModel->get_table_data();
?>
<table class="table table-bordered">
<thead>
<tr>
<th> Name </th>
<th> Email </th>
<th> Phone No </th>
</tr>
</thead>
<tbody>
<?php foreach($get_data as $val){ ?>
<tr>
<td><?php echo $val->name; ?></td>
<td><?php echo $val->email; ?></td>
<td><?php echo $val->phone; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Model
public function get_table_data(){ $qry=$this->db->select('*')->from('Table Name')->get()->result(); return $qry; }