Add rows to table after each search in Codeigniter - codeigniter

Here is my model invoice.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class invoice extends CI_Model{
function __construct(){
parent::__construct();
}
public function search($keyword){
$this->db->like('code',$keyword);
$query = $this->db->get('products');
return $query->result();
}
}
and my controller search.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class search extends CI_Controller{
function __construct(){
parent:: __construct();
$this->load->model('frontend/invoice');
$this->load->helper(array('form', 'url','html'));
}
public function index(){
$keyword = $this->input->post('keyword');
$data['results'] = $this->invoice->search($keyword);
$this->load->view('header');
$this->load->view('frontend/navafterlogin');
$this->load->view('frontend/dashboard');
$this->load->view('frontend/results_view',$data);
//$this->load->view('frontend/invoicetable');
$this->load->view('footer');
}
}
and a view to echo the searched result results_view.php
<table>
<?php
foreach($results as $row){ ?>
<tr>
<td><input type="text" value="<?php echo $row->name?>"/></td>
<td><input type="text" id="rate" value="<?php echo $row->perunitprice?>"/></td>
<td><input type="number" id="quantity"value="" placeholder="quantity"/></td>
<td><input type="number" id="result" value="" placeholder="total"/></td>
</tr>
<?php
}
?>
</table>
and my form from where I search dashboard.php
<div class="row">
<?php echo form_open('frontend/search');?>
<div class="col-sm-2">
<input type="text" class="form-control" name="keyword" placeholder="productcode">
</div>
<div class="col-sm-2">
<button class="btn btn-warning">Find</button>
</div>
</div>
What i want is every time when i perform search, i want to add new row on my table which is results_view in this case and echo out the results in the separate rows.Currently i can search only once and on the second search, the first result gets replaced by second one and I don't want it to happen

Did you tried using "Session", if did not, try this out in your controller index function :
public function index(){
$this->load->library('session');
$keyword = $this->input->post('keyword');
$result = $this->invoice->search($keyword);
$search = $this->session->userdata('search') ? (array)$this->session->userdata('search') : array();
array_push($search, $result);
$this->session->set_userdata('search', $search);
$data['results'] = $search;
$this->load->view('header');
$this->load->view('frontend/navafterlogin');
$this->load->view('frontend/dashboard');
$this->load->view('frontend/results_view',$data);
//$this->load->view('frontend/invoicetable');
$this->load->view('footer');
}

Related

token generate for forgot password

I create forgot password module.
MY Question: How to generate token for forgot password using codeigniter. i used codeignitor framework. below code
Controller:
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->model('user_model');
$this->load->library('session');
}
public function forgotpassword() {
$this->form_validation->set_rules('email', 'Email Address', 'required|valid_emails');
if ($this->form_validation->run() == FALSE) {
$data = array(
'page_title' => 'Forgot Password',
'page_name' => 'user/forgotpassword'
);
$this->load->view('user_template', $data);
} else {
$result = $this->user_model->forgotpassword($_POST);
if (!empty($result)) {
echo '<h1 class="text-center">Thank You</h1>';
}
}
}
}
Model:
class User_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function forgotpassword($data) {
return $this->db->get_where('user', array('email' => $data['email']))->row_array();
}
}
View:
<section class="container">
<section class="login-form">
<?php echo form_open('User/forgotpassword'); ?>
<section>
<h2><span style="color: red">For</span>The<span style="color: red">Love</span>Of<span style="color: red">Food</span>Trucks</h2>
</section>
<div class="text-danger">
<?php echo validation_errors('<li>', '</li>'); ?>
</div>
<input type="hidden" name="token" value=" <?php echo sha1(date("Y/m/d h:i:sa")); ?>">
<label>Email</label>
<input type="text" name="email" id="email" class="form-control" placeholder="Email Address" value="<?php echo set_value('email'); ?>"required=""/>
<br/>
<button type="submit" name="submit" class="btn btn-block btn-danger">Submit</button>
<?php echo form_close(); ?>
</section>
</section>
Please Help me..................................................................................................
There are many ways to generate a token using PHP here is one of them:
$forgotten_code = sha1(uniqid(rand()));
Please note that this is just the answer to your question on 'how to generate the code' but the process involving the recover of the password is more complicated then you might think.
I can advise you to use a class such as ion auth 2 which is written to be used with Codeigniter and you can findit here:
https://github.com/benedmunds/CodeIgniter-Ion-Auth
This class will cover all the user authentication, login, password recovery and much more:
You do not need to reinvent the weel
By the way you have a typo here:
'Email Address', 'required|valid_emails'
You need to correct it like this:
'Email Address', 'required|valid_email'

