Join Tables in codeigniter - php

I have two tables: Company & Users.
I have a form in which i insert Company name and others details. in that same form i have a sub form Sales info and Tech info.
The data i insert in sales and tech info gets stored into users tables.and their id are stored into company table in two fields called sales_id and tech_id.
Now for a view i want to fetch company name its sales person and its tech person,How to do it?
The Code in model:
public function get_company()
{
$this->db->select('*');
$this->db->join('users','users.id = company.sales_id','users.id = company.tech_id');
$this->db->from('company');
$query = $this->db->get();
$result = $query->result();
return $result;
}
IN The View:
<?php if(count($companys)): foreach($companys as $company): ?>
<td><?php echo $company->first_name; ?></td>
how to differentiate which is sales and which is tech person?
the Controller:
public function add_company($id = NULL)
{
$this->data['company'] = $this->company_m->get_new();
$this->data['user'] = $this->user_m->get_new();
$rules = $this->company_m->rules_admin;
$this->form_validation->set_rules($rules);
if ($this->form_validation->run() == TRUE)
{
/*Inserting Sales Person Information*/
$data['first_name'] = $this->input->post('first_name_s');
$data['last_name'] = $this->input->post('last_name_s');
$data['email'] = $this->input->post('email_s');
$data['user_type'] ="sales";
$this->user_m->save($data,$id);
$sales_id = $this->db->insert_id();
/*Inserting Tech Person Information*/
$data_tech =$this->user_m->array_from_post(array('first_name','last_name','email'));
$this->user_m->save($data_tech,$id);
$tech_id = $this->db->insert_id();
/*Insert Company Information*/
$data = $this->company_m->array_from_post(array('org_name','dba','addr1','addr2','city','state','country','pin','sales_id','tech_id','tax_number','comment','url'));
$data['sales_id']= $sales_id;
$data['tech_id']= $tech_id;
$org_id = $this->company_m->save($data, $id);
redirect('admin/company');
}
// Load the view
$this->data['subview'] = 'admin/company/add';
$this->load->view('admin/_layout_main', $this->data);
}
I hope you understood my issue.
The Company table
The Users table
the View code:
<table class="table table-striped ">
<thead>
<tr class="warning">
<th>Organization Name</th>
<th>Sales Person</th>
<th>Tech Person</th>
<th>Tax No</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php if(count($companys)): foreach($companys as $company): ?>
<tr class="active">
<td contenteditable="true" onClick="edit_company(this)" onBlur="save_company(this,'org_name','<?php echo $company->id; ?>')" ><?php echo $company->org_name; ?></td>
<td contenteditable="true" onClick="edit_company(this)" onBlur="save_company(this,'sales_id','<?php echo $company->id; ?>')" ><?php echo $company->first_name; ?></td>
<td contenteditable="true" onClick="edit_company(this)" onBlur="save_company(this,'tech_id','<?php echo $company->id; ?>')" ><?php echo $company->first_name; ?></td>
<td contenteditable="true" onClick="edit_company(this)" onBlur="save_company(this,'tax_number','<?php echo $company->id; ?>')" ><?php echo $company->tax_number; ?></td>
<td><?php echo btn_edit('admin/company/edit/' . $company->id); ?></td>
<td><?php echo btn_delete('admin/company/delete/' . $company->id); ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">We could not find any users.</td>
</tr>
<?php endif; ?>
</tbody>
</table>

