last inserted row number.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • canabatz
    New Member
    • Oct 2008
    • 155

    last inserted row number.

    hi all !!

    i got my results like that

    1 user0 55
    2 user1 53
    3 user2 49
    4 user2 48
    5 user3 47 <------- last inserted row
    6 user4 46

    how do i get to echo the position of the last inserted row?

    the result need to be "last inserted row is 5"!!


    thank you all!!
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Originally posted by canabatz
    hi all !!

    i got my results like that

    1 user0 55
    2 user1 53
    3 user2 49
    4 user2 48
    5 user3 47 <------- last inserted row
    6 user4 46

    how do i get to echo the position of the last inserted row?

    the result need to be "last inserted row is 5"!!


    thank you all!!
    There are many possible ways, it depends on how you're storing this data. Give us some more information. Also, wouldn't the last inserted row by #6 ?

    Comment

    • prabirchoudhury
      New Member
      • May 2009
      • 162

      #3
      1. not very much clear abt asking.
      2. we could get just inserted using

      Code:
      after executing insert statement call
      $last_inserted_id = mysql_insert_id();
      or explain specific in details


      :)

      Comment

      • canabatz
        New Member
        • Oct 2008
        • 155

        #4
        replay

        before

        count | username | bid-amount
        1 user0 55
        2 user2 49
        3 user2 48
        4 user4 46

        after

        count | username | bid-amount
        1 user0 55
        2 user2 49
        3 user2 48
        4 user4 47 <--- last inserted
        5 user4 46


        bid amount going with order by bid_amount DESC ,so amount 47 was last inserted ,the amount 46 count was 4 ,and after the insert of amount 47 ,amount 46 go down to 5 count!!

        im displaying to the user in what place is bid amount are!! ,and this is what im trying to get.

        thanks for your replay :)

        i hope you got me better now.

        Comment

        • prabirchoudhury
          New Member
          • May 2009
          • 162

          #5
          Option A
          1. i dont think it a good idea to update table data for every insert.
          2. it is easier to get the details for particular bit (would be a bit)
          Code:
          table bit
          ---------------------
          | bit_id| details
          ---------------------
          |1001 | car
          |1002 | gold
          --------------------
           
          add a fild bit_id in your described table
          3. when you wanna display current biting status for a particular bit_id, get the query of
          Code:
          ORDER BY `bid-amount` DESC
          option B
          4. otherwise you have to check the data in bid-amount for a bit_id(that you doent have) and rearrange with updates.

          5. i would prefer option A

          :))

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Again, you haven't explained how you're storing the data.

            But, the last inserted is going to be (providing there was only one insert) array_length - 2.

            Comment

            • canabatz
              New Member
              • Oct 2008
              • 155

              #7
              you didnt understand me

              the count is running and doing the count dynamic ,no change in the database

              only the id is changing in the database the id is on auto increment.
              when im displaying the results it is going with bid_amount desc ,so the highst bid_amount is allways at the top!!

              now if second user place a bid and it is not in the top(the bid amount is less then the first user, then the count is just displaying in what line he his!!

              all i need is to show the user in what position he his ,like:

              echo hello $username ."you are in $position position";

              i hope this time you get what i need!!

              thanks

              Comment

              • waqasahmed996
                New Member
                • Jun 2008
                • 160

                #8
                i think your problem will be solved using simply

                Code:
                ORDER BY `bid_amount` DESC

                Comment

                • prabirchoudhury
                  New Member
                  • May 2009
                  • 162

                  #9
                  1. insert a new bid . (without count) [but no duplicate bit-amount.]

                  Code:
                  2. select latest details
                  
                  $sql= "select * from table ORDER BY bit-amount DESC";
                  $csr = mysql_query($sql);
                  $count = 1;
                  while ($row=mysql_fetch_array($csr)){
                  
                  update count with $count where id = '".$row[id]."' ";
                  
                  $count++
                  }
                  then it would update the current bit status


                  :)

                  Comment

                  • canabatz
                    New Member
                    • Oct 2008
                    • 155

                    #10
                    all i need is the row number!

                    i got a table returning all the data from the database ,one row showing the bid_amount!!


                    all i want is to tell the user in what position he his!

                    the table have a dynamic count with count++ !!


                    all i want is to tell the user the row number he is!! thats it!!

                    i cannot get that!
                    thank you!

                    Comment

                    • n8kindt
                      New Member
                      • Mar 2008
                      • 221

                      #11
                      before

                      count | username | bid-amount
                      1 user0 55
                      2 user2 49
                      3 user2 48
                      4 user4 46

                      after

                      count | username | bid-amount
                      1 user0 55
                      2 user2 49
                      3 user2 48
                      4 user4 47 <--- last inserted
                      5 user4 46
                      the title of this post is misleading. you're not looking for the last inserted id but rather the current rank of a user who placed a bid.

                      what i would do is make a query that sorts the users by bid amount, retrieve the results using mysql_fetch_ass oc and then loop thru the results.

                      something like
                      Code:
                      $target_username = "user4"; //username we need to match
                      $username_matched = false; //we have not matched a username yet
                      $q = "SELECT * FROM `bid-history` ORDER BY `bid-amount` ASC";
                      $r = mysql_query($r);
                      $count = 1; //set current
                      while($w = mysql_fetch_assoc($r) && !$username_matched){
                        if($w['username']==$target_username ){
                         $username_matched=true;
                         $user_position = $count;
                         $user_bidamount = $w['bid-amount'];
                        }
                       $count++;
                      
                      
                      }
                      echo "User \"$target_username\" is number $user_position in line with a bid amount of $$user_bidamount";
                         //output: User "user4" is number 4 in line with a bid amount of $47

                      Comment

                      • canabatz
                        New Member
                        • Oct 2008
                        • 155

                        #12
                        this is exactly what i need

                        where does it get the last bid_amount from?

                        there is bids from the same user again and again!!

                        your code is exactly what i need but need some fixes to fit!!

                        thank you!!!

                        Comment

                        • n8kindt
                          New Member
                          • Mar 2008
                          • 221

                          #13
                          if you look, i set it up so that the while loop only runs when $username_match ed equals false. when the user is matched the variable is set to true thus killing the loop

                          Comment

                          • canabatz
                            New Member
                            • Oct 2008
                            • 155

                            #14
                            is it possible to do like that?

                            Code:
                            $q = "SELECT * FROM `bidding_details` where bid_id=$bid_id ORDER BY id desc limit 1";
                            i want to limit the SELECT to find only the id of the last inserted id!

                            now i want to start count++ and see if the $username match the highest ID,

                            if there is a match between the $username and the Highst ID then display
                            the $count!!

                            thanx!

                            Comment

                            • canabatz
                              New Member
                              • Oct 2008
                              • 155

                              #15
                              hi

                              this code is working for me ,but i want it to display only the last inserted row ,position ,now i can display all the rows and the position ,but i need to show the user only his last bid position ,please help me figure this one out!!

                              Code:
                              $q = "(SELECT * FROM `bidding_details` where bid_id=$bid_id and sortbid = '1' order by bid_price desc) union all (SELECT * FROM `bidding_details` where bid_id=$bid_id and sortbid = '0' order by bid_price desc) order by sortbid desc, bid_price desc";
                              $result=mysql_query($q) or die('Failed selecting applications: ' . mysql_error());
                              $num1=mysql_num_rows($result);
                              $rown = 0; 
                              while($row = mysql_fetch_assoc($result)) 
                              { 
                                  $rown++; 
                                  echo $row['bid_price']. ' is pos: ' . $rown . '.<br>'; 
                              	
                              }
                              thank you

                              Comment

                              Working...