Page 1 of 1

need to create a custom quote no

Posted: Mon Mar 12, 2007 11:48 am
by crazytopu
Quote no should start from Q20000 and each time a new quote is added it will be incremented by one Q20001, Q20002, Q20003 and so on.

The way I was thinkig is:

Code: Select all

$insert = "INSERT INTO `request` (`quote_no`, `username` , `title` , `description` , `post_date` , `required_by_date` ) 
                        VALUES ( 
                        '',
						'".mysql_real_escape_string($_POST['title_txt'])."',
						'".mysql_real_escape_string($username)."', 
                        '".mysql_real_escape_string($title)."', 
                        '".mysql_real_escape_string($description)."', 
                        '".mysql_real_escape_string($post_date)."', 
                        '".mysql_real_escape_string($require_by_date)."' 
                       
						                   
                        
                        )";



if($insert_result = $connector->query($insert)) { 
  
//get the id of the row that is just inserted
  $id = $connector->insert_id();

//create quote no

$quote = "Q2000".$id;  // wht happens when id= 2000? the quote no looks like Q20002000 . Not what I want. 

Is there any function I can use?



//now update the quote no



$update = "UPDATE request SET quote_no = $quote WHERE id= $id"

Posted: Mon Mar 12, 2007 12:00 pm
by volka
insert_id() means you already have an auto_increment field in your table and therefore an unique identifier. So, there's no need for another id in - it's completely redundant.

Posted: Mon Mar 12, 2007 12:01 pm
by feyd

Code: Select all

$quote = 'Q' . bcadd(20000, $id, 0);
Note that when 100,000 is reached it will begin to expand in width.

Posted: Mon Mar 12, 2007 12:11 pm
by crazytopu
Thanks. But Volka, if I dont use that how would I know which is the last id that has been added the table? I need to get the value of $id isnot it or else how would this function work?

Code: Select all

bcadd(20000, $id, 0);
?

Thanks feyd.

Posted: Mon Mar 12, 2007 12:18 pm
by feyd
The point volka raises is valid. The identifier you are creating here is directly based on the automatically generated identifier in your table; therefore you don't need to store this identifier.

Posted: Wed Mar 14, 2007 2:19 pm
by crazytopu
Sorry Just had to come back again.

The question is where to get this $id value. I am sorry if I am going in cycle but I just don't understand how to get the $id, I definitely need to find the last inserted id and then start from there, isnot it?

like if I have 3 ids the higest id is 3, and therefore it would be (2000,$id,0) = (20000,3,0) which will give an output 20003.

Do I need to run sql query to get that id before I insert? And I dont know how to get the last id. Any help much appreciated.

Code: Select all

$quote = 'Q' . bcadd(20000, $id, 0);
$insert_to_request = "INSERT INTO `request` (`quote_no` , `username` , `title` , `description` , `post_date` , `required_by_date` , `approve` , `expire` ) 
                        VALUES ( 
                        
						'".mysql_real_escape_string($quote)."',  
                        '".mysql_real_escape_string($_POST['username_txt'])."',   
						'".mysql_real_escape_string($_SESSION['title'])."', 
                        '".mysql_real_escape_string($_SESSION['description'])."', 
                        '".mysql_real_escape_string($_SESSION['post_date'])."', 
                        '".mysql_real_escape_string($_SESSION['required_by'])."', 
                        '', 
                        ''     
                        )";