pass two query result to view in codeigniter - codeigniter

i have two query $q1 and $q2.
from the query1 i get multiple records and each record having id and i want use this id for second query in where condition.
i am doing this in controller.
i have tried following code.
In the foreach i am trying to store id and pass to $q2 where condition.
//Query 1
$q1 = $this->db->select(array(
'spf.id as id' ,
'spf.name',
'spf.added_on'
))->from('sp_form spf')->where($where)->order_by('spf.id desc')->limit(10, $page * 10)->get();
$data['data'] = $q1->result_array();
foreach($data as $rec)
{
$id = $rec['id']; // here how i catch id for each row
//Query 2
$q2 = $this->db->select("count('id') as count ")->from('sp_topic spft')->where('spft.formid',$id)->get();
$data['count'] = $q1->row_object();
}
// pass combine result to view
$this->load->view('myview', $data,true);
Edit:
This is my view.
I have try Nishant answer and i get resultq1 using foreach but how can i get result of resultq2.
<table width="100%">
<tr>
<td class="th"> Details</td>
<td width="5%" class="th"> Answer</td>
<td width="15%" class="th">Started by</td>
<td width="15%" class="th">Created on</td>
</tr>
<?php foreach($resultq1 as $row):?>
<tr>
<td><?php echo $row['name'] ;?></td>
<td >---- </td> // here i want to use resultq2
<td><?php echo $row['added_by'] ;?></td>
<td ><?php echo $row['added_on'];?></td>
</tr>
<?php endforeach;?>
</table>

you can do it like This.
$resultq1= $q1->result_array();
$data['resultq1'] = $resultq1;
$resultq2 = array();$i=0;
foreach($resultq1 as $row){
$id = $row['id'];
$q2 = $this->db->select("count('id') as count ")->from('sp_topic spft')>where('spft.formid',$id)->get();
$resultq2[$i]['count'] = $q1->row_object();
$i++;
}
$data['resultq2'] = $resultq2;
$this->load->view('myview', $data,true);
OR
You can use array_merge
like
$data = array_merge($resultq1, $resultq2);
Then in the myview you will get both the results in variables $resultq1, and $resultq2.
You can pass any numbers of the variables from the controller to the view file by $data['variable_name'] and it can be retrieved in the view file like simple variable $variable_name.
Some Sample links which might help :-
Passing 2 types of array variables in codeigniter view files

$data['count'] = $q1->row_object();
change this like below:
foreach($data as $rec)
{
$id = $rec['id']; // here how i catch id for each row
$q2 = $this->db->select("count('id') as count ")->
from('sp_topic spft')->where('spft.formid',$id)->get();
$data[$id]['count'] = $q2->result_array();
}
$this->load->view('myview', $data,true);

Related

get the selected value from drop down menu in cakephp 2.x