Ajax data retrieve from controller

Hi i have called the database value in codeigniter controller and computed the values of database and stored in one new variable. So how can i get both the database values and the new variable that I have created in the controller as I want to display all database value and the new computed value in view. Please suggest me.
My controller: welcome
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public $total;
public function __construct() {
parent::__construct();
$this->load->library('table');
$this->load->model('product_database');
}
public function index() {
$data['show_table'] = $this->view_table();
$this->load->view('welcome_message', $data);
}
public function view_table(){
$result = $this->product_database->show_all_data();
if ($result != false) {
return $result;
} else {
return 'Database is empty !';
}
}
public function AddtoCart(){
$id = $this->input->post('product_id');
$qty = $this->input->post('qty');
$this->db->where('id', $id); // Select where id matches the posted id
$query = $this->db->get('productlist', 1); // Select the products where a match is found and limit the query by 1
foreach ($query->result() as $row)
{
$data = array(
'id' => $id,
'qty' => $qty,
'price' => $row->price,
'name' => $row->name
);
$total=$qty*$row->price;
echo $total;
}
}
}
?>
my view: welcome_message
<!DOCTYPE HTML>
<html>
<head>
<title>Product list</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> </script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"> </script>
<script type="text/javascript">
$(document).ready(function() {
$("#add").click(function(event) {
event.preventDefault();
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>" + "index.php/Welcome/AddtoCart",
dataType: 'json',
data: {name: },
success: function(res) {
if (res)
{ alert();
// Show Entered Value
jQuery("#total").html(res.total);
}
}
});
});
});
</script>
</head>
<body>
<div class="container">
<div class="message">
<?php
if (isset($read_set_value)) {
echo $read_set_value;
}
if (isset($message_display)) {
echo $message_display;
}
?>
</div>
<div> <?php
if (isset($show_table)) {
echo "<div class='productlist'>";
if ($show_table == 'Database is empty !') {
echo $show_table;
} else {
echo '<h2>Product List</h2><br/><br/>';
?>
<div class="row">
<div class="col-sm-8">
<div class="well">
<div class="table-responsive">
<?php
echo "<table width='98%', >";
echo '<tr><th class="e_id">Id</th><th>ProductName</th> <th>Price</th> <tr/>';
$i=1;
foreach ($show_table as $value) {
?>
<tr class="well" >
<?php
echo "<td width='30%' height='27px'>" . $value->id . "</td>" . "<td width='70%' height='27px'>" . $value->name . "</td>" . "<td height='27px'>" . $value->price . "</td>";
?>
<?php echo form_open('/Welcome/AddtoCart'); ?>
<td><input type="number" name="qty" value="1" style="width:40px;" min="1" max="99"></td>
<input type="hidden" name="product_id" value="<?php echo $value->id ?>" />
<td><input type="submit" value="Add" id="add" width="100%"></td>
<td><input type="submit" value="Rmv" id="rmv" width="100%"></td></br>
<?php echo form_close(); ?>
</tr>
<?php
$i=$i+1;}
echo '</table>';
?>
</div>
</div>
</div>
<div class="col-sm-4">
<div class="well">
<h4>Cart</h4>
Total Rs:<input type="text" value="" id="total"></br><br>
<input type="submit" value="Checkout" id="chk_out">
</div>
</div>
</div>
<?php
}
echo "</div>";
}
?>
</div>
</div>
</body>
</html>
And my model
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Product_database extends CI_Model {
public function show_all_data() {
$this->db->select('*');
$this->db->from('productlist');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
}
}
?>
In place of echo $total you can put this code :
$total= array('total'=> $total);
$youarray=array_merge($data, $total);
echo json_econde($youarray,true);

