sphinx Posted October 22, 2011 Share Posted October 22, 2011 Hi there, I want to allow the ability for DJ's to turn on and off radio requests. Now obviously I've encorporated a check box and submit button within a PHP login. But I was wanting to know if this can be done with PHP or is a database required as the option will need to be remembered for a period of time. I'd probably look to do it by making it display on a page 'requests enabled' if enabled and a blank page if disabled. therefore I could encorporate: <?php if(!empty($requests)) { //requests template } else { //radio requests disabled template } ?> What's the best method for this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/249606-best-method-for-this/ Share on other sites More sharing options...
xtopolis Posted October 22, 2011 Share Posted October 22, 2011 From the information you've given us, storing the two templates inline in the same PHP file would be fine. Adding a database or fileloading (require/include) would likely be more overhead than needed, assuming your template is a fairly small amount of code. If you're talking about saving the "requests turned on/off" thing.. that's a slightly different story. You could probably get away with it by using sessions and having the client pages check on intervals to see whether or not to display the template. Does that answer your question, or are you referring to something else? Quote Link to comment https://forums.phpfreaks.com/topic/249606-best-method-for-this/#findComment-1281412 Share on other sites More sharing options...
sphinx Posted October 22, 2011 Author Share Posted October 22, 2011 Yes this, or can you use php to display content for a certain period of time, such as 60 minutes, then it will revert what it displays. So basically, if check box is ticked (for requests) make a web page display text, if disabled (unticked) make it display nothing. I can then relate that page to determine if requests are on or off using PHP get file contents if(!empty. Quote Link to comment https://forums.phpfreaks.com/topic/249606-best-method-for-this/#findComment-1281417 Share on other sites More sharing options...
xtopolis Posted October 22, 2011 Share Posted October 22, 2011 Yes - for simple things, you might just have it modify a file like (requests.ini) and have PHP read the contents of that file at set intervals. However, if there are going to be multiple DJs, etc, etc, you may switch to using a database for statistical considerations later down the line. Quote Link to comment https://forums.phpfreaks.com/topic/249606-best-method-for-this/#findComment-1281431 Share on other sites More sharing options...
sphinx Posted October 22, 2011 Author Share Posted October 22, 2011 Oh yes, good idea, what stops me from making it editing and removing content from a file dependant on whether it is ticked or unticked. Quote Link to comment https://forums.phpfreaks.com/topic/249606-best-method-for-this/#findComment-1281433 Share on other sites More sharing options...
sphinx Posted October 22, 2011 Author Share Posted October 22, 2011 Can't I just "get file contents" from a txt file and use if(!empty and use: To enable requests $myFile = "requeststatus.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "Requests enabled"; fwrite($fh, $stringData); fclose($fh); and to disable requests $myFile = "requeststatus.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = ""; fwrite($fh, $stringData); fclose($fh); Quote Link to comment https://forums.phpfreaks.com/topic/249606-best-method-for-this/#findComment-1281435 Share on other sites More sharing options...
sphinx Posted October 22, 2011 Author Share Posted October 22, 2011 Unexpected $end <?php if(isset($_POST['submit'])) { if(empty($_SESSION['checkbox'] ) { $myFile = "requeststatus.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "Requests enabled"; fwrite($fh, $stringData); fclose($fh); } else { $myFile = "requeststatus.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = ""; fwrite($fh, $stringData); fclose($fh); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/249606-best-method-for-this/#findComment-1281440 Share on other sites More sharing options...
xtopolis Posted October 22, 2011 Share Posted October 22, 2011 Yes - I don't see any reason why you wouldn't be able to do that. Syntactically, it seems fine. edit: You edited it while I was replying.. , but in the version I saw later you're missing a ) to match the empty( function. (2nd if statement) Quote Link to comment https://forums.phpfreaks.com/topic/249606-best-method-for-this/#findComment-1281441 Share on other sites More sharing options...
sphinx Posted October 22, 2011 Author Share Posted October 22, 2011 Sorry, i tweaked the code slightly before you replied. Quote Link to comment https://forums.phpfreaks.com/topic/249606-best-method-for-this/#findComment-1281442 Share on other sites More sharing options...
xyph Posted October 22, 2011 Share Posted October 22, 2011 Generally, unexpected $end mean you've missed a } closing curly brace somewhere. Formatting your code properly and/or comments will hel you keep track of whats going on. <?php if( someCondition() === TRUE ) { // Start condition check $result = 'Tabulation helps us track how deep we are.'; if( someOtherCondition === TRUE ) { // Start other condition check $result .= ' Comments will help if you still find yourself forgetting them.'; // End other condition check. } // End condition check } ?> Quote Link to comment https://forums.phpfreaks.com/topic/249606-best-method-for-this/#findComment-1281443 Share on other sites More sharing options...
sphinx Posted October 23, 2011 Author Share Posted October 23, 2011 hi there, I've managed to avoid getting errors now, but nothing is getting written to file: <form action="config.php" method="post"> <input type="checkbox" name="requests" value="status" /> Enable music requests <input type="submit" /> </form> Config.php <?php if(isset($_POST['submit'])) { if(empty($_SESSION['checkbox'])) { $myFile = "requeststatus.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "Requests enabled"; fwrite($fh, $stringData); fclose($fh); } else { $myFile = "requeststatus.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = ""; fwrite($fh, $stringData); fclose($fh); } } ?> Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/249606-best-method-for-this/#findComment-1281494 Share on other sites More sharing options...
sphinx Posted October 23, 2011 Author Share Posted October 23, 2011 bump Quote Link to comment https://forums.phpfreaks.com/topic/249606-best-method-for-this/#findComment-1281555 Share on other sites More sharing options...
xtopolis Posted October 23, 2011 Share Posted October 23, 2011 Shouldn't you be checking the value of $_POST['requests'] instead of $_SESSION['checkbox']? From the code you gave us, all I can tell is that in your logic, you are looking at a session variable, but your above code is dealing with a form. Is that it? Quote Link to comment https://forums.phpfreaks.com/topic/249606-best-method-for-this/#findComment-1281588 Share on other sites More sharing options...
xyph Posted October 23, 2011 Share Posted October 23, 2011 Have you read the manual regarding fopen or are you just blindly following a tutorial? The mode 'w' truncates the file to 0 length - empties the file. What's the point in this chunk of code? $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = ""; fwrite($fh, $stringData); fclose($fh); Also, if you have a specific section of code that DOESN'T write anything to the file, how can you be sure that something's going wrong? It seems part of your script will intentionally clear the file. You need to learn to debug your own code, or you'll never excel as a programmer. Quote Link to comment https://forums.phpfreaks.com/topic/249606-best-method-for-this/#findComment-1281591 Share on other sites More sharing options...
sphinx Posted October 23, 2011 Author Share Posted October 23, 2011 Have you read the manual regarding fopen or are you just blindly following a tutorial? The mode 'w' truncates the file to 0 length - empties the file. What's the point in this chunk of code? $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = ""; fwrite($fh, $stringData); fclose($fh); Also, if you have a specific section of code that DOESN'T write anything to the file, how can you be sure that something's going wrong? It seems part of your script will intentionally clear the file. You need to learn to debug your own code, or you'll never excel as a programmer. ok thanks. Your just far to technical for me Quote Link to comment https://forums.phpfreaks.com/topic/249606-best-method-for-this/#findComment-1281645 Share on other sites More sharing options...
xyph Posted October 23, 2011 Share Posted October 23, 2011 Technical and programming. Who would've thought they'd go together? Quote Link to comment https://forums.phpfreaks.com/topic/249606-best-method-for-this/#findComment-1281651 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.