SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Count problem

  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Count problem

    I cannot figure out the PHP count function. I have a page with a roster, I am trying to find a photo for each unit in the roster. What I am trying to do is count how many photos out of the range I have.

    PHP Code:
    function count_ph($i){
        include 
    '/usr/local/lib/connect.php';
        
    mysql_select_db('twincitiesrailfan_com_-_photos',$dbcnx);

        
    $get_photo "SELECT * FROM photos WHERE loco_num='$i'";
        
    $show_photo mysql_query($get_photo);

        
    $is_photo mysql_fetch_array($show_photoMYSQL_ASSOC);
        
        if(
    $is_photo['path'] == TRUE){

            
    $is $is_photo['path'];
            
    $top count($is); 

        }
            print 
    $top;        

    }; 
    What I get is:
    Railroad: BNSF Railway
    Locomotive model: C44-9W (DASH 9-44CW)
    Roster numbers: 4400 through 4499
    Roster count:1111111111111111111111 / 100
    Obviously I am trying to replace the "11111" with "22 / 100". I have tried various array() and count() trials which haven't worked.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I think thats just an sql problem

    "select count(*) from photos where loco_num= '$i' AND path !='' ";

    (that last bit is !='' [two single quotes] and depending on a your schema might be " AND path is not null")

    Should return 22

  3. #3
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,191
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)

    count function

    The best place to start learning about PHP functions is the online documention.
    int count ( mixed $var [, int $mode] )
    Returns the number of elements in var, which is typically an array, since anything else will have one element.
    My guess is you have 22 results, each with a non-array value in their path field, so you get "1" printed 22 times.

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    The best place to start learning about PHP functions is the online documention.
    My guess is you have 22 results, each with a non-array value in their path field, so you get "1" printed 22 times.
    Believe me, I did a lot of reading. I always go to php.net and research. I have tried - unsuccessfully - to make an array of the result. And yes, I am getting "1" 22 times. Maybe part of my problem was not revealed earlier. Say I am looking for units 8600 to 8639; respectively one is $start and $end. I then use this
    PHP Code:
    for($i $start$i <= $end$i++){
                        
    count_ph($i);
                }; 
    to compare each 86xx to my database. I went back and reviewed making arrays at php.net. I also tried mysql_num_rows with my SELECT. I even tried to make an equation to add up all the "1"s. Nothing has worked and I think it is due to the running each $i through the function. But I don't know how else to run each roster through without using the for() function.

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2007
    Location
    Australia
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tried this?
    PHP Code:
    $total 0;
    function 
    count_ph($i){
        include 
    '/usr/local/lib/connect.php';
        
    mysql_select_db('twincitiesrailfan_com_-_photos',$dbcnx);

        
    $get_photo "SELECT * FROM photos WHERE loco_num='$i'";
        
    $show_photo mysql_query($get_photo);

        
    $is_photo mysql_fetch_array($show_photoMYSQL_ASSOC);
        
        if(
    $is_photo['path'] == TRUE){

            
    $is $is_photo['path'];
            
    $top count($is);

        }
        
        
    // Calculate some sort of total here
        
    global $total;
        if(!isset(
    $total)) $total 0;
        
    $total++;
        print 
    $top;

    After you have all the calls to count_ph() done, just echo $total; and it should work. The problem is that you are not counting all the rows, just the current one, you need a query that checks for an image path regardless of the value of loco_num.

  6. #6
    SitePoint Zealot allstar's Avatar
    Join Date
    Sep 2005
    Location
    in my box.
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function count_ph($i)
    {
        include '/usr/local/lib/connect.php';
        mysql_select_db('twincitiesrailfan_com_-_photos',$dbcnx);
    
        $show_photo = mysql_query("SELECT `path` FROM `photos` WHERE `loco_num` = '".$i."'");
    
        $is_photo = mysql_fetch_array($show_photo);
        
        if($is_photo['path'] == TRUE)
        {
            return mysql_num_rows($show_photo);
        }
        else { return 0; }
    
    };
    Are you just trying to return the amount of results found?

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Allstar, I tried yours previous to your suggestion and it didn't work.

    Akash, I tried your original suggestion and got the 'ArrayArrayArrayArray / 10' returned. I then wanted to see what was returned and threw a print_r($top) in your computation code:

    Array ( [0] => cefx/cefx6002_100606.jpg ) Array ( [0] => cefx/cefx6008_100606.jpg ) Array ( [0] => cefx/cefx6015_100606.jpg ) Array ( [0] => cefx/cefx6020_100606.jpg ) / 10
    with this I tried a count($top) and count($top, COUNT_RECURSIVE) but realized each array was its own array.

    Every entry in the database has a 'path' value, but many units have more than one photo and I just want to count how many units have at least one photo. Is there a better way (or a way that works) to run each number in a roster to check for the path value?


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
  •