Array sorting

Discussion in 'PHP' started by sandstorm140, Mar 2, 2009.

  1. #1
    I'd like to be able to sort an array by one of the 3 values (username, joindate, or last_login) before it gets echod off. using ksort($value[2]); or something that would work.
    How can I go about doing this?

    
    	while ($row[67] = mysql_fetch_assoc($result[67])) {
    		$rowRe[] = array(1=>$row[67]['username'], $row[67]['joindate'], $row[67]['last_login']);
    	}
    	
    	foreach ($rowRe as $value) {
    		echo('
    			<tr align="left" class="accountinfo">
    				<td align="center">' . $iii++ . '</td>
    			 	<td>' . $value[1] . '</td>
    			 	<td align="center">' . substr($value[2], 0, -9) . '</td>
    				<td align="left" colspan="7">' . substr($value[3], 0, -9) . '</td>
    			</tr>');	
    	}
    	
    
    PHP:
     
    sandstorm140, Mar 2, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    usort() and make a callback function to do your comparison.
     
    SmallPotatoes, Mar 2, 2009 IP
  3. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Ok thanks, i will try that... may post back if i don't understand it
     
    sandstorm140, Mar 3, 2009 IP
  4. ranacseruet

    ranacseruet Peon

    Messages:
    302
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    why not sorting it through query while getting from database ?
     
    ranacseruet, Mar 3, 2009 IP
  5. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    because i'm pulling information from multiple tables and databases. Join queries can't do that.. well.. as far as I know.
     
    sandstorm140, Mar 3, 2009 IP
  6. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #6
    As long as it's all combined in one query, you can sort any way you like, by any combination of criteria.
     
    SmallPotatoes, Mar 3, 2009 IP
  7. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    What's the best way to select 3 databases with one query?
    I know how to select multiple tables, but not databases.
     
    sandstorm140, Mar 3, 2009 IP
  8. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Using MySQL? Just stick the database name in the beginning:

    select table_1.field_A, table_2.field_B
    from database_Q.table_1, database_Z.table_2
    etc.
     
    SmallPotatoes, Mar 3, 2009 IP
  9. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    ok.. Yes... congrats.. I feel stupid now.. LOL
    I see that all the time.. however, it never occurred to me lol.

    When two tables contain same fields names, lets say table1.field_ID, table2.field_ID, will that cause an conflict?
     
    sandstorm140, Mar 3, 2009 IP
  10. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #10
    It's no problem as long as you refer to them unambiguously. For example, if both table1 and table2 have a field called "field_ID", then never write "field_ID" on its own; instead always write "table1.field_ID" or "table2.field_ID".
     
    SmallPotatoes, Mar 3, 2009 IP
  11. sandstorm140

    sandstorm140 Peon

    Messages:
    80
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thank you ever so much, I owe ya :D
     
    sandstorm140, Mar 3, 2009 IP