I am trying to get details from table1 and table2 on basis of table3 id array.
table3 id and table1 ids are availale in table2.
// $ids is a array of ids like: Array ( [ids] => 10,11 )
function get_detail($ids) {
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table1.aID = table2.aID');
$this->db->join('table3', 'table2.bID = table3.bID','left');
$this->db->where('table3.bID', $ids);
$query = $this->db->get();
}
When you have multiple values you should use IN
Replace
$this->db->where();
With
$this->db->where_in();
Both takes (col, value);
Note : // $ids = Array ( 10,11 );
Hello you use where_in like this
function get_detail($ids) {
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table1.aID = table2.aID');
$this->db->join('table3', 'table2.bID = table3.bID','left');
$this->db->where_in('table3.bID', $ids);
$query = $this->db->get();
}
Note : Pass $ids in array format
$ids = Array (10,11);
you can use an array
$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);
$this->db->where($array);
Related
How can I write this procedural query as codeigniter active record query (update_batch mode )
UPDATE products SET current_stock = current_stock + 1 where product_id = 1
You can use update_batch like this:
$products = $this->db->get('products'))->result();
for( $i = 0; $i < count($products); $i++){
$data[] = array(
'product_id' => $product[$i]->product_id,
'current_stock' => $product[$i]->current_stock + 1
);
}
$this->db->update_batch('products', $data, 'product_id');
You can use below query as explained on Codeigniter Query Builder Class:
$this->db->set('current_stock', 'current_stock+1', FALSE);
$this->db->where('product_id', 2);
$result = $this->db->update('products');
I have a form that I am using to allow people to input data and then upload a preset picture. I want to label that picture as the form_id. I am able to run a LAST_INSERT_ID() query and display it after I insert the form in my view but I can't seem to echo that information out anywhere else. I need the query to select_last_id to run after my $update query or the ID number will be off. Could anyone assist in helping me pass just the value of the row to my view? Here is the code from my controller.
function inspection() {
if($this->input->post('submit')) {
$array = array(
'trlr_num' => $this->input->post('trlr_num'),
'seal' => $this->input->post('seal'),
'damaged' => $this->input->post('damaged'),
'truck_num' => $this->input->post('truck_num'),
'driver_name' => $this->input->post('driver_name'),
'car_code' => $this->input->post('car_code'),
'origin' => $this->input->post('origin'),
'lic_plate' => $this->input->post('lic_plate'),
'del_note' => $this->input->post('del_note'),
'live_drop' => $this->input->post('live_drop'),
'temp' => $this->input->post('temp'),
'level' => $this->input->post('level'),
'ship_num' => $this->input->post('ship_num'),
'trlr_stat' => $this->input->post('trlr_stat'),
'comment' => $this->input->post('comment')
);
$update = $this->trailer_model->insert_form($array);
$query = $this->trailer_model->select_last_id();
$result =& $query->result_array();
$this->table->set_heading('ID');
$data['table'] = $this->table->generate_table($result);
unset($query,$result);
}
$level = $this->trailer_model->select_fuel_level();
$result = $level->result_array();
$data['options'] = array();
foreach($result as $key => $row) {
$data['options'][$row['level']] = $row['level'];
}
unset($query,$result,$key,$row);
$data['label_display'] = 'Fuel Level';
$data['field_name'] = 'level';
$status = $this->trailer_model->select_trailer_type();
$result = $status->result_array();
$data['options1'] = array();
foreach($result as $key => $row) {
$data['options1'][$row['trlr_stat']] = $row['trlr_stat'];
}
unset($query,$result,$key,$row);
$data['label_display1'] = 'Trailer Status';
$data['field_name1'] = 'trlr_stat';
$data['page_title'] = 'Trailer Inspection';
$data['main_content'] = 'dlx/inspection/trailer_inspection_view';
return $this->load->view('includes/template',$data);
}
}
Anything you want to display on a view, you must pass to it.
When you're setting $data, you missed adding $query to it.
Controller:
I understand that $query = $this->trailer_model->select_last_id(); is returning just an ID, so:
$data['last_id'] = $query;
But if $query = $this->trailer_model->select_last_id(); does not return just an ID, but an array or something else, you shall include to return the ID in your model method. Actually, it'd be crucial to know what $this->trailer_model->select_last_id(); is returning.
View:
echo $last_id; -> must echo the last ID, if select_last_id() method from your model returns what it has to return.
i have order table where in product_id is string Like 10,11,12,13. And have Product table with this id.
how to get dataProvider with each product_id
My code is
public function getProducts($id){
$idarray = explode(',', $id);
$dataProviderProduct = Array();
foreach($idarray as $i=>$id){
$dataProviderProduct[$i]=new CActiveDataProvider('Product',
array( 'criteria'=>array(
'condition'=>'id=:id',
'params'=>array(':id' => $id),
),
'pagination'=>array( 'pageSize'=>10),
)
);
}
return $dataProviderProduct;
}
But this is wrong code
Use addInCondition like this:
$idarray = explode(',', $id);
$criteria = new CDbCriteria();
$criteria->addInCondition('id', $idarray);
$data = ModelName::model()->findAll($criteria);
How to create a universal method in PHP for determine the number of rows in mysql?
Someone please help? Thank you.
My method (does not work)
public function countRow($table, $array)
{
$bef = implode(', ', array_keys($array));
$aft = implode(', ', array_fill(0, sizeof($array), '=?'));
$sql = 'SELECT '.$bef.' FROM '.$table.' WHERE '.$aft.' LIMIT 1';
$stmt = $pdo->prepare($sql);
$stmt->execute(array_values($array));
return $stmt->rowCount();
}
Example array (3 key and 3 value)
$array = array('name' => $_POST['name'],
'pass' => secure($_POST['pass']),
'status' => 0);
Or example array (1 key and 1 value)
$array = array('single' => $_POST['one']);
Call method
$this->countRow('tablename', $array);
I have a custom post type "news" in my Wordpress site. I am using Advanced Custom Fields plugin to add meta data to each post.
I want to create an array of news items as an archive:
[2013]
[January] => 5
[2012]
[January] => 20
[February] => 10
[2011]
[April] => 30
I managed to get this working using:
global $wpdb;
$news = $wpdb->get_results(
"SELECT wp_posts.post_date, COUNT(wp_posts.ID) as count
FROM $wpdb->posts
WHERE
wp_posts.post_type = 'news' AND
wp_posts.post_status = 'publish' AND
wp_posts.post_date <= CURDATE() AND
wp_posts.post_date >= DATE_SUB(CURDATE(), INTERVAL 3 YEAR)
GROUP BY YEAR(wp_posts.post_date), MONTH(wp_posts.post_date)
ORDER BY wp_posts.post_date DESC",
ARRAY_A);
$archive = array();
foreach ($news as $post):
$year = date('Y', strtotime($post['post_date']));
$month = date('m', strtotime($post['post_date']));
$month_name = date('F', strtotime($post['post_date']));
$post['url'] = 'NOT SURE ABOUT URL';
$archive[$year][$month_name] = $post;
endforeach;
I need to be able to link to specific years and months using http://example.com/2012/ and http://example.com/2012/10/.
As this involves a custom post type "news" I'm unsure how to make this happen?
In order to do what you require, you need to modify Wordpress rewrites to be able to capture the year/month/etc post data for a custom post type.
You may do this with the following code:
/**
* Custom post type date archives
*/
/**
* Custom post type specific rewrite rules
* #return wp_rewrite Rewrite rules handled by Wordpress
*/
function cpt_rewrite_rules($wp_rewrite) {
$rules = cpt_generate_date_archives('news', $wp_rewrite);
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
return $wp_rewrite;
}
add_action('generate_rewrite_rules', 'cpt_rewrite_rules');
/**
* Generate date archive rewrite rules for a given custom post type
* #param string $cpt slug of the custom post type
* #return rules returns a set of rewrite rules for Wordpress to handle
*/
function cpt_generate_date_archives($cpt, $wp_rewrite) {
$rules = array();
$post_type = get_post_type_object($cpt);
$slug_archive = $post_type->has_archive;
if ($slug_archive === false) return $rules;
if ($slug_archive === true) {
$slug_archive = $post_type->name;
}
$dates = array(
array(
'rule' => "([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})",
'vars' => array('year', 'monthnum', 'day')),
array(
'rule' => "([0-9]{4})/([0-9]{1,2})",
'vars' => array('year', 'monthnum')),
array(
'rule' => "([0-9]{4})",
'vars' => array('year'))
);
foreach ($dates as $data) {
$query = 'index.php?post_type='.$cpt;
$rule = $slug_archive.'/'.$data['rule'];
$i = 1;
foreach ($data['vars'] as $var) {
$query.= '&'.$var.'='.$wp_rewrite->preg_index($i);
$i++;
}
$rules[$rule."/?$"] = $query;
$rules[$rule."/feed/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i);
$rules[$rule."/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i);
$rules[$rule."/page/([0-9]{1,})/?$"] = $query."&paged=".$wp_rewrite->preg_index($i);
}
return $rules;
}
You will notice that I have hard-coded news in to the $rules = cpt_generate_date_archives('news', $wp_rewrite); portion of the code. You can change this as needed.
With this code, you should be able to navigate to http://yoursite.com/news/2013/02/ and receive an archive listing for that specific post type for that specific time.
For completeness, I will include how to generate a monthly archive widget in order to utilize this.
/**
* Get a montlhy archive list for a custom post type
* #param string $cpt Slug of the custom post type
* #param boolean $echo Whether to echo the output
* #return array Return the output as an array to be parsed on the template level
*/
function get_cpt_archives( $cpt, $echo = false )
{
global $wpdb;
$sql = $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_type = %s AND post_status = 'publish' GROUP BY YEAR($wpdb->posts.post_date), MONTH($wpdb->posts.post_date) ORDER BY $wpdb->posts.post_date DESC", $cpt);
$results = $wpdb->get_results($sql);
if ( $results )
{
$archive = array();
foreach ($results as $r)
{
$year = date('Y', strtotime( $r->post_date ) );
$month = date('F', strtotime( $r->post_date ) );
$month_num = date('m', strtotime( $r->post_date ) );
$link = get_bloginfo('siteurl') . '/' . $cpt . '/' . $year . '/' . $month_num;
$this_archive = array( 'month' => $month, 'year' => $year, 'link' => $link );
array_push( $archive, $this_archive );
}
if( !$echo )
return $archive;
foreach( $archive as $a )
{
echo '<li>' . $a['month'] . ' ' . $a['year'] . '</li>';
}
}
return false;
}
To use this function just supply the slug of the custom post type, ie: get_cpt_archives( 'news' ). This will return an array of unique Year/Dates/Links, ie:
Array
(
[0] => Array
(
[month] => February
[year] => 2013
[link] => http://yoursite.com/news/2013/02
)
[1] => Array
(
[month] => January
[year] => 2013
[link] => http://yoursite.com/news/2013/01
)
)
You can loop through these with a foreach and output them however you want.
Alternatively, you can use get_cpt_archives( 'news', true ) which will automatically echo each item wrapped in an <li> linking to its specific archive.
The formatting of the output is not exactly what you wanted, so you will have to tweak it a bit to get it to display in the
Year
Month
Month
Year
Month
format that you require.
I hope this helps.
I know this is an old post, but there is a much more succinct way of doing what the OP is asking:
function cpt_add_rewrite_rules()
{
$cpt = 'news';
add_rewrite_rule($cpt.'/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})?$',
'index.php?post_type='.$cpt.'&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]',
'top');
add_rewrite_rule($cpt.'/([0-9]{4})/([0-9]{1,2})?$',
'index.php?post_type='.$cpt.'&year=$matches[1]&monthnum=$matches[2]',
'top');
add_rewrite_rule($cpt.'/([0-9]{4})?$',
'index.php?post_type='.$cpt.'&year=$matches[1]',
'top');
}
add_filter('init', 'cpt_add_rewrite_rules');