#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3

    Foreach index + 1 ?


    Hi! i'm using

    PHP Code:
    foreach ($_GET['item'] as $position => $item
    to insert a new position for a specific item in the database, The problem is that $item starts at 0, but my item ID's start at 1.. so there's a mismatch. How can i increment the index from the beginning? thanks!
  2. #2
  3. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,303
    Rep Power
    9400
    You can't, but you are totally free to use a bit of simple addition.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3
    I tried to do

    $item + 1;
    before the loop starts, but it's not working..
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    No, you're supposed to do that in the loop.

    The values for $item are 0, 1, 2, ..., but you want 1, 2, 3, ... The obvious solution for that is to simply use $item + 1 within the loop.
  8. #5
  9. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    Originally Posted by Jacques1
    No, you're supposed to do that in the loop.

    The values for $item are 0, 1, 2, ..., but you want 1, 2, 3, ... The obvious solution for that is to simply use $item + 1 within the loop.
    isn't $position the key?
    PHP Code:
    foreach ($_GET['item'] as $position => $item)  {
           
    // SELECT FROM TABLE WHERE id = (int)$position+1;

  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3
    Originally Posted by aeternus
    isn't $position the key?
    PHP Code:
    foreach ($_GET['item'] as $position => $item)  {
           
    // SELECT FROM TABLE WHERE id = (int)$position+1;

    Allright! that's one problem. but it's still not working.

    PHP Code:
    foreach ($_GET['item'] as $item => $position)
    {
        
    $item $item 1;
        
    mysql_query("UPDATE items SET position = $position WHERE id = $item"); 

    hm. weird
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,622
    Rep Power
    595
    Still wrong.
    PHP Code:
    foreach ($_GET['item'] as $item => $position)
    {
        
    mysql_query("UPDATE items SET position = $position WHERE id = ".$item+1); 

    P.S. You should not be using the depreciated MySQL extensions. Use PDO.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3
    Everyone's talking about PDO. I've read about it but it looks WAY complicated.. what would my above statement look like in PDO? thanks
  16. #9
  17. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,303
    Rep Power
    9400
    Originally Posted by gw1500se
    Still wrong.
    PHP Code:
    foreach ($_GET['item'] as $item => $position)
    {
        
    mysql_query("UPDATE items SET position = $position WHERE id = ".$item+1); 

    Or not. notflip's works, yours won't. There's some other problem.
  18. #10
  19. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3
    Indeed. I don't get it .. it's not updating the database at all.
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    204
    Rep Power
    3
    Originally Posted by notflip
    Indeed. I don't get it .. it's not updating the database at all.
    It's updating it but in a total wrong way. damnit!
  22. #12
  23. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Well, I don't think we can help you there without a concrete error description.

    Also, do not insert the raw GET parameters into the query, this makes your code wide open to SQL injections. In fact, you should completely drop the obsolete mysql_ functions and switch to one of the contemporary libraries that supports prepared statements ... yada yada yada

IMN logo majestic logo threadwatch logo seochat tools logo