Data insert codeignetter throwing error

PHP Error was encountered Severity: Notice Message: Undefined
property: Registerform::$db Filename: core/Model.php Line Number: 51
controller file:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Registerform::$db
Filename: core/Model.php
Line Number: 51
controller file:
<?php
class Registerform extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('Registerform_model');
}
public function index() {
//$this->load->database();
$this->load->helper("form");
$this->load->library("form_validation");
$this->form_validation->set_rules("first", "First Name","required");
$this->form_validation->set_rules("last", "Last Name","required");
$this->form_validation->set_rules("email", "Email Address","required|valid_email");
$this->form_validation->set_rules("password", "Password","required");
$this->form_validation->set_rules("phone", "Phone Number","required");
$this->form_validation->set_rules("city", "City","required");
$this->form_validation->set_rules("addrss", " Your Address","required");
if ($this->form_validation->run() == false) {
$this->load->view("registerform_view");
} else {
$first = $_POST["first"];
$last = $_POST["last"];
$email = $_POST["email"];
$password = $_POST["password"];
$phone = $_POST["phone"];
$city = $_POST["city"];
$addrss = $_POST["addrss"];
$data = array(
'first'=>$first,
'last'=>$last,
'email'=>$email,
'password'=>$password,
'phone'=>$phone,
'city'=>$city,
'addrss'=>$addrss
);
//$data="insert into address (first_name,last_name,email,password,phone,city,add) values('$first','$last','$email','$password','$phone','$city','$add')";
$gg = $this->Registerform_model->insert_address($data);
echo $gg;
$this->load->view("registerformsuccess");
}
}
}
?>
model file:
<?php
class Registerform_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function insert_address($data){
$this->db->insert("address", $data);
}
}
?>
view file:
<?php
echo validation_errors();
echo form_open("Registerform");
?>
<label for="first">First Name</label>
<input type="text" name="first" id="first"><br>
<label for="last">Last Name</label>
<input type="text" name="last" id="last"><br>
<label for="email">Email Address</label>
<input type="text" name="email" id="email"><br>
<label for="password">Password</label>
<input type="password" name="password" id="password"><br>
<label for="phone">Phone</label>
<input type="text" name="phone" id="phone"><br>
<label for="city">City Name</label>
<input type="text" name="city" id="city"><br>
<label for="addrss">Your Address</label>
<input type="text" name="addrss" id="addrss"><br>
<input type="submit" name="submit" id="submit" value="Submit">
<?php echo form_close(); ?>
<?php
class Registerform_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function insert_address($data){
$this->load->database();
$this->db->insert("address", $data);
}
}
?>
Its work for me now.
You have to load database.
Put this line in model file constructor function:
$this->load->database();

form_submit codeigniter not working

