Im trying to create a script that will post a notification stored in a text file when a project is created/updated.
If the project is created it needs to create the file and if the project is being updated it needs to update the existing txt file for it.
So far I’ve got it to create the txt file and update it. However it is only updating it once, if i try to update the file again it just doesn’t do it.
Here’s the code:
function addPost($userID, $content, $date, $featured, $projectID){
if($projectID != 0){
$addClass = 'normal';
$sql1 = "SELECT * FROM `blog` WHERE `projectID` = ".$projectID;
$query1 = mysql_query($sql1);
$num_rows1 = mysql_num_rows($query1);
if($num_rows1 != 0){
$addClass = 'internalUpdate';
}
}else{
$addClass = 'normal';
}
switch($addClass){
case normal;
//SQL to get next ID
$nameQuery = mysql_query("SELECT MAX(ID) FROM `blog`");
$nameRow = mysql_fetch_array($nameQuery);
$nextID = $nameRow['MAX(ID)'] + 1;
//Set Blogname
$blogName = md5($nextID).'.txt';
$blogTargetFile = './file/blog/'.$blogName;
$fileHandle = fopen($blogTargetFile, 'w');
fwrite($fileHandle, $content);
fclose($fileHandle);
$sql2 = "INSERT INTO `blog` (`userID`, `name`, `created`, `featured`, `projectID`) VALUES ('".$userID."', '".$blogName."', '".$date."', '".$featured."', '".$projectID."')";
break;
case internalUpdate;
$sql1 = "SELECT * FROM `blog` WHERE `projectID` = ".$projectID;
$query1 = mysql_query($sql1);
$row1 = mysql_fetch_array($query1);
$blogName = $row1['name'];
$blogTargetFile = './file/blog/'.$blogName;
$fileHandle = fopen($blogTargetFile, 'w');
fwrite($fileHandle, $content);
fclose($fileHandle);
$sql2 = "UPDATE `blog` SET `created` = '".$date."' WHERE `ID` = ".$row1['ID'];
break;
}
mysql_query($sql2);
};
An Ideas?
Charles – richini-design.co.uk
Not sure I got it right, but you’re overwriting the file because of the “w” mode used with fopen function.
Try
fopen([filename], "a")to append data.See http://fr.php.net/manual/en/function.fopen.php for more infos.