CodeIgniter display last id - php

I want to display as a value of textbox the last id of my record and then increment it.
This is what I have:
view.php
<?php
$id = $this->db->insert_id();
$newId = $id + 1;
$data = array(
'name' => 'customercode',
'id' => 'inputCustomerCode',
'type' => 'text',
'readonly' => 'true',
'class' => 'form-control',
'value' => 'CUST0000' . $newId
);
echo form_input($data);
?>
But it's just displaying CUST00001? What am I doing wrong in here? Help is much appreciated and needed. Thanks.

you can get last inserted id with $this->db->insert_id(); if you want to use this for multiple places, please take this in a variable and use that variable.
you can use $this->db->insert_id() after insert query.
$id = $this->db->insert_id();
you can use a simple query to get max id
$query = $this->db->query("SELECT MAX (id) FROM table");
$id = $query->row()->id;
and use $id in form
'value' => 'CUST0000' . $id + 1

Instead of using $id = $this->db->insert_id(); you may use following code:
$query = $this->db->query("SELECT id FROM mytable ORDER BY id DESC LIMIT 1");
$id = $query->row()->id;
$newId = $id + 1;
$data = array(
'name' => 'customercode',
'id' => 'inputCustomerCode',
'type' => 'text',
'readonly' => 'true',
'class' => 'form-control',
'value' => 'CUST' . str_pad($id+1, 5, '0', STR_PAD_LEFT)
);
echo form_input($data);

Instead of
$this->db->insert_id()+1
You need to display $newId.
Make sure you have queried for the last Id. $this->db->insert_id() returns only when you have inserted some record. If you haven't , then you need to query to get the last inserted id. You can use this simple query.
SELECT id FROM mytable ORDER BY id DESC LIMIT 1

$this->db->insert_id()
The insert ID number when performing database inserts.
Looked at http://ellislab.com/codeigniter/user_guide/database/helpers.html and the first function is $this->db->insert_id();
This also works with active-record inserts...

Related

how can i retrieve id using this method? - codeignitor db

The given syntax can insert the given values but i cannot retrieve the value with method. do anyone knoe how can i do that?
$supplier = array(
'cname' => $request->getPost('cname'),
'clname' => $request->getPost('clname'),
'tlidt' => $request->getPost('tlidt'),
'tledt' => $request->getPost('tledt'),
'pname' => $request->getPost('pname'),
'tla' => $request->getPost('tla'),
);
$builder = $db->table('suppliers');
$builder->insert($supplier);
$id = $this->$db->insert_id();
Try inserting like that
$this->db->insert('suppliers',$supplier);
$id = $this->db->insert_id();
or try removing $ sign from your last line 'db'.

Codeigniter, mysql, select_max and add 1 before inserting another record

When I insert a new record into a database table, I need to take an existing previous value of a column called el_order, add +1, and use that new el_order+1 to insert the new record with that value in the column.
I can't use autoincrement because I need to do some things with that column (reorder, move, etc) and have to use it as an integer.
Table
ID name el_order
1 1 1
21 bla 2
2 2 3
--NEW-- --NEW-- 3+1 (NEW)
I add a new record, and need to insert it with 3+1 in it's el_order column...
I have tried this, but no luck:
$this->db->select_max('el_order');
$res = $this->db->get('elem_diccio');
$eldi_key = url_title($this->input->post('id'), 'underscore', TRUE);
$el_order = $res+1;
$datos = array(
'ID' => $id,
'el_order' => $el_order,
'name' => $this->input->post('name'),
);
$this->db->insert('elem_diccio', $datos);
Just like this
$this->db->select_max('el_order');
$res = $this->db->get('elem_diccio')->row()->el_order;
$eldi_key = url_title($this->input->post('id'), 'underscore', TRUE);
$el_order = $res+1;
$datos = array(
'ID' => $id,
'el_order' => $el_order,
'name' => $this->input->post('name'),
);
$this->db->insert('elem_diccio', $datos);
$res is a CI_DB_mysqli_result Object. To get the column, you need
$this->db->select_max('el_order');
$res = $this->db->get('elem_diccio')->row();
$el_order = $res->el_order+1;
$datos = array(
'ID' => $id,
'el_order' => $el_order,
'name' => $this->input->post('name'),
);

Wordpress update query not working

I have an update query which is not working for me. I am able to make selects quite happily on the same page but I cannot get an update statement to work.
The table is not a part of wordpress so Im wondering if that could be it or if I have just got something wrong.
$query = "UPDATE login_count SET `count` = '100' WHERE `user_id` = $userID ";
$insrt = $wpdb->query($query);
Try this,
$insrt = $wpdb->update(
'login_count', //table_name
array('count'=>'100'),//data
array('user_id'=>$userID),//where
array('%s'),//data format
array('%s')
);
$insrt = $wpdb->update(
'login_count', //table_name
array(
'count' => '100', // string
),
array( 'user_id' => $user_id ), //Where Condition
array(
'%d', // value1
),
array( '%d' )
);
Try this ..user_id = {$userID} ";
Edit
See: Use a $variable inside a SQL string?

How to get single element from $row[id]

I'm working on a project for PHPBB. I have a problem that I get some $row types. And they have some "id"s from MySQL, But I want theese "id"s by one by.
It's SQL query ;
$sql = $db->sql_build_query('SELECT', array(
'SELECT' => 'u.*, z.friend, z.foe, p.*',
'FROM' => array(
USERS_TABLE => 'u',
POSTS_TABLE => 'p',
),
'LEFT_JOIN' => array(
array(
'FROM' => array(ZEBRA_TABLE => 'z'),
'ON' => 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id'
)
),
'WHERE' => $db->sql_in_set('p.post_id', $post_list) . '
AND u.user_id = p.poster_id'
));
$result = $db->sql_query($sql);
And then for user_id's
$rowset[$row['post_id']] = array(
'hide_post' => ($row['foe'] && ($view != 'show' || $post_id != $row['post_id'])) ? true : false,
'user_id' => $row['user_id'],
.
.
I logged $row[user_id] for in a topic and it return ;
97777
97778
97779
97783
But I want just first id i mean -> "97777" . So how can i pass just "97777" to a variable?
Dont suggest about sql level because I can't change any SQL command.
Dont suggest about sql level because I can't change any SQL command.
This is the problem though, your PHP is clearly selecting all 9 rows;
Using WHERE user_id = '97777' you will only select the record you want.
Why can you not edit the SQL? aren't you familiar with SQL or is the code your are using your own?
Without any code it is hard to say what you want to do:
reset($row);
$first_key = key($row);
or
reset($rows);
$first_key = key($rows);
Hard to guess without seeing any code.
You can limit your records using LIMIT clause.
SELECT column_name FROM table_name LIMIT 1;
It will return the first record of that table.

CakePHP: How to retrieve data by multiple condition

Please help me to retrieve data from a table by multiple condition in Cakephp
I have one table name: article; I have tried to retrieve data with the code below
I want to get specific id as given in the parameter; article_price > 0 and article_status > 1
public function getArticle($artID = ''){
return $this->find('all', array(
'condition' => array(
'article_id =' => $artID,
'article_price' => '> 0',
'article_status = ' => '1'),
'order' => 'article_id DESC'
));
}
// the out put was selected all data without condition that I want.
What was the problem with my code?
What I found out is I print: echo $this->element ('sql_dump'); and I got the following sql statement:
SELECT `article`.`article_id`, `article`.`name`, `article`.`article_price`, `article`.`article_status` FROM `db_1stcakephp`.`article` AS `article` WHERE 1 = 1 ORDER BY `article_id` DESC
Please help me.
Thank!
If your model name is Article:
public function getArticle($art_id) {
return $this->find('first', array(
'conditions' => array(
'Article.article_id' => $art_id,
'Article.article_price >' => 0,
'Article.article_status >' => 1,
),
));
}
Using 'Model.field' syntax is optional, until your models have relationship and have the same names - for example Article.status and Author.status.
Moving comparison sign into array's key part allows you to do:
'Article.price >' => $minPrice,
'Article.price <=' => $maxPrice,
And I didn't really notice typo in 'conditions'.

Categories