SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard cmuench's Avatar
    Join Date
    Jul 2005
    Location
    At my computer
    Posts
    2,251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simple id function

    I need a function that gets an number id through a variable $id and then increases it once then returns the value of the new number with $id. Any help?
    The following is what I have created but It isn't working.
    PHP Code:
    $id '1';
    function 
    cart_id($id)
    {
    $id $id;
    return 
    $id;


  2. #2
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    London
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like what you require could be achieved with...

    PHP Code:
    $id=$id+1
    ... but I'm sure that's not your intent. Could you clarify what you're looking for? Thanks.

  3. #3
    SitePoint Evangelist DMacedo's Avatar
    Join Date
    May 2004
    Location
    Braga, Portugal
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool

    Firstly you're wrongly creating the $id as a string. You remove the ' (single quotes) from around the 1:
    PHP Code:
    $id '1'// This is '1' (string), as in text
      
    $id 1// This is ONE (integer), as in number 
    Next, you don't need to use a function for what you're trying to accomplish:
    PHP Code:
    $id 1;
      echo 
    $id// prints: 1
      
    ++$id;
      echo 
    $id// prints: 2 
    So wherever you're using:
    PHP Code:
    $new_id cart_id($id);
      
    // Where you want $new_id to be "$id value plus one"
      // change it to
      
    $new_id = ++$id
    ~ Daniel Macedo

  4. #4
    SitePoint Wizard cmuench's Avatar
    Join Date
    Jul 2005
    Location
    At my computer
    Posts
    2,251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well thank you everyone for your input I went with the ++$id as it sounds like the easiest.
    Also is there a way to get the biggest number from a row in a mysql table? Say for example I have the cart_id and there are 4 rows with the number 1, 2, 3, 4. I want mysql to find the biggest number and then give it to me. Can this be accomplished.?

  5. #5
    SitePoint Evangelist DMacedo's Avatar
    Join Date
    May 2004
    Location
    Braga, Portugal
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For better understanding, here's the explanation on increment/decrement operators
    ~ Daniel Macedo

  6. #6
    SitePoint Evangelist DMacedo's Avatar
    Join Date
    May 2004
    Location
    Braga, Portugal
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From what you're saying, I think what you want is the NEXT auto_increment number (correct me if I'm wrong)

    If I'm correct, you can find it in two different ways:
    First one, is to get the last inserted ID and increment it
    PHP Code:
    $result mysql_query('INSERT INTO `table` ...');
    $last_id mysql_insert_id();
    // $last_id has the value of the primary key of the last inserted row
    $new_id = ++$last_id
    A second way, in case you don't want it right after the INSERT statement, is to get it from the table information:

    PHP Code:
    $result mysql_query('SHOW TABLE STATUS LIKE `table`');
    $row mysql_fetch_assoc($result);
    $next_id $row['auto_increment']; 
    I haven't tested it, but I think it's correct
    ~ Daniel Macedo

  7. #7
    SitePoint Wizard cmuench's Avatar
    Join Date
    Jul 2005
    Location
    At my computer
    Posts
    2,251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for helping me I figured it out.

  8. #8
    SitePoint Evangelist DMacedo's Avatar
    Join Date
    May 2004
    Location
    Braga, Portugal
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Always a pleasure
    Have a nice day
    ~ Daniel Macedo

  9. #9
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    if you are thinking of using a query to get the MAX(id) from the table, then adding 1 to it, then using the result to do an INSERT into the same (or another) table, then i beg you, please, please don't do it that way
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  10. #10
    SitePoint Evangelist DMacedo's Avatar
    Join Date
    May 2004
    Location
    Braga, Portugal
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I trust he needs the ID for some link or reference
    ~ Daniel Macedo


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •