SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2013
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to rename indexes?

    Hi guys,

    I have a PHP array and it has contents like this below
    Please note indexes are from MySQL records.
    Code:
    Array
    (
        [12] => 'john'
        [11] => 'george'
        [15] => 'karen'
        [14] => 'joseph'
    )
    Now I want to rename the same indexes from MySQL in ascending order so it will look like this below,
    Code:
    ID  | name
    ----------------
    11 |  george
    12 |  john
    15 |  joseph
    16 |  karen
    How to interchange the indexes?

    Thanks in advance.

  2. #2
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,805
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by super_noob View Post
    Hi guys,

    I have a PHP array and it has contents like this below
    Please note indexes are from MySQL records.
    Code:
    Array
    (
        [12] => 'john'
        [11] => 'george'
        [15] => 'karen'
        [14] => 'joseph'
    )
    Now I want to rename the same indexes from MySQL in ascending order so it will look like this below,
    Code:
    ID  | name
    ----------------
    11 |  george
    12 |  john
    15 |  joseph
    16 |  karen
    How to interchange the indexes?

    Thanks in advance.
    I think that id should be a Primary Key and the table->name should be indexed

    #
    PHP Code:

       $sql 
    'SELECT id, name from ' $tableName .' ORDER BY id ASC';
       
    $obj mysql_query($sql);
       while (
    $row mysql_fetch_object($obj)):
          echo  
    $row->id .' ==> ' $row->name;
       endwhile;            
       
    // results
        
    12   ==> 'john'
        
    11   ==> 'george'
        
    15   ==> 'karen'
        
    14   ==> 'joseph'


       
    $sql =  'SELECT id, name from ' $tableName .' ORDER BY name ASC'
       
    $obj mysql_query($sql);
       while (
    $row mysql_fetch_object($obj)):
          echo  
    $row->id .' ==> ' $row->name;
       endwhile;            
       
    // results
        
    11   ==> 'george'
        
    12   ==> 'john'
        
    14   ==> 'joseph'
        
    15   ==> 'karen' 
    By the way, MYSQL will be dropped in the near future so it is advisable to learn PDO instead.
    Last edited by John_Betong; Nov 24, 2013 at 23:01. Reason: spelling: not my fortay

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2013
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @John_Betong ;

    Thanks man but that's not what I'm looking for.
    What I want is to interchange the ids.

    If the id is 11 and I want to change it to 12 and so on...
    How to do this?

    thanks in advance.

  4. #4
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,805
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by super_noob View Post
    @John_Betong ;

    Thanks man but that's not what I'm looking for.
    What I want is to interchange the ids.

    If the id is 11 and I want to change it to 12 and so on...
    How to do this?

    thanks in advance.
    I think I understand. Try sorting the array items:

    // asort
    PHP Code:

    echo '<pre>';

    // Create array of items
    $items = array
    (
        
    '12' => 'john',
        
    '11' => 'george',
        
    '15' => 'karen',
        
    '14' => 'joseph',
    );

    // Before Sorting:
    print_r($items);
    // Output
    Array
    (
        [
    12] => john
        
    [11] => george
        
    [15] => karen
        
    [14] => joseph
    )

    // After Sorting:
    asort($items);
    print_r($items);
    // Output
    $items = array
    (
        [
    11] => george
        
    [12] => john
        
    [14] => joseph
        
    [15] => karen

    What I thought you meant was swap (exchange) two item keys in the array
    Off Topic:

    //===============================
    //
    // function to swap two array item key values.
    //
    // usage:
    // $items = swapkeys($items, '12','11');
    //
    //===============================
    function swapkeys($items, $key_1, $key_2)
    {
    $tmp = $items[ $key_1 ];
    $items[ $key_1 ] = $items[ $key_2 ];
    $items[ $key_2 ] = $tmp;

    return $items;
    }


  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2013
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still not right.
    Only the ids must change/rename not the values.
    But anyway thanks, I'll try to figure it out myself.

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by super_noob View Post
    Now I want to rename the same indexes from MySQL in ascending order
    may i ask why?

    perhaps there is some other way to achieve your ultimate objective
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2013
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @r937 ;

    Okay here is the scenario.

    Inside an Admin Control Panel.
    An admin has the right to/can re-order the records.
    Example:
    http://jsfiddle.net/xeon9/tZd7C/40/

    Noticed you can re-order the records by dragging them up and down. (This is the same feature in the Admin Panel).

    And on the public page these records will be displayed in ascending order (A must).

    So as you can see my codes should fit this logic,
    It should rename the indexes in ascending order base from an Admin re-ordered records choice.

    Is renaming Primary Indexes illegal?
    Is there any easy way to implement this without renaming the indexes?
    How about creating new table? etc...?

    Thanks in advance.

  8. #8
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by super_noob View Post
    http://jsfiddle.net/xeon9/tZd7C/40/

    Noticed you can re-order the records by dragging them up and down.
    i didn't see anything that i could drag, sorry

    i think i understand what you're trying to do, but i would recommend strongly against trying to renumber the id values

    (note: don't call them indexes, they are actually primary key values)

    use an additional column, called something like sort_sequence, and manipulate that instead

    suggestion: use FLOAT for sort_sequence

    that way, when you want to move an item so that it will come between 3 and 4, just update its sort_serquence value to 3.5

    if you want another one to move in between the item with 3 and the item with 3.5, just update its value to 3.25

    you can keep doing this almost indefinitely with floating point numbers

    just don't do it to the id values
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  9. #9
    SitePoint Enthusiast
    Join Date
    Nov 2013
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @r937 ;

    i didn't see anything that i could drag, sorry
    It's the small table on the right side, just hover your mouse on the table and drag it up and down.

    Anyway I'll follow your advice, Thanks.


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
  •