need to create a custom quote no

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

need to create a custom quote no

Post 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"
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
crazytopu
Forum Contributor
Posts: 259
Joined: Fri Nov 07, 2003 12:43 pm
Location: London, UK
Contact:

Post 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'])."', 
                        '', 
                        ''     
                        )";
Post Reply