Results 1 to 6 of 6

Thread: rearranging table rows

  1. #1
    Join Date
    Mar 2007
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default rearranging table rows

    hi,
    i have a table with rows of name. also there are arrows up and down corresponding to each row. when up arrow is clicked the position of the row should go 1 place higher and when down arrow is clicked it should come down 1 place.
    the name come from database and have random id which are unique.
    can some1 pls help me with this.
    i'm trying for quite a while but am clueless.
    Thanks in advance..Suk

  2. #2
    Join Date
    Jul 2007
    Location
    Azerbaijan, Baku
    Posts
    144
    Thanks
    11
    Thanked 27 Times in 25 Posts

    Default

    You mean when you press up arrow, that row go to up and other one go down?

  3. #3
    Join Date
    Mar 2007
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    yes.
    example:

    aaa up down
    bbb up down [ say up and down are buttons]
    ccc up down

    if i click down for aaa then the list shpuld look like

    bbb up down
    aaa up down
    ccc up down

  4. #4
    Join Date
    Jul 2007
    Location
    Azerbaijan, Baku
    Posts
    144
    Thanks
    11
    Thanked 27 Times in 25 Posts

    Default

    If you mean that. Your table is like this:

    Code:
       id   |   name
    ---------------
       1    |   hello
       2    |   hi
       3    |   goodbye
    To echo all rows:

    Code:
    <?php
    $result = mysql_query("SELECT * FROM mytable ORDER BY id DESC");
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    $id = $row['id'];
    $name = $row['name'];
    echo $name."&nbsp;&nbsp;<a href=\"?up=".$id."\">Go up<a/>&nbsp;&nbsp;<a href=\"?down=".$id."\">Go down</a><br/><br/>";
    }
    ?>
    When press up or down:

    Code:
    <?php
    if($_GET['up']){
    $id = $_GET['up'];
    $newid = $id + 1;
    $current = mysql_query("UPDATE mytable SET id = '$newid' WHERE id = '$id'");
    $other = mysql_query("UPDATE mytable SET id = '$id' WHERE id = '$newid'");
    }
    if($_GET['down']){
    $id = $_GET['down'];
    $newid = $id - 1;
    $current mysql_query("UPDATE mytable SET id = '$newid' WHERE id = '$id'");
    $other = mysql_query("UPDATE mytable SET id = '$id' WHERE id = '$newid'");
    }
    ?>
    I don't know if it is correct way or not. But hope it will help...

  5. #5
    Join Date
    Mar 2007
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    this may work but the problem is that in my table the ids are not in order..

    my table:
    ---------
    id | name
    ---------------
    1 | hello
    10 | hi
    14 | goodbye
    ---------
    this is because entries get deleted also.. so any clue how to proceed?

  6. #6
    Join Date
    Jul 2007
    Location
    Azerbaijan, Baku
    Posts
    144
    Thanks
    11
    Thanked 27 Times in 25 Posts

    Default

    I don't know how but you can make: on click hi's up, replace hi's id with goodbye's one and goodbye's one with hi's one.

    Um got idea. But i think it is incorrect:

    Change this:
    Code:
    <?php
    if($_GET['up']){
    $id = $_GET['up'];
    $newid = $id + 1;
    $current = mysql_query("UPDATE mytable SET id = '$newid' WHERE id = '$id'");
    $other = mysql_query("UPDATE mytable SET id = '$id' WHERE id = '$newid'");
    }
    if($_GET['down']){
    $id = $_GET['down'];
    $newid = $id - 1;
    $current mysql_query("UPDATE mytable SET id = '$newid' WHERE id = '$id'");
    $other = mysql_query("UPDATE mytable SET id = '$id' WHERE id = '$newid'");
    }
    ?>
    To this:

    Code:
    <?php
    if($_GET['up']){
    $id = $_GET['up'];
    $selectup = mysql_query("SELECT * FROM mytable WHERE id > '$id' LIMIT 1") or die(mysql_error());
    while($row = mysql_fetch_array($selectup, MYSQL_ASSOC)){
    $newid = $row['id'];
    }
    $updatecurrent = mysql_query("UPDATE mytable SET id = '$newid' WHERE id = '$id'") or die(mysql_error());
    $updateup = mysql_query("UPDATE mytable SET id = '$id' WHERE id = '$newid'") or die(mysql_error());
    }
    if($_GET['down']){
    $id = $_GET['down'];
    $selectdown = mysql_query("SELECT * FROM mytable WHERE id < '$id' LIMIT 1") or die(mysql_error());
    while($row = mysql_fetch_array($selectdown, MYSQL_ASSOC)){
    $newid = $row['id'];
    }
    $updatecurrent = mysql_query("UPDATE mytable SET id = '$newid' WHERE id = '$id'") or die(mysql_error());
    $updatedown = mysql_query("UPDATE mytable SET id = '$id' WHERE id = '$newid'") or die(mysql_error());
    }
    ?>
    Hope it will be correct without any errors.
    Last edited by allahverdi; 06-24-2008 at 07:04 AM.

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
  •