In model Use
public function get_company()
{
$this->db->select('*');
$this->db->from('company');
$query = $this->db->get();
$result = $query->result();
return $result;
}
For view :-
<?php if(count($companys)): foreach($companys as $company):
$tech_person = $this->db->get_where("users",array("id"=>$company->tech_id))->row();
$sales_person = $this->db->get_where("users",array("id"=>$company->sales_id))->row();
?>
<tr class="active">
<td contenteditable="true" onClick="edit_company(this)" onBlur="save_company(this,'org_name','<?php echo $company->id; ?>')" ><?php echo $company->org_name; ?></td>
<td contenteditable="true" onClick="edit_company(this)" onBlur="save_company(this,'sales_id','<?php echo $company->id; ?>')" ><?php echo $tech_person->first_name; ?></td>
<td contenteditable="true" onClick="edit_company(this)" onBlur="save_company(this,'tech_id','<?php echo $company->id; ?>')" ><?php echo $sales_person->first_name; ?></td>
<td contenteditable="true" onClick="edit_company(this)" onBlur="save_company(this,'tax_number','<?php echo $company->id; ?>')" ><?php echo $company->tax_number; ?></td>
<td><?php echo btn_edit('admin/company/edit/' . $company->id); ?></td>
<td><?php echo btn_delete('admin/company/delete/' . $company->id); ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">We could not find any users.</td>
</tr>
<?php endif; ?>
</tbody>
In this method you even do not need join to get tech and sales person.
Edited According to your view

At first move the code your have in the controller to a model. If you use a MVC framework try and use it for what it's built, it will help keep everything clean.
So now in the model you should have the following:
function getCompany(){
$query = $this->db->select('users.first_name, company.org_name')
->from('company')
->join('users','users.id = company.sales_id','users.id = company.tech_id')
->get()
->result_array();
return $query;
}
In your view you would do the following:
<?php if(count($companys)): foreach($companys as $company): ?>
<tr>
<td><?php echo $company['first_name']; ?></td>
<td><?php echo $company['org_name']; ?></td>
</tr>
<?php endforeach; endif;?>

Related

Fatal error: Cannot use object of type stdClass as array in C:\xampp\htdocs\ov400\application\views\Useraccount\Userlist.php on line 38

I get error Fatal error: Cannot use object of type stdClass as array in
my controller name is Useraccount
function userList() {
$result['data'] = $this->Useraccount_mod->getUser_list();
$data['userlist']=$result['data'] ;
$this->load->view('Useraccount/Userlist', $data);
}
and model name is Useraccount_mod
function getUser_list() {
$query=$this->db->query("select * from users");
return $query->result();
}
and view name is Userlist
<tbody>
<?php
if ($userlist > 0) {
foreach ($userlist as $row) {
?>
<tr>
<td width="100"><?php echo $row['username']; ?></td>
<td width="100"><?php echo $row['name']; ?></td>
<td width="100"><?php echo $row['address']; ?></td>
<td width="100"><?php echo $row['creatdate']; ?></td>
<td width="100"><?php echo $row['updateddate']; ?></td>
</tr>
<?php
}
} else {
?>
<tr>
<td colspan="3" align="center"><strong>No Record Found</strong></td>
</tr>
<?php
}
?>
</tbody>
A PHP Error was encountered
Severity: Error
Message: Cannot use object of type stdClass as array
Filename: Useraccount/Userlist.php
Line Number: 38
Backtrace:
You are using result() function to fetch records, but this give records as object not as array. If you want to get array than you will have to use result_array().
function userList() {
$data['userlist'] = $this->Useraccount_mod->getUser_list();
$this->load->view('Useraccount/Userlist', $data);
}
Method 1:
function getUser_list() {
$query = $this->db->query("select * from users")->result_array(); //here we can use result() as well but it gives object not array
return $query;
}
<tbody>
<?php
if ($userlist > 0) {
foreach ($userlist as $row) {
?>
<tr>
<td width="100"><?php echo $row['username']; ?></td>
<td width="100"><?php echo $row['name']; ?></td>
<td width="100"><?php echo $row['address']; ?></td>
<td width="100"><?php echo $row['creatdate']; ?></td>
<td width="100"><?php echo $row['updateddate']; ?></td>
</tr>
<?php
}
} else {
?>
<tr>
<td colspan="3" align="center"><strong>No Record Found</strong></td>
</tr>
<?php
}
?>
</tbody>
Method 2:
function getUser_list() {
$query = $this->db->query("select * from users")->result();
return $query;
}
<tbody>
<?php
if ($userlist > 0) {
foreach ($userlist as $row) {
?>
<tr>
<td width="100"><?php echo $row->username; ?></td>
<td width="100"><?php echo $row->name; ?></td>
<td width="100"><?php echo $row->address; ?></td>
<td width="100"><?php echo $row->creatdate; ?></td>
<td width="100"><?php echo $row->updateddate; ?></td>
</tr>
<?php
}
} else {
?>
<tr>
<td colspan="3" align="center"><strong>No Record Found</strong></td>
</tr>
<?php
}
?>
</tbody>

