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
Craig shouldn't $county=Somerset be $county==Somerset or === for identical to? or did you just make a mistake posting?
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
<?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: