0

I want to show list of my product as below :

Sr.No | Name | Type 
--------------------
1     |  aa  | 2
2     |  bb  | 2
3     |  cc  | 3
4     |  dd  | 2
5     |  cc  | 4 

But its coming as below

Sr.No | Name | Type 
--------------------
3     |  aa  | 2
5     |  bb  | 2
1     |  cc  | 3
4     |  dd  | 2
2     |  cc  | 4 

and when any new entry made then : But its coming as below

Sr.No | Name | Type 
--------------------
4     |  aa  | 2
5     |  bb  | 2
2     |  cc  | 3
6     |  dd  | 2
3     |  cc  | 4 
1     |  ec  | 5     // this below are two new entry made 
7     |  fc  | 4 

above should be as below :

Sr.No | Name | Type 
--------------------
1     |  aa  | 2
2     |  bb  | 2
3     |  cc  | 3
4     |  dd  | 2
5     |  cc  | 4 
6     |  ec  | 5     // this below are two new entry made 
7     |  fc  | 4 

Its not that i want order by name , i just want normal Sr.No as per entry .. means if i add any entry first then Sr.No 1 , 2 , 3 and so on..

But when i add any new entry then its Sr.No get changed but it should get last sr.no

Below is what i have tried :

SELECT (@cnt := @cnt + 1) AS rowNumber, orn.name, orn.type ,  u.* 
FROM  ofr AS u 
CROSS JOIN (SELECT @cnt := 0) AS dummy
LEFT JOIN  or_name AS orn ON u.id=orn.id 
GROUP BY u.id ORDER BY rowNumber AND u.add_datetime DESC

please note (@cnt := @cnt + 1) AS rowNumber is generating serial no and this value is pass below under $cols "Sr.No" .... Sr.No is column name and nothing else ... its value is coming by " rowNumber "

Listing is call in below way :

$result =   get_details()

$cols = array( 
        _("Sr.No.")=>array('align'=>'center'),
        _("Name")=>array('align'=>'center'),
        _("Type")=>array('align'=>'center')
       );

$table =& new_db_pager('tablename', $result, $cols, 'ofr','id',10); 

$table->width = "95%";

display_db_pager($table);

Database :

id : bigint(20)     UNSIGNED    No  None    AUTO_INCREMENT

Note : above format is from http://frontaccounting.com/ demo link : http://demo.frontaccounting.eu/