Diffrent query in one function codeigniter

I use Codeigneter and I make a search function and in my function I want run two diffrent query and I want show them in diffrent table, How I can do that Sorry I can't show the data
This is My code:
My controller
function showDetail(){
// Retrieve the posted search term.
$detailTiket = $this->input->get('ticket_id');
// Use a model to retrieve the results.
$data["result"]= $this->tracking_model->showDetail($detailTiket);
$data1["result"]= $this->tracking_model->showDetail2($detailTiket);
// Pass the results to the view.
$this->load->view('tracking/tiket_detail',$data,$data1);
}
My Model
function showDetail($detailTiket)
{
if($detailTiket==""){
$detailTiket = "";
}
$showDetailTiket=$this->db->query("My Query1");
return $detailTiketDown->result();
}
function showDetail2($detailTiket)
{
if($detailTiket==""){
$detailTiket = "";
}
$detailTiketDown=$this->db->query("My query2");
return $detailTiketDown->result();
}
My view
<table Width='800'>
<?php
foreach($data as $row){?>
<tbody>
<tr>
<td><?php echo $row->ticket_id; ?></td>
</tr>
<tr>
<td><?php echo $row->created_time; ?></td>
</tr>
<tr>
<td><?php echo $row->start_IT; ?></td>
</tr>
<tr>
<td><?php echo $row->estimasi_selesai; ?></td>
</tr>
<tr>
<td><?php echo $row->name; ?></td>
</tr>
<tr>
<td><?php echo $row->description; ?></td>
</tr>
<tr style="background-color: cyan">
<td><b><?php echo $row->Status; ?></b></td>
</tr>
</tbody>
<?php } ?>
</table>
</center>
</div>
<div>
<table Width='1000'>
<?php foreach($data1 as $rows){ ?>
<tbody>
<tr>
<td><?php echo $rows->Tgl_Waktu; ?></td>
<td><?php echo $rows->PIC; ?></td>
<td><?php echo $rows->Tracking_Ticket; ?></td>
<td><?php echo $rows->Keterangan_Ticket; ?></td>
<td><?php echo $rows->File_Pendukung; ?></td>
</tr>
</tbody>
<?php }?>
</table>
You can pass data like Associative array to view
change your controller like this
Controller:
$data["result"]= $this->tracking_model->showDetail($detailTiket);
$data["result1"]= $this->tracking_model->showDetail2($detailTiket);
// Pass the results to the view.
$this->load->view('tracking/tiket_detail',$data);
view:
Table1 :
foreach($result as $row)
{
//for first result
}
Table2:
foreach($result1 as $row1)
{
//for second result
}

How to create incrementing button in Codeigniter that updates database onclick?

