multiple users for shopping cart - 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.

Related

How to pass table value to the modal?

I actually have projects and modules table. When i click button,
i want to get the data from model of that specific project.
Here is my code :
this is my table where i want to show the data of modules that represent specific project.
<table class="report">
<tr>
<th class="report-th"> Module ID </th>
<th class="report-th"> Module Name </th>
<th class="report-th"> Module Status </th>
</tr>
#if($modules->where('project_id','=',$project->id))
#foreach($modules as $module)
<tr>
<td>{{$module->id}}</td>
<td>{{$module->title}}</td>
<td>{{$module->status}}</td>
</tr>
#endforeach
#endif
</table>
below is the code in controller:
public function show()
{
$sortBy = 'id';
$sortDirection = 'ASC';
if (request('sortby') || request('sortdir')) {
$sortBy = request('sortby');
$sortDirection = request('sortdir');
}
$projects = projects::orderBy($sortBy, $sortDirection)->paginate(6);
$modules= modules::all();
return view('tms.projects', compact('projects','modules'));
}

sort by alphabetical field

I need alphabetical order of how the name from a list. What I need to do while I occupied the pager CakePHP but I could not be. I thought in order by the field (in this case the name) so DESC and ASC does not work me in any case.
In Controller :
$conditions = array('active !=' => -1, 'NOT' => array('role_id' => 2));
$users = $this->paginate('User', $conditions);
$this->set(compact('users','filter'));
In View :
<div class="row">
<table class="table table-striped table-hover table-condensed col-lg-12">
<thead>
<tr>
<th>#</th>
<th><?=$this->Paginator->sort('username', h(__('User')));?></th>
<th><?=$this->Paginator->sort('name');?></th>
<th><?=$this->Paginator->sort('email');?></th>
<th><?=$this->Paginator->sort('created', h(__('Registration date')));?></th>
<th><?=$this->Paginator->sort('active', h(__('Status')));?></th>
</tr>
</thead>
<tbody>
<?php
$offset = $this->Paginator->counter(array('format' => '{:start}'));
foreach($users as $user) {
echo '<tr onclick="window.location.href=\''.$this->Html->url(array('controller'=>'users','action'=>'view',
$user['User']['id'])).'\'">'.
'<td>'.($offset++).'</td>'.
'<td>'.h($user['User']['username']).'</td>'.
'<td>'.h($user['User']['name'].' '.$user['User']['father_surname'].' '.$user['User']['mother_surname']).'</td>'.
'<td>'.str_replace('#','[at]',h($user['User']['email'])).'</td>'.
'<td>'.$this->Time->format('d-m-Y H:i',strtotime($user['User']['created'])).'</td>'.
'<td>';
if ($user['User']['active'] == 1)
echo '<span class="label label-success">'.h(__('Active')).'</span>';
else
echo '<span class="label label-warning">'.h(__('Blocked')).'</span>';
echo '</td>'.
'</tr>';
}
?>
</tbody>
</table>
</div>
Add the following after your conditions
$this->User->order(
array('User.username ASC') //use whatever field you want to sort
);

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:

pass two query result to view in 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);

Resources