I am trying to get the selected from a drop down menu. but it returns the index of the selected elements. I want to retrieve the value of that index. I want to get the value of $year. how can I get the value?
Controller that generate the years
<?php
class StockReportConditionsController extends AppController{
public function report() {
$years = array();
for ($i = 0; $i < 4; $i++) {
$year = date("Y");
$year = $year -$i;
$years[$i]=$year;
}
$this->set(compact('years'));
}
View
<h1>Weekly Report</h1>
<table>
<tr>
<th>Year</th>
<th>Stores</th>
<th>Department</th>
</tr>
<?php echo $this->Form->create('StockReport',array('url'=>array('type' => 'get','action'=>'search')));?>
<tr>
<td>
<?php echo $this->Form->input('year',array('type'=>'select','options'=>$years)); ?>
</td>
<td>
<?php echo $this->Form->input('Store.name',array('type'=>'select','options'=>$stores)); ?>
</td>
<td>
<?php echo $this->Form->input('StockReportCondition.bu_no',array('type'=>'select','options'=>$departments)); ?>
</td>
</tr>
<?php echo $this->Form->end(__('Search'));?>
</table>
Controller
<?php
class StockReportsController extends AppController{
public function search(){
$year = $this->request->data['StockReport']['year'];
echo $year;
$store_no = $this->request->data['Store']['name'];
echo $store_no;
$dept_no = $this->request->data['StockReportCondition']['bu_no'];
echo $dept_no;
$result = $this->StockReport->find('all',array(
'conditions'=>array('yearweek'=>'', 'bu_no'=>'','tn_no'=>'')
));
}
}
It would seem that there's probably never a useful thing that you could do with the zero-based index of the year, and you'd only ever want the actual year value. If that's the case, then set up your options array so that the index and value are the same:
$years[$year]=$year;

Show data from multiple checkbox in codeigniter

i hope all of you can help me to solve my application problem
my problem is when i would like to show data from my multiple checkbox , data success to show but just showing my first data that i have check.
example is i check data 1 , data 2 , data 3, but the only data 1 are showing on my page.
my controller :
function comparison()
{
if ($this->input->post('submit')) {
foreach ($id_product = $this->input->post('id_product') as $rm) {
$show_compare = $this->Compare->start_compare($rm);
}
$data['comparison'] = $show_compare;
$data['title'] = "Comparison";
$data['meta_keywords'] = ". . .";
$data['meta_descriptions'] = ". . .";
$this->load->view('theme/comparison',$data);
}
}
My Model :
function start_compare($id_product)
{
$this->db->select('product.id_subcategory,product.type,product.product_name,specificcategory.specificcategory_name,specification_biostar.*');
$this->db->join('specification_biostar', 'specification_biostar.id_product = product.id_product', 'left');
$this->db->join('specificcategory', 'specificcategory.id_specificcategory = product.id_subcategory', 'left');
$this->db->where('product.id_product', $id_product);
$sql = $this->db->get('product')->result_array();
return $sql;
}
my view (option multiple-checkbox) :
<div class="box-body">
<input type="checkbox" name="id_product[]" id="txt" onClick="EnableSubmit3(this)" value="<?php echo $row['id_product']; ?>"><label>Choose</label>
</div>
my view (result data) :
<table class="table">
<?php foreach ($comparison as $row){ ?>
<tr>
<td colspan="2"><?php echo $row['id_product'] ?></td>
</tr>
<?php } ?>
</table>
in your code take $show_compare = array(); before foreach loop and use array_push in foreach.
$show_compare = array();
foreach ($id_product = $this->input->post('id_product') as $rm) {
array_push($show_compare,$this->Compare->start_compare($rm));
}
$data['comparison'] = $show_compare;

multiple users for shopping cart

currently I'm doing a bookstore system. almost similar with another shopping cart like amazon or whatsoever. my problem is how to apply multiple users to add product on cart . i do have login session and i have set MM_username for each user login. but when users add product into cart, it only appears name of one user.
here's the code below
if(isset($_POST["place_order"]))
{
$insert_order = "
INSERT INTO tbl_order(customer_id, creation_date, order_status)
VALUES(103, now(), 'pending')
";
$order_id = "";
if(mysqli_query($connect, $insert_order))
{
$order_id = mysqli_insert_id($connect);
}
$_SESSION["order_id"] = $order_id;
$order_details = "";
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
$order_details .= "
INSERT INTO tbl_order_details(order_id, product_name, product_price, product_quantity)
VALUES('".$order_id."', '".$values["product_name"]."', '".$values["product_price"]."', '".$values["product_quantity"]."');
";
}
if(mysqli_multi_query($connect, $order_details))
{
unset($_SESSION["shopping_cart"]);
echo '<script>alert("You have place an order...your order status will be inform through phone call within maximum 3 days..Thank you")</script>';
echo '<script>window.location.href="cart.php"</script>';
}
}
if(isset($_SESSION["order_id"]))
{
$customer_details = '';
$order_details = '';
$total = 0;
$query = '
SELECT * FROM tbl_order
INNER JOIN tbl_order_details
ON tbl_order_details.order_id = tbl_order.order_id
INNER JOIN tbl_customer
ON tbl_customer.CustomerID = tbl_order.customer_id
WHERE tbl_order.order_id = "'.$_SESSION["order_id"].'"
';
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result))
{
$customer_details = '
<label>'.$row["CustomerName"].'</label>
<p>'.$row["Address"].'</p>
<p>'.$row["City"].', '.$row["PostalCode"].'</p>
<p>'.$row["Country"].'</p>
';
$order_details .= "
<tr>
<td>".$row["product_name"]."</td>
<td>".$row["product_quantity"]."</td>
<td>".$row["product_price"]."</td>
<td>".number_format($row["product_quantity"] * $row["product_price"], 2)."</td>
</tr>
";
$total = $total + ($row["product_quantity"] * $row["product_price"]);
}
echo '
<h3 align="center">Order Summary for Order No.'.$_SESSION["order_id"].'</h3>
<div class="table-responsive">
<table class="table">
<tr>
<td><label>Customer Details</label></td>
</tr>
<tr>
<td>'.$customer_details.'</td>
</tr>
<tr>
<td><label>Order Details</label></td>
</tr>
<tr>
<td>
<table class="table table-bordered">
<tr>
<th width="50%">Product Name</th>
<th width="15%">Quantity</th>
<th width="15%">Price</th>
<th width="20%">Total</th>
</tr>
'.$order_details.'
<tr>
<td colspan="3" align="right"><label>Total</label></td>
<td>'.number_format($total, 2).'</td>
</tr>
</table>
</td>
yes i know i shouldn't have put 103 as customer_id and i know that is the reason it will only read 103 user, but i have tried many ways include filter MM_username, ' ', $value and whatsoever. but it didn't work.