I am new to codeigniter. I have a project where Admin allots balance to its reseller. Now I want to create a button in html which provides interface to admin to increment the balance on button click. and when he increments it the value should be also updated in database. Maybe this is very simple but i couldn't do this! I think I should use ajax or Jquery both for this maybe
Below is my code for view :(want to add increment and decrement button besides the balance input )
<?php echo validation_errors(); ?>
<?php echo form_open(); ?>
<table class="table">
<tr>
<td>SIP Username</td>
<td><?php echo form_input('sip_username', set_value('sip_username', $user->sip_username)); ?></td>
</tr>
<tr>
<td>SIP Password</td>
<td><?php echo form_input('sip_password', set_value('sip_password', $user->sip_password)); ?></td>
</tr>
<tr>
<td>Key</td>
<td><?php echo form_input('key', set_value('key', $user->key), 'readonly'); ?></td>
</tr>
<tr>
<td>Allocation Block</td>
<td><?php echo form_input('allocation_block', set_value('allocation_block', $user->allocation_block)); ?></td>
</tr>
<tr>
<td>Name</td>
<td><?php echo form_input('name', set_value('name', $user->name)); ?></td>
</tr>
<tr>
<td>Reseller Email</td>
<td><?php echo form_input('email', set_value('email', $user->email)); ?></td>
</tr>
<tr>
<td>Password</td>
<td><?php echo form_password('password'); ?></td>
</tr>
<tr>
<td>Confirm password</td>
<td><?php echo form_password('password_confirm'); ?></td>
</tr>
<tr>
<td>User_Required</td>
<td><?php echo form_input('user_num', set_value('user_num', $user->user_num)); ?></td>
</tr>
<tr>
<td>Balance</td>
<td><?php echo form_input('balance', set_value('balance', $user->balance)); ?></td>
</tr>
<tr>
<td>Phone</td>
<td><?php echo form_input('phone', set_value('phone', $user->phone)); ?></td>
</tr>
<tr>
<td>Address</td>
<td><?php echo form_input('address', set_value('address', $user->address)); ?></td>
</tr>
<tr>
<td>Status</td>
<td><?php echo form_dropdown('status', array('Active' => 'Active', 'Inactive' => 'inactive', 'Delete' => 'delete'), $this->input->post('status') ? $this->input->post('status') : $user->status ); ?></td>
</tr>
<tr>
<td>Country</td>
<td><?php echo form_input('country', set_value('country', $user->country)); ?></td>
</tr>
<tr>
<td>Country Code</td>
<td><?php echo form_input('country_code', set_value('country_code', $user->country_code)); ?></td>
</tr>
<tr>
<td></td>
<td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
</tr>
</table>
<?php echo form_close();?>
The Controller :
public function edit ($id = NULL)
{
// Fetch a user or set a new one
if ($id) {
$this->data['user'] = $this->reseller_m->get($id);
count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
}
else {
$this->data['user'] = $this->reseller_m->get_new();
}
// Set up the form
$rules = $this->reseller_m->rules_admin;
$id || $rules['password']['rules'] .= '|required';
$this->form_validation->set_rules($rules);
// Process the form
if ($this->form_validation->run() == TRUE) {
$data = $this->reseller_m->array_from_post(array('sip_username','sip_password','key','allocation_block','name','email','password','phone','balance','user_num','address','country','country_code','created','modified','status'));
$data['password'] = $this->reseller_m->hash($data['password']);
$key=$this->reseller_m->save($data, $id);
//here we get the last inserted record id in $last_id
$last_id = $this->db->insert_id();
//The logic to create blank rows in user table mapped to reseller_id
$values=array($this->input->post('name'),$this->input->post('country_code'),$this->input->post('allocation_block'),$this->input->post('user_num'));
$key=implode('-',$values);
$this->db->where('id',$last_id);
$this->db->update('reseller',array('key'=>$key));
for($i=1; $i<=$data['user_num'];$i++)
{
$userdata=array('key'=>$key);
// here users is taken name of user table with retailer_id is field
$this->db->insert('users',$userdata);
}
redirect('admin/reseller');
}
// Load the view
$this->data['subview'] = 'admin/reseller/edit';
$this->load->view('admin/_layout_main', $this->data);
}
If you are familiar with ajax, you can use ajax for this.
On click of the button you can call ajax and write code in the controller function to update the value in the database.

Get Magento Order ID, Order Value and Coupon Code in success.phtml