So I am having trouble with the form_submit in codeigniter. I have a controller named welcome with a method named email. Whenever I hit my submit button the page refreshes but then just appends ?firstname=&email=&message=&submit=Submit to my url but doesn't carry out the method. Any reason this may be happening?
below is my html code.
<form role="form">
<div class="form-group">
<label>Name</label>
<?php $this->load->helper("form"); ?>
<?php echo validation_errors('<p class = "error">'); ?>
<?php echo form_open('welcome/email');
$data = array('type'=>'text','class'=>'form-control', 'name'=>'firstname');
echo form_input($data);
?>
</div>
<div class="form-group">
<label>Email</label>
<?php
$data = array('type'=>'email','class'=>'form-control', 'name'=>'email');
echo form_input($data);
?>
</div>
<div class="form-group">
<label>Message</label>
<?php
$data= array('type'=>'text','name'=>'message','class'=>'form-control','rows'=>7);
echo form_textarea($data);
?>
</div>
<div class="pull-right">
<!--<button type="submit" class="btn btn-custom btn-lg" action="welcome/email">Submit</button> -->
<?php
echo form_submit('submit','Submit');
echo form_close();
?>
</div>
This is my controller
<?php /*if ( ! defined('BASEPATH')) exit('No direct script access allowed');*/
class Welcome extends CI_Controller {
function index(){
$this->load->view('index-sidebar');
}
function email(){
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email Address','required|valid_email');
$this->form_validation->set_rules('firstname', 'Name', 'required|min_length[2]|max_length[30]');
$this->form_validation->set_rules('message', 'Message', 'required|min_length[5]|max_length[200]');
if ($this->form_validation->run()==FALSE){
$this->load->view('message not sent');
}
else{
$this->load->library('email');
$this->email->from(set_value('email'),set_value('name'));
$this->email->to("mhansen1989#gmail.com");
$this->email->subject('tutoring');
$this->email->message(set_value('message'));
$this->email->send();
echo $this->email->print_debugger();
$this->load->view('success');
}
}
}
there is no action parameter in your form tag.how do you submit the form?
try this
<form action="<?= base_url().'welcome/email'?>" role="form" method="post">
so here
if ($this->form_validation->run()==FALSE){
$this->load->view('message not sent');
}
put in your correct view name where it says 'message not sent'
$this->load->helper("form");
load your helpers in the controller -- better yet do it config/autoload
and this
$data = array('type'=>'text','class'=>'form-control','name'=>'firstname');
echo form_input($data);
in the form you repeat $data over and over. technically it will work but its going to mess up at some point so make them different names
$first = array('type'=>'text','class'=>'form-control','name'=>'firstname');
echo form_input($first);
and you need something to show the values in the form again if the validation is false. check out http://www.codeigniter.com/user_guide/libraries/form_validation.html#repopulatingform

Error: Call to a member function getKeyName() - component for Joomla 2.5

