How can I join a foreach with another foreach in the query WHERE.
I have stored in the second Foreach the contents with the ID of the first ID and wants to display this in the respective column.
So if in the second column db_buy.tb_buy_shop has the ID 2, this should show in the HTML column where the db_shop.tb_shop_id 2.
view:
<?php
foreach ($shops_where_1 as $shop_where_1):
foreach ($buy_sums as $buy_sum):
if($buy_sum['tb_buy_shop']==$shop_where_1['tb_shop_id']) {
$gesamtsumme = $buy_sum['gesamtsumme'];
}
endforeach;
?>
<tr>
<td><?php echo $shop_where_1['tb_shop_name']; ?></td>
<td></td>
<td><div class="input-group"><div class="input-group-addon">€</div><input type="text" class="form-control" value="<?php echo number_format($buy_sum['gesamtsumme'],2,",",".");?>" disabled></div></td>
<td><div class="input-group"><div class="input-group-addon">€</div><input type="text" class="form-control" value="<?php echo number_format($gesamtsumme,2,",",".");?>" disabled></div></td>
<td></td>
<td></td>
</tr>
<?php
// endforeach;
endforeach;
?>
Model:
public function shops_where_1()
{
$this->db->select('*');
$this->db->from('db_shop');
$this->db->where('db_shop.tb_shop_buy = 1');
$this->db->order_by('tb_shop_name');
$query = $this->db->get();
return $query->result_array();
}
public function buy_sums()
{
$this->db->select('(SELECT SUM(db_buy.tb_buy_gesamt) FROM db_buy) AS gesamtsumme');
$this->db->select('(SELECT SUM(db_buy.tb_buy_abbezahlt) FROM db_buy) AS abbezahlt', FALSE);
$this->db->select('tb_buy_shop');
$this->db->from('db_buy');
$query = $this->db->get();
return $query->result_array();
}
Controller:
public function buy_shop($slug)
{
// if (!$this->session->userdata('logged_in'))
// {
// redirect('users/login');
// }
$data['get_shops'] = $this->Admin_model->get_shops($slug);
$data['shops_where_1'] = $this->Admin_model->shops_where_1();
$this->load->view('templates/header_acp');
$this->load->view('admin/buy_shop', $data);
$this->load->view('templates/footer');
}
If I am correct in what you are asking it seems that you are just wanting to know how to format the code so that it will build the table. This would be how to do so.
<?php
foreach ($shops_where_1 as $shop_where_1):
?> <tr>
<td><?php echo $shop_where_1['tb_shop_name']; ?></td>
<?php
foreach ($buy_sums as $buy_sum):
if($buy_sum['tb_buy_shop']==$shop_where_1['tb_shop_id']) {
?>
<td><div class="input-group"><div class="input-group-addon">€</div><input type="text" class="form-control" value="<?php echo number_format($buy_sum['gesamtsumme'],2,",",".");?>" readonly></div></td>
<?php
}
endforeach; // endforeach 2
</tr>
endforeach; // endforeach 1;
?>
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 } } } ?>
Unable to display the data in a CodeIgniter view. Need to print the builders_name in a CodeIgniter view. So far I have the following code:
Model :
public function get_builders()
{
$query1 = $this->db->select('name')->from('builders')->where("name LIKE 'a%'")->limit(3)->get();
$query2 = $this->db->select('name')->from('builders')->where("name LIKE 'b%'")->limit(3)->get();
$result1 = $query1->result();
$result2 = $query2->result();
return array($result1, $result2);
}
Controller:
public function index(){
$this->load->database();
$data['h']= $this->Builders_Model->get_builders();
$this->load->view('home', $data);
}
View:
<?php foreach ($h->result as $row) { ?>
<tr>
<td><?php echo $row->name;?></td>
</tr>
<?php } ?>
Don't like this try it
public function get_builders()
{
$query1 = $this->db->select('name')
->from('builders')
->like('name','a')
->or_like('name','b')
->limit(3)->get();
$result = $query1->result();
return $result;
}
views code
<?php foreach ($h as $row) { ?>
<tr>
<td><?php echo $row->name;?></td>
</tr>
<?php } ?>
Hope fully works . Thank You
What you pass to the view in Codeigniter is accessible by its name. Just <php echo $h; ?> and you should be good to go
you can use :
<?php
foreach($h as $row){?>
<tr>
<td><?php echo $row->name;?></td>
</tr>
}?>
You have to use $h variable in foreach for notation :
<?php
<tr>
<td><?php echo $rowDetail->name; ?></td>
</tr>
}
?>
First this is you haven't loaded the Builder_modal in your controller try to do some thing like
$this->load->model('Builder_model');
after $this->load->database();
Next thing in your view you have to echo like
echo $h; or var_dump($h) if you have an array
It's really working
model
public function get_builders()
{
$this->db->select('name');
$this->db->from('builders');
$this->db->like('name','a');
$this->db->or_like('name','b');
$this->db->limit(3);
$query = $this->db->get();
return $query->result();
}
controller
public function index(){
$this->load->model('Builder_model');
$data= $this->Builders_Model->get_builders();
$this->load->view('home', ['data'=>$data]);
}
view
<?php foreach ($h->result as $row) { ?>
<tr>
<td><?php echo $row->name;?></td>
</tr>
<?php } ?>
$data['posts'] = $this->whiteboard_model->u_getTSrowsindex(array('limit'=>$this->perPage));
$data['postsexp'] = $this->whiteboard_model->u_getTSrowsexp(array('limit'=>$this->perPage));
//print_r($data['postsexp']); exit;
foreach($data['posts'] as $post)
{
$project_id =$post['project_name'];
$data['projectName'] = $this->whiteboard_model->get_project_name($project_id);
print_r($data['projectName']);
}
//This is my controller
public function get_project_name($project_id) {
$this->db->select('project_name');
$this->db->from('project_list');
$this->db->where('project_id',$project_id);
$query = $this->db->get();
//echo $this->db->last_query();
//return fetched data
return ($query->num_rows() > 0)?$query->result_array():FALSE;
}
//my model
<?php if(!empty($posts)): foreach($posts as $post):
//print_r($post);?>
<tr>
<td> <?php echo $post['advisor']; ?></td>
<td> <?php echo $post['spentdate']; ?></td>
<td><?php echo $post['project_name']; ?>
<?php if(!empty($projectName)){
foreach($projectName as $prj_name){ //print_r($projectName);
echo $prj_name['project_name'];
}
} ?>
</td>
<td> <?php echo $post['activity']; ?></td>
<td> <?php echo $post['billtype']; ?></td>
<td> <?php echo $post['timespent']; ?></td>
<td> <?php if($post['status'] == 1){?>
<b style = "color:blue;">Pending</b>
<?php }else if($post['status'] == 2){?>
<b style = "color:green;">Approved</b>
<?php }else{?> <b style = "color:red;">Rejected</b>
<?php } ?> </td>
</tr>
<?php endforeach; else: ?>`enter code here`
//my view file
Finally iam not getting proper values from projectName array ..its giving only one name
It seems like you are fetching data using the project_id
$this->db->select('project_name');
$this->db->from('project_list');
$this->db->where('project_id',$project_id);
$query = $this->db->get();
Here you may be getting only the details of one project where the project_id = $project_id
Comment out that line and see if you get all the project names. Additionally use a print_r($query->result()); to verify your data.
So that it would look like:
$this->db->select('project_name');
$this->db->from('project_list');
//$this->db->where('project_id',$project_id);
$query = $this->db->get();
print_r($query->result());
You can refer more here.
I am working on a point of sale for a hardware shop developed using codeigniter framework.
I want to come up with a detailed statement of account like in the picture below
I have two tables which are sales and payment, I want to get data from this two tables so that I can generate a statement of account of a specific customer.
This will help since the customer can be able to see all the items he/she bought on cash basis or credit basis and also show how much they has paid grouped by date.
It will also be possible to calculate the amount due.
I can be able to list separately (sales and payment) by using the below code and thereby calculate the amount due.
Sales
<?php
$salestotal = 0;
if (is_array($sales) || is_object($sales))
{
foreach ($sales as $key=>$sale): {?>
<tr>
<td>
<?php echo $sale->date; ?>
</td>
<td>
<?php echo $sale->id; ?>
</td>
<td>
<?php echo $sale->grand_total; ?>
</td>
<td>
<?php echo $sale->paid; ?>
</td>
<td></td>
</tr>
<?php
if(isset($sale->grand_total))
$salestotal += $sale->grand_total;
} endforeach ; }?>
Payments
<?php
$paymentstotal = 0;
if (is_array($payments) || is_object($payments))
{
foreach ($payments as $key=>$payment): {?>
<tr>
<td>
<?php echo $payment->date; ?>
</td>
<td class="text-center">
<?php echo $payment->sale_id; ?>
</td>
<td class="text-center">
<?php echo $payment->paid_by; ?>
</td>
<td class="text-center">
<?php echo $payment->cheque_no; ?>
</td>
<td class="text-center">
<?php echo $payment->amount; ?>
</td>
<td class="text-center">
<?php echo $this->customers_model->getUserna($payment->created_by); ?>
</td>
<td class="text-center">
<?php echo $payment->pos_paid; ?>
</td>
<td class="text-center">
<?php echo $payment->pos_balance; ?>
</td>
<td class="text-right">
<?php echo $this->customers_model->getStorename($payment->store_id); ?>
</td>
</tr>
<?php
if(isset($payment->amount))
$paymentstotal += $payment->amount;
} endforeach ;}?>
My controller
function statement($id = NULL)
{
if (!$this->Admin) {
$this->session->set_flashdata('error', $this->lang->line('access_denied'));
redirect('pos');
}
if($this->input->get('id')) { $id = $this->input->get('id', TRUE); }
$this->data['sales'] = $this->customers_model->getSales($id);
$this->data['payments'] = $this->customers_model->getPayments($id);
$this->data['customer'] = $this->customers_model->getCustomername($id);
$this->data['customer_id'] = $id;
$this->page_cons('customers/statement', $this->data);
}
My Model
public function getSales($id)
{
$q = $this->db->get_where('sales', array('customer_id' => $id));
if( $q->num_rows() > 0 ) {
return $q->result();
}
return FALSE;
}
public function getPayments($id)
{
$q = $this->db->get_where('payments', array('customer_id' => $id));
if( $q->num_rows() > 0 ) {
return $q->result();
}
return FALSE;
}
How do I combine this two tables?
I hope I am clear enough on this question, I have been trying to Google but I have no luck.
I will appreciate any help. Thanks
Edit
MYSQL tables
Sales
Payments
You can reuse the given methods getSales() and getPayments() in a new method getSalesWithPayments() to combine the two results:
public function getSalesWithPayments($customerId) {
$sales = [];
foreach ($this->getSales($customerId) as $sale) {
$sale->payment = null;
$sales[$sale->id] = $sale;
}
foreach ($this->getPayments($customerId) as $payment) {
$sales[$payment->sale_id]->payment = $payment;
}
return $sales;
}
In the first loop you initialize $sale->payment (for the case that a sale has no payment yet) and populate the $sales array, which is indexed by the id column. This way you can easily access any sale by its id without an expensive search.
In the second loop you assign the payments to the corresponding sales.
Note that you might need to add logic to handle empty results (which wouldn't be necessary if you would just return an empty array instead of FALSE).
You can now use it like..
Controller:
$this->data['sales'] = $this->customers_model->getSalesWithPayments($id);
View:
<?php foreach ($sales as $sale): ?>
<tr>
<td><?php echo $sale->date; ?></td>
<td><?php echo $sale->id; ?></td>
...
<?php if($sale->payment !== null): ?>
<td><?php echo $sale->payment->date; ?></td>
<td><?php echo $sale->payment->amount; ?></td>
...
<?php else: // empty cells if no payment ?>
<td></td>
<td></td>
...
<?php endif; ?>
</tr>
<?php endforeach; ?>
If you post the sales and payments table the answer will be better and exact. You can use MySQL Join to get combined data from 2 tables.
Try this:
$this->db->select('*')
->from('sales')
->join('payments', 'payments.sales_id = sales.id', 'right')
->where(array('sales.customer_id' => $id));
$results = $this->db->get();
I just want to ask how can you get the value of the texbox in this code:
Here is my view:
<div class="row">
<h3><input type='text' name="table1" value='<?php echo $table1; ?>' style='border: none; background-color: transparent;' readonly/></h3>
<div class="table_design">
<button style='position: absolute; top:290px; right: 173px;' class='btn_addinventory btn-success btn-sm' id='btn_addinventory' name='btn_addinventory'><span class='glyphicon glyphicon-plus'></span> Add Inventory</button>
<table class="boom_table">
<tr>
<?php foreach($sample1 as $field) { ?>
<th><?php echo $field->name; ?></th>
<?php } ?>
</tr>
<?php foreach($select1 as $row) { ?>
<tr>
<td><?php echo $row->ID; ?></td>
</tr>
<?php } ?>
</table>
<?php foreach($sample1 as $field) { ?>
<input type="text" name="txt_rowname" value="<?php echo $field->name; ?>"/>
<?php } ?>
</div>
</div>
Here is the controller:
public function inventory_new(){
$tablenaming1 = $_POST['ninja7'];
// var_dump($tablenaming1);
// exit;
$this->session->userdata('login_session');
$this->data['title'] = "Inventory";
$this->load->vars($this->data);
$this->load->view('homeview');
$select_inv['inventorytype'] = $this->inventory_model->select_tables();
$this->load->view('inventoryview', $select_inv);
$sample['sample'] = $this->inventory_model->select_fields_2($_POST['ninja7']);
$select_details['select'] = $this->inventory_model->select_queries2($this->input->post());
// var_dump($select_details);
// exit;
$value_array = array('sample1' => $sample['sample'],
'table1' => $tablenaming1,
'select1' => $select_details['select']);
$this->load->view('sampleviewtable', $value_array);
$this->load->view('footer_view');
}
And here is my model:
public function select_fields_2($tablename2){
$fields = $this->db->field_data($tablename2);
return $fields;
}
public function select_queries2($tablename){
$select_table = $this->db->select('*')
->from($tablename['ninja7']);
return $select_table->get()->result();
}
In my view above you will see:
<?php foreach($select1 as $row) { ?>
<tr>
<td><?php echo $row->ID; ?></td>
</tr>
<?php } ?>
And this:
<?php foreach($sample1 as $field) { ?>
<input type="text" name="txt_rowname" value="<?php echo $field->name; ?>"/>
<?php } ?>
I want to get the value of the <input type="text" name="txt_rowname" value="<?php echo $field->name; ?>"/> from my view, For example: if the value of this textbox is equal to 'Name' instead of $row->ID now it should be $row->Name. I really can't figure out this yet, I want to pass the value of the textbox instead of putting 'ID' on $row. Is there a way to do that?
Thank you in advance for your help! :)
Eljon as per your comments and what i understand your requirement is you need to populate table structure on clicking table name.
So,in codeigniter their is a simple way to achieve this like this:
$fields = $this->db->list_fields('table_name');
foreach ($fields as $field)
{
echo $field;
}
Here 'list_fields' will give you your fields name of specified table.
If you want to get field data you can try this out :
$fields = $this->db->field_data('table_name');
foreach ($fields as $field)
{
echo $field->name;
echo $field->type;
echo $field->max_length;
echo $field->primary_key;
}
Here is more information in codeigniter documentation
https://ellislab.com/codeigniter/user-guide/database/fields.html
Hope this helps you.
Regards,
Zeeshan.
----- Update ------
You can populate database table in tabular structure using inbuilt codeigniter library,you can directly echo that table in your view the code goes like this:
$this->load->library('table');
$query = $this->db->get('table_name');
$page_data['db_table'] = $this->table->generate($query);
In your view :
echo $db_table
--- Update 2 ----
Update as per your comments and requirements
you can use set_template of table class to set the id as
$tmpl = array ( 'table_open' => '<table id="YOUR_ID" class="mytable">' );
$this->table->set_template($tmpl);
More details regarding table class here https://ellislab.com/codeigniter/user-guide/libraries/table.html