We need to get Order ID, Order Value and Coupon Code from success.phtml.
We already have Google added to our success.phtml, and now we need to set up a new affiliate. We are not sure what $order_details and $adwords_saleamt do? Can we re-use them in any way?
Example
<?php
$order_details = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
$adwords_saleamt = $order_details->subtotal;
?>
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = xxxxxxxxxx;
var google_conversion_language = "xy";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "xxxxxxxxxxxx";
var google_conversion_value = 0.00;
if (<?php echo $adwords_saleamt; ?>) {
google_conversion_value = <?php echo $adwords_saleamt; ?>;
}
var google_conversion_currency = "EUR";
var google_remarketing_only = false;
/* ]]> */
</script>
You can try following
$order_details = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
$adwords_saleamt = $order_details->subtotal; //subtotal without tax and shipping
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId(); //Order Id
$couponCode = $order_details->coupon_code; //Coupon code
Then use the values in your affiliate code.
Code there please, copy and paste in your file and then check, if any issue let me know...
echo $this->getLayout()->createBlock('checkout/cart_totals')->setTemplate('onepagecheckout/onepage/review/totals.phtml')->toHtml();
$order_id = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order_details = Mage::getModel('sales/order')->loadByIncrementId($order_id);
// Get shipping method
$shipping_method = $order_details->_data["shipping_description"];
// Get ship-to address information
$shipping_address_data = $order_details->getShippingAddress();
$session = Mage::getSingleton('core/session', array('name'=>'frontend'));
$card = $session->getGiftcard();
$cvv = $session->getGiftcardCvd();
$amount = $session->getGiftcardDeduct();
?>
<div class="inner_content">
<div class="inner-header-categories clearfix">
<h1><?php echo $this->__('Order Confirmation') ?></h1>
</div>
<div class="order_confirmation_div">
<h3><?php echo $this->__('Thank you for your order!') ?></h3>
<p>
<?php echo $this->__('Dear') ?> <?php echo $shipping_address_data['firstname']; ?>,<br>
<?php echo $this->__('Thank you for your order!') ?><br>
<?php if ($this->getCanViewOrder()) :?>
<p><?php echo $this->__('Your order id is: %s.', sprintf('%s', $this->escapeHtml($this->getViewOrderUrl()), $this->escapeHtml($this->getOrderId()))) ?></p>
<?php else :?>
<p><?php echo $this->__('Your order id is: %s.', $this->escapeHtml($this->getOrderId())) ?></p>
<?php endif;?>
</p>
<?php
$orderObj = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$orderItems = $orderObj->getAllItems();
?>
<h4><?php echo $this->__('Item Summary') ?></h4>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="order_ct_table">
<tr>
<th><?php echo $this->__('Product') ?></th>
<!--<th>Ship by</th>
<th>Delivers By</th>-->
<th><?php echo $this->__('Qty') ?></td>
<th class="no_border"><?php echo $this->__('UNIT PRICE') ?></th>
</tr>
<?php foreach($orderItems as $item)
{
?>
<tr>
<td class="porduct_dark_text"><div class="image_cart_in"><p><?php echo $item->getName();?></p></div></td>
<!--<td>18 August 2013</td>
<td>20 August 2013</td>-->
<td><?php echo round($item->getQtyOrdered(), 0);?></td>
<td class="no_border">$<?php echo number_format($item->getPrice(),2); ?></td>
</tr>
<?php }
?>
</table>
<h4><?php echo $this->__('Purchase Summary') ?>:</h4>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="order_ct_table_two">
<tr>
<td class="col_one_left"><?php echo $this->__('ITEM SUB TOTAL') ?> :</td>
<td class="no_border">$<?php echo number_format($orderValue = $orderObj->getSubtotal(),2);?></td>
</tr>
<?php if($orderObj->getDiscountAmount()!=0) { ?>
<tr>
<td class="col_one_left"><?php echo $this->__('Discount') ?> :</td>
<td class="no_border">$<?php echo number_format($orderValue = $orderObj->getDiscountAmount(),2);?></td>
</tr>
<?php } ?>
<tr>
<td class="col_one_left"><?php echo $orderObj->getShippingDescription(); ?>:</td>
<td class="no_border">$<?php echo number_format($orderSValue = $orderObj->getShippingAmount(),2); ?></td>
</tr>
<?php if(!empty($card)): ?>
<tr>
<td class="col_one_left"><?php echo $this->__('Giftcard ('.$card.') discount') ?>:</td>
<td class="no_border">$<?php echo number_format($amount,2); ?></td>
</tr>
<?php endif; ?>
<tr>
<td class="col_one_left"><?php echo $this->__('Tax') ?>:</td>
<?php
$GT = number_format($orderObj->getGrandTotal(),2);
$STA = number_format($orderSValue = $orderObj->getShippingAmount(),2);
$IST = number_format($orderValue = $orderObj->getSubtotal(),2);
$TAX = $IST+$STA;
$TTAX = $GT-$TAX;
?>
<td class="no_border">$<?php echo number_format($orderSValue = $orderObj->getTaxAmount(),2); ?></td>
</tr>
<tr>
<td class="col_one_left"><?php echo $this->__('Total') ?>:</td>
<td class="no_border">$<?php echo $GT;?></td>
</tr>
</table>
<ul class="billing_detail_confirm_ul_top"><li><h4><?php echo $this->__('Billing Address') ?></h4></li>
<li><h4><?php echo $this->__('Shipping Address') ?></h4></li></ul>
<ul class="billing_detail_confirm_ul">
<li>
<table border="0" cellspacing="0" cellpadding="0" style="border-right:none;">
<tr>
<td><?php echo $this->__('First Name') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['firstname']; ?></td>
</tr>
<tr>
<td><?php echo $this->__('Last Name') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['lastname'];?></td>
</tr>
<tr>
<td><?php echo $this->__('Company') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['company'];?></td>
</tr>
<tr>
<td><?php echo $this->__('Phone') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['telephone'];?></td>
</tr>
<tr>
<td><?php echo $this->__('Address') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['street'];?><br>
<?php echo $shipping_address_data['region']; ?>, <?php echo $shipping_address_data['postcode']; ?></td>
</tr>
<tr>
<td><?php echo $this->__('City') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['city'];?></td>
</tr>
<tr>
<td><?php echo $this->__('Country') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['country_id'];?></td>
</tr>
<tr>
<td><?php echo $this->__('State') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['region']; ?></td>
</tr>
<tr>
<td><?php echo $this->__('Zip') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['postcode']; ?></td>
</tr>
<tr>
<td><?php echo $this->__('Email') ?></td>
<td class="right_bold_text"><?php if($shipping_address_data['email']=='') { echo Mage::getSingleton('customer/session')->getCustomer()->getEmail(); } else { echo $shipping_address_data['email']; } ?></td>
</tr>
</table>
</li>
<li>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td><?php echo $this->__('First Name') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['firstname']; ?></td>
</tr>
<tr>
<td><?php echo $this->__('Last Name') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['lastname'];?></td>
</tr>
<tr>
<td><?php echo $this->__('Company') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['company'];?></td>
</tr>
<tr>
<td><?php echo $this->__('Phone') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['telephone'];?></td>
</tr>
<tr>
<td><?php echo $this->__('Address') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['street'];?><br>
<?php echo $shipping_address_data['region']; ?>, <?php echo $shipping_address_data['postcode']; ?></td>
</tr>
<tr>
<td><?php echo $this->__('City') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['city'];?></td>
</tr>
<tr>
<td><?php echo $this->__('Country') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['country_id'];?></td>
</tr>
<tr>
<td><?php echo $this->__('State') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['region']; ?></td>
</tr>
<tr>
<td><?php echo $this->__('Zip') ?></td>
<td class="right_bold_text"><?php echo $shipping_address_data['postcode']; ?></td>
</tr>
<tr>
<td><?php echo $this->__('Email') ?></td>
<td class="right_bold_text"><?php if($shipping_address_data['email']=='') { echo Mage::getSingleton('customer/session')->getCustomer()->getEmail(); } else { echo $shipping_address_data['email']; } ?></td>
</tr>
</table>
</li>
</ul>
<h3><td><?php echo $this->__('Thank you for shopping with us!') ?></td></h3>
<button class="proceed" style="margin-bottom:40px;" onclick="window.location='<?php echo $this->getUrl() ?>'"><?php echo $this->__('Return to Homepage') ?></button>
</div>
</div>

