I am trying to limit the events shown to only the upcoming events, currently i am getting all events from the page displayed past and present. below is the code:
<?php
date_default_timezone_set('America/Chicago');
$fql = "SELECT eid, name, pic, start_time, end_time, location, description
FROM event WHERE eid IN ( SELECT eid FROM event_member WHERE uid = XXXXXXXXXXXX )
ORDER BY start_time asc";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $facebook->api($param);
foreach( $fqlResult as $keys => $values ){
$sdate = strtotime($values['start_time']);
$edate = strtotime($values['end_time']);
$start_date = date( 'l, m/d', $sdate );
$end_date = date( 'l, m/d', $edate );
if(empty ($edate)){
$edate = $sdate;
}
$start_time = date( 'g A', $sdate );
$end_time = date( 'g A', $edate );
$eventid = $values['eid'];
?>
How can this be done?
Use start_time > now().
SELECT eid, name, pic, start_time, end_time, location, description
FROM event WHERE eid IN ( SELECT eid FROM event_member WHERE uid = xxxxxxxx ) AND start_time > now()
ORDER BY start_time asc
Related
I am trying to download data from database to excel file. I have import the Maatwebsite\Excel from composer also.
my code in my controller is:
function excel()
{
$month = date('m');
DB::statement("CREATE OR REPLACE VIEW monthly_cost AS
select user_name, id, phone, sum(cost) as cost from (
select users.name as user_name, users.roll as id,users.phone as phone, BC.individual_cost as cost
from breakfast_orders BO, breakfast_costs BC,users
WHERE (BO.date=BC.date) and (BO.user_id=users.id)and MONTH(BO.date)='$month'
UNION ALL
select users.name as user_name, users.roll as id,users.phone as phone, LC.individual_cost as cost
from lunch_orders LO, lunch_costs LC,users
WHERE (LO.date=LC.date) and (LO.user_id=users.id) and MONTH(LO.date)='$month'
UNION ALL
select users.name as user_name, users.roll as id,users.phone as phone, DC.individual_cost as cost
from dinner_orders ddO, dinner_costs DC,users
WHERE (ddO.date=DC.date) and (ddO.user_id=users.id) and MONTH(ddO.date)='$month'
) x group by id,user_name,phone");
$customer_data=DB::table('monthly_cost')->get()->toArray();
$customer_array[] = array('Name', 'ID', 'Phone', 'Cost', 'Month');
foreach($customer_data as $customer)
{
$customer_array[] = array(
'Name' => $customer->user_name,
'ID' => $customer->id,
'Phone' => $customer->phone,
'Cost' => $customer->cost,
'Month' => $month
);
}
Excel::create('Customer Data', function($excel) use ($customer_array){
$excel->setTitle('Customer Data');
$excel->sheet('Customer Data', function($sheet) use ($customer_array){
$sheet->fromArray($customer_array, null, 'A1', false, false);
});
})->download('xlsx');
}
what should I do now?
Just run php artisan config:clear or php artisan config:cache.
I have a list of my topics by default it will get the topics where the reply_id is 0 And then arrange them by there date_created.
How every if there is a reply to one of the topics then that topic should go to the top of the list.
Question How can I make sure if there is a new reply to a topic then that topic will go to list.
Currently my var dump as you can see have two items how ever the 2nd array item should be at top because it has a new last post time()
Array
(
[0] => Array
(
[title] => How to
[author] => demo
[author_post_time] => Tue Jun 13 , 2017 19:43:01 pm
[last_post_by] => demo
[latest_post_time] => Tue Jun 13 , 2017 19:43:01 pm
)
[1] => Array
(
[title] => How to install codeigniter
[author] => admin
[author_post_time] => Sat Jun 10 , 2017 23:26:48 pm
[last_post_by] => demo
[latest_post_time] => Tue Jun 13 , 2017 20:48:00 pm
)
)
Image of database
Model
<?php
class Topic_model extends CI_Model
{
public function get_active_topics($fid)
{
$data = array();
$this->db->select('t.*, u.username');
$this->db->from('topics t');
$this->db->join('user u', 'u.user_id = t.user_id');
$this->db->where('t.reply_id', '0');
$this->db->order_by('t.date_created', 'desc');
$query = $this->db->get();
foreach ($query->result_array() as $topic_result)
{
$reply = $this->get_reply($topic_result['topic_id']);
// This $date1 just for display only as shown on image
$date1 = date('D M d', $topic_result['date_created']) . ' , ' . date('Y H:i:s a', $topic_result['date_created']);
// This $date2 just for display only as shown on image
$date2 = date('D M d', $reply['date_created']) . ' , ' . date('Y H:i:s a', $reply['date_created']);
$data[] = array(
'title' => $topic_result['title'],
'author' => $topic_result['username'],
'author_post_time' => $date1,
'last_post_by' => isset($reply['username']) ? $reply['username'] : $topic_result['username'],
'latest_post_time' => ($reply['reply_id'] > 0) ? $date2 : $date1,
);
}
return $data;
}
public function get_reply($reply_id)
{
$this->db->select('t.*, u.username');
$this->db->from('topics t');
$this->db->join('user u', 'u.user_id = t.user_id');
$this->db->where('t.reply_id', $reply_id);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row_array();
}
return false;
}
}
Please try to use below mentioned line, I hope this will fix your issue.
Change From
$this->db->order_by('t.date_created', 'desc');
TO
$this->db->order_by('t.reply_id DESC, t.date_created DESC');
Try ordering your table first by date2 and if that doesnt exist (no reply) then date1. You seem to have it implemented already, you're just ordering by date created on the post.
can anyone frame this sub query in yii2???
select name, crt_by, (select name from tbl_employee_master
where tbl_employee_master.contact = (select username from tbl_user
where tbl_user.id = tbl_dealer_master.crt_by)) as
employee, district, contact_person, contact,
(select count(*) from tbl_dealer_post
where tbl_dealer_post.fk_user_id = tbl_dealer_master.id)
as post_count from tbl_dealer_master
where status=1
Help is appreciable!!
Thanks in advance!!
Here is activequery you can use ..
$query= (new Query())->select([
'name' ,
'crt_by' ,
'employee' => (new Query())->select('name')
->from('tbl_employee_master')
->where([
'=','tbl_employee_master.contact', (new Query())
->select('username')
->from('tbl_user')
->where('tbl_user.id=tbl_dealer_master.crt_by')
]),
'district',
'contact_person',
'contact',
'post_count' => (new Query())->select('count(*)')
->from('tbl_dealer_post')
->where('tbl_dealer_post.fk_user_id=tbl_dealer_master.id')
])->from('tbl_dealer_master')->where(['status' => 1]);
This is the complete solution to the given problem!!
$query= (new Query())->select([
'name' ,
'crt_by' ,
'employee' => (new Query())->select('name')
->from('tbl_employee_master')
->where([
'=','tbl_employee_master.contact', (new Query())
->select('username')
->from('tbl_user')
->where('tbl_user.id=tbl_dealer_master.crt_by')
]),
'district',
'contact_person',
'contact',
'post_count' => (new Query())->select('count(*)')
->from('tbl_dealer_post')
->where('tbl_dealer_post.fk_user_id=tbl_dealer_master.id')
])->from('tbl_dealer_master')->where(['status' => 1]);
I have followed How to create a custom grid from scratch to create custom Sales Orders. Admin is creating vendors and vendors will be uploading product. I want to restrict vendors such that they can see orders placed on their own product only.
There is one new model jbmarketplace/jbmarketplaceproducts in which vendors user_id and product_id is being stored when vendor creates product. But when I'm filtering it gives SQLSTATE[42S22]: Column not found: 1054 Unknown column 'product_id' in 'where clause'. But product_id is available in sales_flat_order_item table.
This problem is Fixed. Updated Code
protected function _prepareCollection()
{
// Get current logged in user
$current_user = Mage::getSingleton( 'admin/session' )->getUser();
// Limit only for vendors
if ( $current_user->getRole()->getRoleId() == Mage::getStoreConfig( 'jbmarketplace/jbmarketplace/vendors_role' ) ) {
// echo( $current_user->getUserId());
$my_products = Mage::getModel( 'jbmarketplace/jbmarketplaceproducts' )
->getCollection()
->addFieldToSelect( 'product_id' )
->addFieldToFilter( 'user_id', $current_user->getUserId() )
->load();
$my_product_array = array();
foreach ( $my_products as $product ) {
$my_product_array[] = $product->getProductId();
$entity = Mage::getModel('sales/order_item')
->getCollection()
->addFieldToSelect('order_id')
->addFieldToFilter('product_id',$my_product_array)
->load();
// echo $entity->getSelect();// will print sql query
}
$d=$entity->getData();
if($d){
$collection = Mage::getResourceModel('sales/order_collection')
// My code
->addFieldToFilter('entity_id', $d)
->join(array('a' => 'sales/order_address'), 'main_table.entity_id = a.parent_id AND a.address_type != \'billing\'', array(
'city' => 'city',
'country_id' => 'country_id'
))
// ->join(Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar', 'main_table.products_id ='.Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar.entity_id',array('value'))
->join(array('c' => 'customer/customer_group'), 'main_table.customer_group_id = c.customer_group_id', array(
'customer_group_code' => 'customer_group_code'
))
->addExpressionFieldToSelect(
'fullname',
'CONCAT({{customer_firstname}}, \' \', {{customer_lastname}})',
array('customer_firstname' => 'main_table.customer_firstname', 'customer_lastname' => 'main_table.customer_lastname'))
->addExpressionFieldToSelect(
'products',
'(SELECT GROUP_CONCAT(\' \', x.name)
FROM sales_flat_order_item x
WHERE {{entity_id}} = x.order_id
AND x.product_type != \'configurable\')',
array('entity_id' => 'main_table.entity_id')
)
;
parent::_prepareCollection();
$this->setCollection($collection);
return $this;
}
else
{
echo("Current there are no purchases on your product. Thank you");
}
}
else{
echo("Please Login as Vendor and you will see orders on your products.<br>");
// $current_user = Mage::getSingleton( 'admin/session' )->getUser()->getUserId();
// echo($current_user);
}
}
Here is the code which worked for me.
protected function _prepareCollection()
{
// Get current logged in user
$current_user = Mage::getSingleton( 'admin/session' )->getUser();
// Limit only for vendors
if ( $current_user->getRole()->getRoleId() == Mage::getStoreConfig( 'jbmarketplace/jbmarketplace/vendors_role' ) ) {
// echo( $current_user->getUserId());
$my_products = Mage::getModel( 'jbmarketplace/jbmarketplaceproducts' )
->getCollection()
->addFieldToSelect( 'product_id' )
->addFieldToFilter( 'user_id', $current_user->getUserId() )
->load();
$my_product_array = array();
foreach ( $my_products as $product ) {
$my_product_array[] = $product->getProductId();
$entity = Mage::getModel('sales/order_item')
->getCollection()
->addFieldToSelect('order_id')
->addFieldToFilter('product_id',$my_product_array)
->load();
// echo $entity->getSelect();// will print sql query
}
$d=$entity->getData();
if($d){
$collection = Mage::getResourceModel('sales/order_collection')
// My code
->addFieldToFilter('entity_id', $d)
->join(array('a' => 'sales/order_address'), 'main_table.entity_id = a.parent_id AND a.address_type != \'billing\'', array(
'city' => 'city',
'country_id' => 'country_id'
))
// ->join(Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar', 'main_table.products_id ='.Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar.entity_id',array('value'))
->join(array('c' => 'customer/customer_group'), 'main_table.customer_group_id = c.customer_group_id', array(
'customer_group_code' => 'customer_group_code'
))
->addExpressionFieldToSelect(
'fullname',
'CONCAT({{customer_firstname}}, \' \', {{customer_lastname}})',
array('customer_firstname' => 'main_table.customer_firstname', 'customer_lastname' => 'main_table.customer_lastname'))
->addExpressionFieldToSelect(
'products',
'(SELECT GROUP_CONCAT(\' \', x.name)
FROM sales_flat_order_item x
WHERE {{entity_id}} = x.order_id
AND x.product_type != \'configurable\')',
array('entity_id' => 'main_table.entity_id')
)
;
parent::_prepareCollection();
$this->setCollection($collection);
return $this;
}
else
{
echo("Current there are no purchases on your product. Thank you");
}
}
else{
echo("Please Login as Vendor and you will see orders on your products.<br>");
// $current_user = Mage::getSingleton( 'admin/session' )->getUser()->getUserId();
// echo($current_user);
}
}
For starters, the Codeigniter documentation on update_batch does not exist. kenjis was kind enough to provide some documentation and submit it to the repository. Hopefully they pull it soon.
Does anyone know how to add multiple where conditions to Codeigniters update_batch command?
My Desired Use:
$where = array(
'title',
'name'
);
$this->db->update_batch('mytable', $data, $where);
When I tried this code I got the follow error:
A Database Error Occurred
One or more rows submitted for batch updating is missing the specified index.
Filename: C:\wamp\www\wheel\system\database\DB_active_rec.php
Line Number: 1451
Update Batch Documentation by kenjis:
$this->db->update_batch();
Generates an update string based on the data you supply, and runs the query. You can either pass an array or an object to the function. Here is an example using an array:
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name 2' ,
'date' => 'My date 2'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name 2' ,
'date' => 'Another date 2'
)
);
$this->db->update_batch('mytable', $data, 'title');
// Produces:
// UPDATE `mytable` SET `name` = CASE
// WHEN `title` = 'My title' THEN 'My Name 2'
// WHEN `title` = 'Another title' THEN 'Another Name 2'
// ELSE `name` END,
// `date` = CASE
// WHEN `title` = 'My title' THEN 'My date 2'
// WHEN `title` = 'Another title' THEN 'Another date 2'
// ELSE `date` END
// WHERE `title` IN ('My title','Another title')
The first parameter will contain the table name, the second is an associative array of values, the third parameter is the where key.
Sources:
kenjis's documentation update:
https://bitbucket.org/kenjis/ci-user-guide/changeset/3d579dd14afe
saintnicster's pull request: https://github.com/EllisLab/CodeIgniter/pull/448
You can't add multiple where clauses to update_batch(). It only accepts a string as the third parameter for the where clause so I'm sure there's no way to do this the way the method is currently written.
From the source:
/**
* Update_Batch
*
* Compiles an update string and runs the query
*
* #param string the table to retrieve the results from
* #param array an associative array of update values
* #param string the where key
* #return object
*/
public function update_batch($table = '', $set = NULL, $index = NULL)
I am using codeigniter 3.1.5 and had the same problem, but I solved my problem as follows:
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name 2' ,
'date' => 'My date 2'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name 2' ,
'date' => 'Another date 2'
)
);
$this->db->where('name','My Name 2');
$this->db->update_batch('mytable', $data, 'title');
Produces it:
// Produces:
// UPDATE `mytable`
// SET `name` = CASE
// WHEN `title` = 'Another title' THEN 'Another Name 2'
// WHEN `title` = 'My title' THEN 'My Name 2'
// ELSE `name`
// END,
// `date` = CASE
// WHEN `title` = 'My title' THEN 'My date 2'
// WHEN `title` = 'Another title' THEN 'Another date 2'
// ELSE `date`
// END
// WHERE `title` IN ('My title','Another title')
// AND `name` = 'My Name 2'
UPDATE
I had a problem trying to add more than 100 records with update_batch, for example:
$data = [1=>a,2=>b ... 200=>zz];
First call (with WHERE):
// Produces:
// UPDATE `mytable`
// SET `name` = CASE
// WHEN `title` = 'My title' THEN 'My Name 2'
// WHEN `title` = 'Another title' THEN 'Another Name 2'
// ELSE `name`
// END,
// `date` = CASE
// WHEN `title` = 'My title' THEN 'My date 2'
// WHEN `title` = 'Another title' THEN 'Another date 2'
// ELSE `date`
// END
// WHERE `title` IN ('My title','Another title')
// AND `name` = 'My Name 2'
Second call on (Without WHERE):
// Produces:
// UPDATE `mytable`
// SET `name` = CASE
// WHEN `title` = 'My title' THEN 'My Name 2'
// WHEN `title` = 'Another title' THEN 'Another Name 2'
// ELSE `name`
// END,
// `date` = CASE
// WHEN `title` = 'My title' THEN 'My date 2'
// WHEN `title` = 'Another title' THEN 'Another date 2'
// ELSE `date`
// END
// WHERE `title` IN ('My title','Another title')
Try this:
$chunk1 = array_chunk($data,100);
for($i=0;$i < count($chunk1);$i++) {
$this->upload_model->update_data($chunk1[$i],'My Name 2');
}
Model:
public function update_data($data='',$name=''){
$this->db->where('name',$name);
$this->db->update_batch('mytable', $data, 'title');
}
Multiple where conditions are broken in update_batch because the WHERE query is being cleared in the batch loop.
Here is the batch update loop:
for ($i = 0, $total = count($this->qb_set_ub); $i < $total; $i += $batch_size)
{
if ($this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set_ub, $i, $batch_size), $index)))
{
$affected_rows += $this->affected_rows();
}
$this->qb_where = array();
}
Notice that the passed WHERE conditions are cleared by $this->qb_where = array();.
In CodeIgniter v3.1.10, the offending line is on 1940 in DB_query_builder.php. This produces a very unexpected behavior where WHERE conditions work for the first batch processed (default 100) and fail for subsequent batches.
There are two possible solutions:
Use the 4th batch_size parameter of update_batch and pass a large number such as 100,000 so all the queries are processed in the first batch and the WHERE condition is not cleared.
Update the offending line to restore the initial WHERE conditions.
Code for Solution #2:
// Save initial where conditions.
$where_holder = $this->qb_where;
// Batch this baby
$affected_rows = 0;
for ($i = 0, $total = count($this->qb_set_ub); $i < $total; $i += $batch_size)
{
if ($this->query($this->_update_batch($this->protect_identifiers($table, TRUE, NULL, FALSE), array_slice($this->qb_set_ub, $i, $batch_size), $index)))
{
$affected_rows += $this->affected_rows();
}
// Restore intial where conditions.
$this->qb_where = $where_holder;
}
Hope this helped!
Try this one also !
Suppon you have import data through csv/excel
then store all record in single array like:
Array
(
[0] => Array
(
[price] => 100.00
[part_no] => PD001
[brand] => 44
[special_price] => 90.10
)
[1] => Array
(
[price] => 200.00
[part_no] => PD002
[special_price] => 150.00
)
)
Step 2:
Call to model
$model = new CatalogModel();
$result = $model->batchUpdateData($final_array);
function batchUpdateData($array = array()){
$query = $this->db->table('product');
$price = array();
$part_no = array();
$special = array();
$brand = array();
if (!empty($array)) {
foreach ($array as $key => $value) {
$price[] = $value['price'];
$part_no[] = $value['part_no'];
$special[] = $value['special_price'];
$brand[] = $value['brand'];
}
$num = count($part_no);
$sql ="UPDATE product SET ";
// price colum update
$sql .=" price = CASE ";
for($i=0; $i < $num; $i++){
$sql .=" WHEN part_no = '".$part_no[$i]."' AND brand = '".$brand[$i]."' THEN '".$price[$i]."' ";
}
$sql .="ELSE price END ,";
// special colum update
$sql .=" special_price = CASE ";
for($i=0; $i < $num; $i++){
$sql .=" WHEN part_no = '".$part_no[$i]."' AND brand = '".$brand[$i]."' THEN '".$special[$i]."' ";
}
$sql .="ELSE special_price END";
$sql .=" WHERE part_no IN ('" . implode("','", $part_no) . "') ";
return $this->db->query($sql);
}
}
This will product query like:
UPDATE `product` SET `price` = CASE WHEN `part_no` = 'PD001' and `brand` = '44' THEN '100.00' WHEN `part_no` = 'PD002' and `brand` = '44' THEN '200.00' ELSE `price` END
WHERE `part_no` IN('PD001','PD002');
If this helpfull give a thumbs up