Codeigniter Get Nested Hierarchical Data from Database

How can i get Hierarchical Data from db in Codeigniter. I read this :
http://www.sitepoint.com/hierarchical-data-database/
And i do good that but i cant optimize this tutorial with my model, controler and views
Default Category
|----- Sub category
| ----One more category
|----- Somthing else
I try but dont show sub category:
My model:
public function fetchChildren($parent, $level) {
$this->handler = $this->db->query("SELECT * FROM content_categories WHERE parent_id='".$parent."' ");
foreach($this->handler->result() as $row ) {
$this->data[$row->id] = $row;
//echo str_repeat(' ',$level).$row['title']."\n";
}
return $this->data;
}
Controller :
$this->data['node'] = $this->categories_model->fetchChildren(' ',0);
Views:
<table class="module_table">
<thead>
<tr>
<th><?php echo lang('categories_table_title'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($node as $row) : ?>
<tr>
<th> <?php echo str_repeat('|----', 0+1). $row->title ?> </th>
</tr>
<?php endforeach; ?>
</tbody>
</table>
And output is :
----Default
----Default
----Test Category 1
----Seccond Test Category 1
----Another Test Category 1
When i do this in model all work fine but when i try that to call in controler and loop in view i have result like above example:
This work onlu in model:
public function fetchChildren($parent, $level) {
$this->handler = $this->db->query("SELECT * FROM content_categories WHERE parent_id='".$parent."' ");
foreach($this->handler->result() as $row ) {
echo str_repeat('|-----',$level).$row->title."\n";
$this->fetchChildren($row->title, $level+1);
}
return $this->data;
}
And like output i have :
Default
|----Test Category 1
|----Seccond Test Category 1
|----Another Test Category 1
Any one have solution or example thanks.
Try storing the level value for each category.
In your model:
public function fetchChildren($parent, $level){
$this->handler = $this->db->query("SELECT * FROM content_categories WHERE parent_id='".$parent."' ");
foreach($this->handler->result() as $row ) {
$row->level = $level;
$this->data[] = $row;
$this->fetchChildren($row->title, $level+1);
}
return $this->data;
}
In your controller:
$this->data['node'] = $this->categories_model->fetchChildren(' ',0);
In your view
<table class="module_table">
<thead>
<tr>
<th><?php echo lang('categories_table_title'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($node as $row) : ?>
<tr>
<th><?php echo str_repeat('|----', $row->level). $row->title ?> </th>
</tr>
<?php endforeach; ?>
</tbody>
</table>

how to get result of query in view in codeigniter

I have use two query to get result on the view.
To get the result of resultq1 i use foreach but how can i get result of resultq2 in view.
For each row of resultq1 i get record "reccount" in resultq2.
//controller
//Query 1
$q = $this->db->select(array(
'spf.id as id' ,
'spf.name',
"if(u.username != '',u.username,spf.added_by) as added_by ",
'spf.added_on'
))->from('sp_forum spf')->join('sp_users u', 'spf.added_by = u.id', 'left')->where($where)->order_by('spf.id desc')->limit(10, $page * 10)->get();
$resultq1= $q->result_array();
$data['resultq1'] = $resultq1;
$resultq2 = array();
$i=0;
foreach($resultq1 as $rec)
{
//query 2
$id = $rec['id'];
$q1 = $this->db->select("count('id') as reccount ")->from('sp_forum_topic spft')->where('spft.forumid',$id)->get();
$resultq2[$i] = $q1->row_object();
$i++;
}
$this->load->view('forumview', $data, true);
//view file
<table width="100%">
<tr>
<td class="th"> Details</td>
<td width="5%" class="th"> Answer</td>
<td width="15%" class="th">Started by</td>
<td width="15%" class="th">Created on</td>
</tr>
<?php foreach($resultq1 as $row):?>
<tr>
<td><?php echo $row['name'] ;?></td>
<td >---- </td> // here i want to use resultq2
<td><?php echo $row['added_by'] ;?></td>
<td ><?php echo $row['added_on'];?></td>
</tr>
<?php endforeach;?>
</table>
Array print in view for resultq2.
Resultq1 have 4 rows so that i get 4 value in resultq2.
Array ( [0] => stdClass Object ( [reccount] => 0 ) [1] => stdClass Object ( [reccount] => 0 ) [2] => stdClass Object ( [reccount] => 0 ) [3] => stdClass Object ( [reccount] => 2 ) ) 0002
You need to pass $resultq2 to the view as well.
In the controller, just before calling the view
$data['resultq2'] = $resultq2
You can now use $resultq2 in your view.
PS - SQL queries should be in models, not controllers.
Just Write '$data['resultq2'] = $resultq2' before loading the view,
$data['resultq2'] = $resultq2
$this->load->view('forumview', $data, true);
And you can retrieve it in the view by $result2 variable.
Just do var_dump($result2); in your view file, You will get the complete data of $result2 varaible.
And xbonez is right, do write your DB queries in the model, controller is only for login, and coordinating between (models, views, libraries, helper etc...)
I optimized your second query a bit, but logic is pretty much the same.
$q = $this->db->select(array(
'spf.id as id' ,
'spf.name',
"if(u.username != '',u.username,spf.added_by) as added_by ",
'spf.added_on'
))->from('sp_forum spf')->join('sp_users u', 'spf.added_by = u.id', 'left')->where($where)->order_by('spf.id desc')->limit(10, $page * 10)->get();
$resultq1= $q->result_array();
$data['resultq1'] = $resultq1;
$ids = array();
foreach($resultq1 as $result)
$ids[] = $result['id'];
$resultq2 = $this->db->select("count('id') as reccount ")->from('sp_forum_topic spft')->where_in('spft.forumid',$ids)->get();
foreach( $resultq2->result_array() as $res )
$newArr[$res['forumid']] = $res;
$data['resultq2'] = $newArr;
After resultq2 is indexed by original id, its easy to use id from resultq1 loop to show correct data
<table width="100%">
<tr>
<td class="th"> Details</td>
<td width="5%" class="th"> Answer</td>
<td width="15%" class="th">Started by</td>
<td width="15%" class="th">Created on</td>
</tr>
<?php foreach($resultq1 as $row):?>
<tr>
<td><?php echo $row['name'] ;?></td>
<td >
<?php
print_r( $resultq2[$row['id']]);
?>
</td> // here i want to use resultq2
<td><?php echo $row['added_by'] ;?></td>
<td ><?php echo $row['added_on'];?></td>
</tr>
<?php endforeach;?>
</table>
This should fix it.
Modify the controller - see comments also
<?php
//Query 1
$q = $this->db->select(array(
'spf.id as id',
'spf.name',
"if(u.username != '',u.username,spf.added_by) as added_by ",
'spf.added_on'
))->from('sp_forum spf')->join('sp_users u', 'spf.added_by = u.id', 'left')->where($where)->order_by('spf.id desc')->limit(10, $page * 10)->get();
$resultq1 = $q->result_array();
$data['resultq1'] = $resultq1;
$resultq2 = array();
foreach ($resultq1 as $rec) {
//query 2
//echo $id = $rec['id']; //Test and See if all IDs echo out correctly
$data["id"] = $id; //Add this for the ID
$q1 = $this->db->select("count('id') as reccount, spft.forumid")->from('sp_forum_topic spft')->where('spft.forumid', $id)->get();
$resultq2[$id] = $q1->result(); //Change this line
$data["resultq2"][$id] = $resultq2[$id]; //Add this line
}
//echo "<pre>";
//$export = var_export($data, true);// Test the returned value...
//echo "</pre>";
$this->load->view('forumview', $data, true);
?>
In Your view
<table width="100%">
<tr>
<td class="th"> Details</td>
<td width="5%" class="th"> Answer</td>
<td width="15%" class="th">Started by</td>
<td width="15%" class="th">Created on</td>
</tr>
<?php //foreach ($resultq1 as $row): ?>
<?php foreach ($data as $row): ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td>
<?php
//$resultq2 is now array
//print_r($resultq2); //Do this so you can see the depth of the array
foreach ($resultq2 as $counts) {
foreach ($counts as $count) { //The second foreach might be necessary - please test
echo $count->reccount; //This should print out the count result
//echo "<br />";
}
}
?>
</td>
<td><?php echo $row['added_by']; ?></td>
<td ><?php echo $row['added_on']; ?></td>
<td >
</td>
</tr>
<?php
endforeach;
exit;
?>
</table>
I haven't had time to test it but it should work. Let me know if you find any issues:

Resources