Striped Fish2 Posted September 4, 2011 Share Posted September 4, 2011 I've been playing with php and trying to write a simple AI program that will learn things you tell it and will store the stuff in text files. Anyway I am having an issue writing to the file I don't get a 500 it just doesn't write anything to the file. Also how do I make line breaks? Oh and how do I redirect to another page?Here's my code: <?php//This array shows the possible greetings a user can type in.$greetings[0]="HI";$greetings[1]="HEY";$greetings[2]="HELLO";$usercommand=$_GET["command"];$command = strtoupper($usercommand);If ($command==$greetings[0] OR $command==$greetings[1] OR $command==$greetings[2]) $cresponse="[AI] Who is it?";Else $cresponse="[AI] I don't understand tell me what does " . $usercommand . " mean?"; $newfiledata = "[You] " . $usercommand . "" . $cresponse . "";echo $newfiledata;echo file_put_contents("conversation.txt", $newfiledata, "FILE_APPEND");?> Link to comment Share on other sites More sharing options...
JamesB Posted September 4, 2011 Share Posted September 4, 2011 you don't need the " around constants (FILE_APPEND)new lines can be done with \nto redirect a browser: header('Location: example.com'); Link to comment Share on other sites More sharing options...
ApocalypeX Posted September 4, 2011 Share Posted September 4, 2011 It would probably make more sense to write this "AI" in Javascript as it would be more responsive/less-load-time. Also if you used Javascript you could redefine the AI at runtime, which is what AI's do. Link to comment Share on other sites More sharing options...
Striped Fish2 Posted September 5, 2011 Author Share Posted September 5, 2011 Oh well I'm actually planning on writing it as a computer application in Visual Basic because I want to eventaully add voice recognition to it. I'm just building a prototype in PHP to get all my thoughts together and come up with all the features.Anyway I have another problem I decided to work on it on a different computer because PHP was acting odd on my Desktop and taking 91% of the processor. I can write to the file but I can't read from it. Here's my code for the index.php page. Everytime I run it says the page has exceeded the 30 seconds and timed out. <?phpecho "<html><head></head><body style='background:black;'>";echo "<div style='color:white;'>";$file = fopen("conversation.txt","r");while(!feof($file));{echo fgets($file);}echo "</div>";echo "<form action='proccesscommand.php'><input type='text' name='command' style='color:White;border:0px;border-bottom:1px solid white;";echo "background:black;width:100%;border-top: 1px solid white'/></form></body></html>";?> What am I doing wrong? Link to comment Share on other sites More sharing options...
justsomeguy Posted September 5, 2011 Share Posted September 5, 2011 Make sure error reporting is enabled. If fopen doesn't return a valid handle then feof will never return true. Add this to the top:ini_set('display_errors', 1);error_reporting(E_ALL); Link to comment Share on other sites More sharing options...
Man In Tan Posted September 6, 2011 Share Posted September 6, 2011 If you're just outputting the file, you could use readfile(). A loop is unnecessary. <?phpecho '<html><head></head><body style="background:black;">';echo '<div style="color:white;">';readfile('conversation.txt');echo '</div>';echo '<form action="proccesscommand.php"><input type="text" name="command" style="color:White;border:0px;border-bottom:1px solid white;';echo 'background:black;width:100%;border-top: 1px solid white"/></form></body></html>'; Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.