user3209031
  • 837
  • 1
  • 14
  • 38
  • try with - `ORDER BY rowNumber, u.add_datetime DESC` – Sougata Bose Nov 06 '14 at 07:07
  • hey that is also right .. but serial no not coming properly .. i means its not as per added date .. also when i add any new entry then sr.no get change .. but it should not happen ..as if i have add any new entry then sr.no allotted to new entry should be last sr.no +1 ... but it not working get changed – user3209031 Nov 06 '14 at 07:11
  • you can do it with php also. – Sougata Bose Nov 06 '14 at 07:13
  • i know count , count++ in while loop ... but here i am not using any while loop for listing its standard format of : http://demo.frontaccounting.eu/ which there are calling in array pagination ..and not getting how i can build the same in it – user3209031 Nov 06 '14 at 07:14
  • you are passing the data in `$result`, just generate it as needed. – Sougata Bose Nov 06 '14 at 07:16
  • yes i know under $cols = array (_("Sr.No.")=>array('align'=>'center','fun'=>'myownfunction')); they have this 'fun' option to create own function for particular column .. hence function myownfunction($row) { //generate sr.no } .. what i can do here ..can u guide me ?? – user3209031 Nov 06 '14 at 07:19
  • hence by using $cols = array (_("Sr.No.")=>array('align'=>'center','fun'=>'myownfunction')); i can do function myownfunction($row) { //generate sr.no } but not getting how i can generate sr.no here – user3209031 Nov 06 '14 at 07:23
  • any one here ? ... still waiting – user3209031 Nov 06 '14 at 08:44
  • any one here ? ..still waiting .. pls help me – user3209031 Nov 06 '14 at 12:48

1 Answers1

0

After reading the question a little more carefully, I'm deleting my old answer and writing a new one.

It seems like you're trying to automatically assign a serial number to each new entry in the table, and you want that number to be persistent. There is a tool specifically for this kind of problem: the auto-incrementing integer. You never have to set this field when inserting records -- the database will assign the next number automatically.

In your DDL you specify that you want a field that is an integer, auto-incrementing, and not null:

CREATE TABLE ofr (
    serial MEDIUMINT NOT NULL AUTO_INCREMENT,
    ... the rest of your fields ...
)

When you query the table you don't have to do any fancy row-counting. Just select the serial field along with everything else you want.

People use this all the time for generating unique IDs within a table. It is often used as a primary indexing key, too. More information is available at the MySQL reference.

slashingweapon
  • 11,007
  • 4
  • 31
  • 50
  • Hey slashingweapon : thanks for reply .. but i have used auto increment system ... please check my question last line " Database : id : bigint(20) UNSIGNED No None AUTO_INCREMENT " ... reason i am not using this is .. suppose i have 1,2,3,4,5 entry and i delete my 4 or 3 number entry then new entry will be 6 not 4 or 3 ..also again the ordering will be change ..if i delete my 4 or 3 entry and new entry gets same number then it will be confusing ..hope u get my point what i mean to tell.... thanks once again ..hoping for another solutions. – user3209031 Nov 07 '14 at 11:08
  • Hey after here and there i just check with simple query as : " SELECT (cnt := cnt + 1) AS rowNumber, u.* FROM ofr AS u , (SELECT cnt := 0) AS dummy WHERE 1 GROUP BY u.id ORDER BY u.id DESC " -- AND GUESS IT WORKING I AM GETTING PROPERLY SERIAL NO ... I THINK THERE IS ISSUE WITH JOIN QUERY ??? – user3209031 Nov 07 '14 at 11:44
  • yes i got it because of LEFT JOIN " LEFT JOIN or_name AS orn ON u.id=orn.id " //// BUT FOR DEMO I JUST CREATED ANOTHER TABLE AND REPALCE ABOVE LEFT JOIN WITH IT THEN ..IST WORKING FINE .. BUT WITH ABOVE LEFT JOIN ITS NOT WHY SOO ...LEFT JOIN or_name ... database " id smallint(6) No None AUTO_INCREMENT " – user3209031 Nov 07 '14 at 12:16
  • I guess I just don't understand what you're trying to achieve. It just doesn't make any sense to me. I don't understand why you don't just use the ID you already have. From everything you say, it should do exactly what you want. What's wrong with it? – slashingweapon Nov 08 '14 at 19:30
  • auto-increment dont form proper serial no .. ex : 1,2,3,4,5,6,7,8,9,10...etc .. because if we delete some entry then that number will not be seen again.. ex: if i delete 10 id then my new entry will be 11 not 10 ..which is not as per serial no ordering ..anyway thanks for the support till now :) – user3209031 Nov 10 '14 at 05:03
  • Well, yeah. Serial numbers are supposed to be unique. They are used once, ever. One piece of equipment, one serial number. If the equipment is destroyed, you don't re-use the serial number. If what you're trying to do is number things by the date they were entered, then maybe you should add a date field and set it to automatically set to `now()` when a new record is created. Then order by date and count the rows like you're doing now. Pretty screwy, though. Not really a serial number. – slashingweapon Nov 10 '14 at 05:22
  • HMM.. tried that too ..added date column .. i think something wrong with my join query ..as if i remove join query then above thing works well but as soon as i used JOIN query then my serial no is not coming proper – user3209031 Nov 10 '14 at 08:28
  • You should probably stop calling this a serial number. If you can come up with a different name for it, based on what you're really trying to do, then a better solution may present itself. I think your nomenclature is getting in the way of a proper analysis. – slashingweapon Nov 10 '14 at 18:30