cant fetch row data from database after search in search form using codeigniter

hey guys i am new in codeigniter and i am working on an application in which i have database and many records in database..i make a search form in which i make a text field in which user can fill any field value and search records.but here is a small problem..when i fill a name in textbox and click on search button no record display on view . . pls help me . . thanks ..
my controller is:
function search()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('inputsearch', 'Search Your Text Here','required');
if ($this->form_validation->run() == TRUE)
{
$this->load->helper('form');
$this->load->helper('html');
$search_this=$this->input->post('inputsearch');
$this->load->model('mod_user');
$searchmember=$this->mod_user->search_member($search_this);
$data['searchmember'] = $searchmember;
var_dump($searchmember);
$this->load->view('view_home',$data);
$this->load->view('view_homemember',$data);
}
else
{
redirect('ctl_home');
}
}
view is:
<form class="userinfo" action="" method="post">
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td width="5%;"> </td>
<td width="5%;"> </td>
<td width="15%;">Name</td>
<td width="15%;">Moderator Name</td>
<td width="20%;">KCC Branch</td>
<td width="15%;">Father/Husband Name</td>
<td width="15%;">Address</td>
<td width="10%;">Date</td>
</tr>
<?php foreach ($rows as $row):?>
<tr>
<td><?php echo anchor("Ctl_Home/edit_member/" . $row->member_id, 'Edit'); ?></td>
<td>Delete</td>
<td><?php echo $row->member_name; ?></td>
<td><?php echo $row->moderator_name; ?></td>
<td><?php echo $row->branch_name; ?></td>
<td><?php echo $row->father_name; ?></td>
<td><?php echo $row->address; ?></td>
<td><?php echo $row->date; ?></td>
</tr>
<?php endforeach; ?>
</table>
</form>
my model is:
function search_member($search_this)
{
$query=$this->db->query("SELECT tbl_members.* , tbl_moderators.member_id AS moderator_id, tbl_moderators.member_name AS moderator_name, tbl_moderators.member_no AS moderator_no, tbl_branches.branch_name FROM tbl_members, tbl_members AS tbl_moderators, tbl_branches WHERE tbl_members.moderator_id = tbl_moderators.member_id AND tbl_branches.branch_id = tbl_members.kcc_branch
AND CONCAT(tbl_members.member_name, ' ', tbl_moderators.member_name, ' ',tbl_members.moderator_id, ' ', tbl_moderators.member_id, ' ',tbl_branches.branch_name, ' ',tbl_members.father_name, ' ',tbl_members.address, ' ',tbl_moderators.address, ' ',tbl_members.date, ' ',tbl_moderators.date) like '%".$search_this."%'");
if ($query->num_rows >= 0)
{
foreach($query->result_array() as $row)
{
$data[]=$row;
}
return $data;
}
}
Your loop should have been like this
<?php foreach ($searchmember as $row):?>
<tr>
<td><?php echo anchor("Ctl_Home/edit_member/" . $row->member_id, 'Edit'); ?></td>
<td>Delete</td>
<td><?php echo $row->member_name; ?></td>
<td><?php echo $row->moderator_name; ?></td>
<td><?php echo $row->branch_name; ?></td>
<td><?php echo $row->father_name; ?></td>
<td><?php echo $row->address; ?></td>
<td><?php echo $row->date; ?></td>
</tr>
<?php endforeach; ?>
It is $searchmember as $row instead of $rows as $row in foreach
If $data['searchmember'] contains value in controller,
you can access the varibale in view as $searchmember

Categories