I got a fatal error with my component. Let's say the component name is com_shirts and this is my code
my code in administrator/component/com_shirts/controllers/code.php
<?php
defined('_JEXEC') or die;
jimport('joomla.application.component.controllerform');
class ShirtsControllerCode extends JControllerForm{
protected $view_list = 'codes';
}
my code in administrator/component/com_shirts/controllers/codes.php
<?php
defined('_JEXEC') or die;
jimport('joomla.application.component.controlleradmin');
class ShirtsControllerCodes extends JControllerAdmin{
protected $text_prefix = 'COM_SHIRTS';
function getModel($name='Code', $prefix='ShirtsModel', $config=array('ignore_request'=>TRUE)){
$model = parent::getModel($name, $prefix, $config);
return $model;
}
}
my code in administrator/component/com_shirts/models/code.php
<?php
defined('_JEXEC') or die;
jimport('joomla.application.component.modeladmin');
class ShirtsModelCode extends JModelAdmin{
function getTable($type='Code', $prefix='ShirtsTable', $config=array()){
return JTable::getInstance($type, $prefix, $config);
}
function getForm($data=array(), $loadData=TRUE){
$form = $this->loadForm();
return $form;
}
}
my code in administrator/component/com_shirts/models/codes.php
<?php
defined('_JEXEC') or die;
jimport('joomla.application.component.modellist');
class ShirtsModelCodes extends JModelList{
function getItems(){
$items = parent::getItems();
foreach($items as &$item){
$item->url = 'index.php?option=com_shirts&task=code.edit&code_id='.$item->code_id;
$item->event_description = substr($item->code_desc, 0);
}
return $items;
}
function getListQuery(){
$query = parent::getListQuery();
$query->select('*');
$query->from('#__shirts_codes');
return $query;
}
}
my code in administrator/component/com_shirts/tables/code.php
<?php
defined('_JEXEC') or die;
class ShirtsTableCode extends JTable{
public function __construct(&$dbo){
parent::__construct('#__shirts_codes', 'code_id', $dbo);
}
}
my code in administrator/component/com_shirts/views/codes/view.html.php
<?php
defined('_JEXEC') or die;
jimport('joomla.application.component.view');
class ShirtsViewCodes extends JView{
protected $codes;
function display($tmpl=NULL){
$this->codes = $this->get('Items');
$this->toolbar();
parent::display($tmpl);
}
function toolbar(){
JToolBarHelper::title(JText::_('HEADER'));
JToolBarHelper::addNew('code.add');
JToolBarHelper::editList('code.edit');
JToolBarHelper::divider();
JToolBarHelper::publishList('codes.publish');
JToolBarHelper::unpublishList('codes.unpublish');
JToolBarHelper::divider();
JToolBarHelper::archiveList('codes.archive');
JToolBarHelper::trash('codes.trash');
JToolBarHelper::divider();
}
}
my code in administrator/component/com_shirts/views/codes/tmpl/default.php
<?php defined('_JEXEC') or die; ?>
<form action="index.php?option=com_shirts&view=codes" method="post" name="adminForm" id="adminForm">
<table class="adminlist">
<thead>
<tr>
<th style="width: 1%">
<input type="checkbox" name="checkall-toggle" value="" onclick="checkAll(this)" />
</th>
</tr>
</thead>
<tbody>
<?php foreach($this->codes as $i=>$code):
$url = 'index.php?option=com_shirts&view=code&task=code.edit&event_id='.$code->code_id;
?>
<tr class="row<?php echo $i%2; ?>">
<td class="center">
<?php echo JHtml::_('grid.id', $i, $code->code_id); ?>
</td>
<td class="center">
<a href="<?php echo $code->url; ?>">
<?php echo $this->escape($code->code); ?>
</a>
</td>
<td>
<a href="<?php echo $code->url; ?>">
<?php echo $this->escape($code->code_desc); ?>
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<input type="hidden" name="task" value="" />
<input type="hidden" name="boxchecked" value="0" />
<?php echo JHtml::_('form.token'); ?>
</form>
my code in administrator/component/com_shirts/views/code/tmpl/edit.php
<?php defined('_JEXEC') or die; ?>
<form
method="post"
name="adminForm"
class="form-validate"
action="index.php?option=com_shirts&code_id=<?php echo $this->code->code_id; ?>"
>
<div class="width-50 fltlft">
<fieldset class="adminform">
<ul class="adminformlist">
<?php foreach($this->form->getFieldset('required') as $field): ?>
<li>
<?php echo $field->label; ?>
<?php echo $field->input; ?>
</li>
<?php endforeach; ?>
</fieldset>
</div>
<div class="width-40 fltrt">
<fieldset class="adminform">
<ul class="adminformlist">
<?php foreach($this->form->getFieldset('optional') as $field): ?>
<li>
<?php echo $field->label; ?>
<?php echo $field->input; ?>
</li>
<?php endforeach; ?>
</ul>
</fieldset>
</div>
<input type="hidden" name="task" value="" />
<?php echo JHtml::_('form.token'); ?>
</form>
my code in administrator/component/com_shirts/views/code/view.html.php
<?php
defined('_JEXEC') or die;
jimport('joomla.application.component.view');
class ShirtsViewCode extends JView{
protected $code;
protected $form;
function display($tmpl=NULL){
$this->event = $this->get('Item');
$this->form = $this->get('Form');
$this->toolbar();
parent::display($tmpl);
}
function toolbar(){
if($this->code->code_id){
JToolBarHelper::title(JText::_('COM_SHIRTS_EDIT'));
}else{
JToolBarHelper::title(JText::_('COM_SHIRTS_ADD'));
}
JToolBarHelper::apply('code.apply', 'JTOOLBAR_APPLY');
JToolBarHelper::save('code.save', 'JTOOLBAR_SAVE');
JToolBarHelper::save2new('code.save', 'JTOOLBAR_SAVE_AND_NEW');
JToolBarHelper::divider();
JToolBarHelper::cancel('code.cancel');
}
}
and when I enter the component view, I get this error
"PHP Fatal error: Call to a member function getKeyName() on a non-object in /var/www/joomla/libraries/joomla/application/component/controllerform.php on line 393"
How do I fix this?
OK that was my bad i had mistake in manifest.xml everything is good now
In manifest there was no line
<folder>tables</folder>

Resources