PHP in database

Discussion in 'PHP' started by billabong, Dec 21, 2005.

  1. #1
    Hi guys,
    You'll prob find this question very simple, but I'm fairly new to PHP and this is doing my nut in!! :(
    I need my script to add up the number of records in my database which have a specific field name. For example i have a column with "counties" in, i would like to add up the records with the county Somerset in that cell. I'm thinking its something along the lines of if $county = Somerset => count $county
    Im using a flatfile database, since Im not using a lot of data. Below is my code so far without this sum function...
    
    <?php
    $fp = fopen('data.csv','r'); 
    if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}
    
    while (!feof($fp)) { 
    $line = fgets($fp, 2048); 
    list ($country, $county, $campsite, $town, $address, $postcode) = split ('\,', $line); 
    
    NEED YOUR FUNCTION HERE PLEASE!! :)
    
    }
    $fp++; 
    }
    
    fclose($fp); 
    ?>
    PHP:
    Thank you so much for ay help that you can offer,
    Craig
     
    billabong, Dec 21, 2005 IP
  2. Foxy

    Foxy Chief Natural Foodie

    Messages:
    1,614
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Craig shouldn't
    $county=Somerset be $county==Somerset
    or === for identical to?

    or did you just make a mistake posting?
     
    Foxy, Dec 22, 2005 IP
  3. billabong

    billabong Peon

    Messages:
    184
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    yeah it shud, but i dont know how to write the lines, Im pretty sure that the thing Im looking for is only 2 lines long, do you have an idea of what the function is??? Please help, Im desperate!!!! lol :)
     
    billabong, Dec 22, 2005 IP
  4. Sham

    Sham Peon

    Messages:
    136
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    <?php
    $fp = fopen('data.csv','r'); 
    if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}
    
    $count=0;
    $fieldname='county';
    $searcherm='Somerset';
    
    
    while (!feof($fp)) { 
    $line = fgets($fp, 2048); 
    list ($country, $county, $campsite, $town, $address, $postcode) = split ('\,', $line); 
    
    if ($$fieldname == $searcherm)$count++;
    
    }
    $fp++; 
    }
    
    fclose($fp);
    
    echo "The $fieldname $searchterm appears $count times!";
     
    ?>
    PHP:
     
    Sham, Dec 22, 2005 IP