Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,342

    * Again - Array Sorting Problem

    I am reading a list of countries from a field in a database,

    I want as I am reading from that field (after breaking it apart), to enter it into an array..

    After that I want to sort() the array and just print it out.

    This is what I got so far, I think the sort() will be a pieace of cake, and the printing will be a slight problem (if anyone knows it)..

    PHP Code:
    while ($break_countries[$i] != NULL) {
           
    $stat_break explode('-',$break_countries[$i]);
           
    $i $i +1;
           
    $countrylist .= array($stat_break[0] => $stat_break[1]);

    Problem the array is not there?

    tried printing it and it just gives me letters a A??

    Thanks in advanced,


    [0] conatins country name, [1] contains the count
    Peace,
    Testing 1.. Testing 1..2.. Testing 1..2..3...

  2. #2
    Join Date
    Jan 2003
    Posts
    1,715
    I assume this is a text file 'database'? If it's an SQL database, you could just have the server sort it for you. Anyway, .= is a string operation; you probably want array_push(). I haven't tested it, but the docs imply that += should also work.
    Game Servers are the next hot market!
    Slim margins, heavy support, fickle customers, and moronic suppliers!
    Start your own today!

  3. #3
    I'm assuming you want to sort it alphabetically? In your SELECT query do something like this...
    PHP Code:
    $result mysql_query("SELECT CountryName FROM CountryTable ORDER BY CountryName ASC"); 
    Then they are already sorted alphabetically for you. If you want to just display them then echo out the CountryName as you loop through the results instead of adding them to an array, sorting the array, then echoing the array...
    PHP Code:
    while ($row mysql_fetch_array($result)) 
    {
    echo 
    $row['CountryName'];
    echo 
    "<br>";
    }

    mysql_free_result($result); 

  4. #4
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,342
    Ok...

    First answer is that it is a sql database,

    I know I can arrange it with ORDER BY but it will not work in this case.

    There is only 1 Field which contains the country list and counts formated this way:

    United States-30|Germany-5|United Kingdom-23|

    First I break them into country with count..

    Then i break those and display them. What I want to do is instead of displaying them, store them into an array and then sort the array and then display them.

    As you guys now there is around 300 countires and more, and since I do not enter all of them I just enter them into the database first come first serve.

    Thank you

    Aziz
    Testing 1.. Testing 1..2.. Testing 1..2..3...

  5. #5
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    PHP Code:
    <?php
    /* Assuming you have a string
    United States-30|Germany-5|United Kingdom-23
    and you want it sorted by country name */

    $string "United States-30|Germany-5|United Kingdom-23";
    $bits explode("|",$string);
    while(list(,
    $v) = each($bits))
    {
        
    $tmp explode("-",$v);
        
    $country["$tmp[0]"] = $tmp[1];
    }
    ksort($country,SORT_STRING);
    echo 
    "<pre>"print_r($country); echo "</pre>";
    ?>
    Outputs :
    Code:
    Array
    (
        [Germany] => 5
        [United Kingdom] => 23
        [United States] => 30
    )
    Enjoy
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  6. #6
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,342
    fyrestrtr.... Words can not explain my thanks.....

    Your the best i've seen..

    thnx alot


    Thanks to all of you guys...

    Peace,
    Testing 1.. Testing 1..2.. Testing 1..2..3...

  7. #7
    Join Date
    May 2004
    Location
    Singapore
    Posts
    262
    First answer is that it is a sql database
    hmm... have you considered actually using the relational aspect of the database?

    i.e. the table should have at least 2 fields, 1 for country, 1 for count.
    I would add a primary key that auto increments.
    #include<cstdio>
    char*s="#include<cstdio>%cchar*s=%c%s%c;%cint main(){std::printf(s,10,34,s,34,10);}";
    int main(){std::printf(s,10,34,s,34,10);}

  8. #8
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,342
    Well i would've done that..

    but if i did it would be a very large database and just imagine the sql statements I will have to make each time fo hundreads of countries...

    Also there isnt just one counter to track the countires, there can be many..

    So 20 counters each having there own country tables would be annoying, so i broke it down to this:

    each counter has a list field in the country table, the list starts at 0, as countries come in they get added it, so if no one comes from china, chine wont be displayed since its not in the database.

    Thnx

    Peace,
    Testing 1.. Testing 1..2.. Testing 1..2..3...

  9. #9
    Join Date
    May 2004
    Location
    Singapore
    Posts
    262
    but if i did it would be a very large database and just imagine the sql statements I will have to make each time fo hundreads of countries...
    You're now using explode(), and a loop that performs explode() on each iteration.
    explode() itself loops through the array elements.
    Effectively, what you're using now is just as expensive, if not more expensive.

    Also there isnt just one counter to track the countires, there can be many..
    So there's a many-many relationship between countries and counters.
    You can easily account for that in your database, by using 3 related tables.
    #include<cstdio>
    char*s="#include<cstdio>%cchar*s=%c%s%c;%cint main(){std::printf(s,10,34,s,34,10);}";
    int main(){std::printf(s,10,34,s,34,10);}

  10. #10
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,342
    Originally posted by laserlight
    You're now using explode(), and a loop that performs explode() on each iteration.
    explode() itself loops through the array elements.
    Effectively, what you're using now is just as expensive, if not more expensive.


    So there's a many-many relationship between countries and counters.
    You can easily account for that in your database, by using 3 related tables.
    I guess you are right..

    Never thought of it this way..

    God's well I will implement it on the next verison

    peace,
    Testing 1.. Testing 1..2.. Testing 1..2..3...

  11. #11
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,342
    one mroe question case anyone still here

    can i sort them by the count?

    i looked at ksort and sort functions, they only mention 4 different kind of sorts all realting to the countryname, can i sort the count?

    peace,
    Testing 1.. Testing 1..2.. Testing 1..2..3...

  12. #12
    Join Date
    Jan 2003
    Posts
    1,715
    sort() will sort by value and maintain the key names. You could also use one of the usort functions to make your own comparison function.

    You really only need two tables, since country isn't really a key on its own. You have a table like

    counterid | country | count

    counterid+country is a unique ID. As with the current system, you only have rows for countries that have a count, but now you can sort by country or count. You can also use the grouping functions to track total visitors for a counter, total visitors for a country, etc.
    Game Servers are the next hot market!
    Slim margins, heavy support, fickle customers, and moronic suppliers!
    Start your own today!

Posting Permissions

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