www.webdeveloper.com
Results 1 to 4 of 4

Thread: for loop - error

  1. #1
    Join Date
    Sep 2007
    Posts
    136

    for loop - error

    hello.
    i came to a lil problem i was not able to solve myself.
    so here it is..

    i have some table in db:

    Structure is : ID | ORDER_NO | TITLE | SUBTITLE | FULLTEXT | ATTACHMENT |

    and if i have 5 rows in my table and remove one of them
    forexample: 2nd one the order no then becomes: 1, ,3,4,5... 2 is missing

    so i wanted to reorder it when deleting and tried something like this



    PHP Code:
    $q "SELECT * FROM bps2news";
    $re mysql_query($q) or die (mysql_error());
    $ro mysql_fetch_array($re);
    $num mysql_num_rows($re);
    $newnum $num 1;
    for (
    $i 1$i $newnum$i++)
    {
    mysql_query("UPDATE bps2news SET order_no = '$i'");


    and this code does not do what i want but gives every row maximum value of $i

    any ideas?

    THANKS

  2. #2
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    PHP Code:
    $numRemoved 2;
    mysql_query("UPDATE bps2news SET order_no = order_no-1 WHERE order_no > $numRemoved"); 
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  3. #3
    Join Date
    Sep 2007
    Posts
    136
    this is if i remove 1 row..
    but what if i remove several rows

    2nd, 5th, 15th.. etc..

  4. #4
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    PHP Code:
    $numsRemoved = array(2515);
    $query = ('UPDATE bps2news SET order_no = order_no - (order_no>'.
                  
    implode(')-(order_no>'$numsRemoved).
                  
    ')'); 
    I'd echo out $query first, should result in something like:
    Code:
    UPDATE bps2news SET order_no = order_no - (order_no>2)-(order_no>5)-(order_no>15)
    if that code should work(but I've not tested it).
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles