Hi i need to count the number items in an album now on the album i have create a field called count which counts the number items in that album but now i dnt knw how to update those as soon insert a item in the table item so i thought woul be possible to create a script that when insert data into item tables also update album table row count by adding one like update count with A now A equal row count ++1 minus if delete any help on start of it if possible???/
i tried like this but did not work any sugestions are welcome?? $sql = "INSERT INTO " .PHOTOS_TABLE. " ( photo_name, photo_date, photo_proper, photo_size, album_id ) VALUES ( '" .addslashes($photo_name). "', " .time(). ", '" .addslashes($key_name). "', " .intval($size). ", " .$album. " )"; $update_query="UPDATE albums set $count=count+'1' WHERE $count=count"; // total will be updated to total+number of new puppies added. mysql_query($update_query); PHP:
what your thinking might work but not as accurately when your database grows... this album is like a directory right?? why not just count how many items are inside that directory.. search more on fopen() directories function in php.
try something like $update_query = 'UPDATE `albums` SET `count` = `count` + 1 WHERE `album_id` = ' . $album . ' LIMIT 1'; Code (markup):
true that however, know that your albums table is redundant if it's only purpose is to keep the count. Even if it has any other purpose, i would suggest not to keep the count in there .. it may become a source of errors in future. instead, to show the count, i would suggest to simply use the following in your script (the one that should show the count): $count = mysql_result(mysql_query(SELECT COUNT(*) FROM `PHOTOS_TABLE` WHERE `album_ID` = '$someID' ), 0); the count queries are usually very light on MySQL so it should execute very fast (just make sure your PHOTOS_TABLE has a primary key) good luck