Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: Array Posting

  1. #1
    New to the CF scene
    Join Date
    May 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Array Posting

    Hi all!

    I have a question about Arrays.

    I have an array being assigned values by a loop:
    PHP Code:
    if ($myrow mysql_fetch_array($result)) {
    do {

    $ip_country 'United States'//This value is actually being taken from the database, just trying to keep it simple for here.

    $countries[] = $ip_country;

    } while (
    $myrow mysql_fetch_array($result));  

    I would like to print out the values on the Array, whatever they may be, and how many times each value is placed into it.

    Example:

    United States 800 times.
    United Kigndom 700 times.

    I understand how to get everything to work, except how to echo the value only once and have it count the times it shows.

    I may be new to the forum, but I have a strong understanding of PHP. Arrays have just been the one thing I have tried to avoid.

    Thanks,
    PM

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,129
    Thanks
    2
    Thanked 328 Times in 320 Posts
    If your goal is to obtain a count of each country in your database, it is best to get the database to do this for you in a query. A query will do this very quickly. Fetching all of your data into an array and then using an array function to count the number of times each country appears is the slowest possible way of doing this.

    If you insist on using an array, if you research the array functions in the PHP manual http://php.net/array you will find that the array_count_values() function will do what you want.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New to the CF scene
    Join Date
    May 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for the fast reply.

    My database actually does not have the countries in it, I am using GeoIP to pick up the ip and verifying it against known ranges for each country. This is so I can verify the location of each user who inputs data on my site.

    I tried using print_r(array_count_values($countries)); but that did not give me the values separated just

    Array ( [United States] => 385 [Uruguay] => 1 [Venezuela] => 10 [Puerto Rico] => 61 [China] => 1 [Peru] => 2 [United Kingdom] => 1 [Canada] => 2 [Spain] => 1 )
    Sorry, I should have mentioned I had tried that.

  • #4
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    You should be able to access each country like this:

    $countries = array_count_values($countries);
    print $countries['United States']; //Outputs 385

    ...right? ..or am I missing something

    HTH
    Dan
    Last edited by whizard; 05-04-2007 at 01:44 AM.
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,129
    Thanks
    2
    Thanked 328 Times in 320 Posts
    If your issue is how to iterate through the array and produce some output for each array index, see the foreach() function - http://php.net/foreach
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,129
    Thanks
    2
    Thanked 328 Times in 320 Posts
    My database actually does not have the countries in it
    You might want to add a column for this. It will save on all the extra processing needed every time you want to obtain a count.

    If you complete your code using the current method, benchmark the time it takes to produce a count. Then add a column for the country and populate it. Then recheck the timing necessary to produce a count using a query. If you don't want to keep the database method, you can just delete the country column.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #7
    New to the CF scene
    Join Date
    May 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    so what youre saying is that instead of figuring out where they came from, just add it to the database as they enter it?

    Alright, I just thought that it would be easier to do it this way since I've already got about 500 entries that I need to do this for. but yeah it makes sense since then all i'd have to do is group them through mySQL.

    I'll just write a script to update the blank fields.

    Thanks
    PM

  • #8
    New to the CF scene
    Join Date
    May 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks, i took the advice and it works great... I just needed a new approach


  •  

    Posting Permissions

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