Jump to content

Best method for this


sphinx

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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);
}
}
?>

Link to comment
Share on other sites

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
}

?>

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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  :rtfm:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.