Quick PHP question - Fopen() and related

SpaceshipSpaceship
SpaceshipSpaceship
SpaceshipSpaceship
Watch

Jeanco

RyanPrice.ca - DeveloperEstablished Member
Impact
4
Ok, I have a form set up with the code listed below. What I'm trying to do is when the user submits the form the following happens:

1) The file with the data is opened. This will contain 2 numbers. One holding the number of votes and one with the vote total (explained in more detail later).
2) the two numbers are stored to two variables, $voteTotal and $numVotes or something like that.
3) The new vote is added to $voteTotal (its a rating script so this will be a number between 1 and 10) and $numVotes is increased by one.
4) Finally, I want to the file saved and closed. The data still stored in $numVotes and $voteTotal will be used to display an avg rating (avg = $voteTotal/$numVotes).

As you can see I have this planned out I just can't compeltely figure out how the fopen() fread() and fwrite() functions work. Any help would be appreciated. Here is the code for the form:

PHP:
	<form action="process.php" method="get">
	<select name="menu1" onChange="MM_jumpMenu('parent',this,0)">
  <option>Rate It!</option>
  <option>----------</option>
  <option value="process.php?rating=1">1</option>
  <option value="process.php?rating=2">2</option>
  <option value="process.php?rating=3">3</option>
  <option value="process.php?rating=4">4</option>
  <option value="process.php?rating=5">5</option>
  <option value="process.php?rating=6">6</option>
  <option value="process.php?rating=7">7</option>
  <option value="process.php?rating=8">8</option>
  <option value="process.php?rating=9">9</option>
  <option value="process.php?rating=10">10</option>
</select>

	</form>

Thanks
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
are the votes stored in one text file?

http://uk.php.net/manual/en/function.fwrite.php


this page should explain most of what you need :)

fopen will load a file so it can be read. written etc etc fopen("local/path/to/file", "r+");

the r+ tells php that you need to write to the file

fread will open the contents of the file you just opened

fwrite will write to the file

fclose is needed to free up the resources of the opened file


if not

php.net/fread
php.net/fopen
 
Last edited:
0
•••
Hi Ryan,

Can you post the snippet from process.php where you read the values and write using fopen?

You may want to consider writing the data to a simple two column MySQL table. It's more reliable than text files and easier to manage.

Also the form you posted seems somewhat unusual. Will that work? This kind of format is what I would use

Code:
<form action="process.php" method="get">
    <select name="rating" onChange="MM_jumpMenu('parent',this,0)">
  <option>Rate It!</option>
  <option>----------</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
  <option value="8">8</option>
  <option value="9">9</option>
  <option value="10">10</option>
</select>

    </form>

You would when have the $_GET["rating"] (or $rating) variable available to you with the rating value.
 
0
•••
Here is the code I'm using at the moment. I know its not completely efficient (recommend changes if you see them, I'm sitll learning). Its having a bit of problems outputting the results (I'm getting a header error), but its a start:

PHP:
<?php if (!isset($a)) { ?>

<script language="JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</script>

<table align="center" width="150" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>
	<b>Movie:</b> Troy<br>
	<b>Stars:</b>  B. Pitt, O. Bloom<br>
	<b>Director:</b> W. Peterson
	</td>
  </tr>
  <tr>
    <td align="center">
	<form action="entertainment/rating.php" method="get">
	<select name="menu1" onChange="MM_jumpMenu('parent',this,0)">
  <option>Rate It!</option>
  <option>----------</option>
  <option value="entertainment/rating.php?a=1&rating=1">1</option>
  <option value="entertainment/rating.php?a=1&rating=2">2</option>
  <option value="entertainment/rating.php?a=1&rating=3">3</option>
  <option value="entertainment/rating.php?a=1&rating=4">4</option>
  <option value="entertainment/rating.php?a=1&rating=5">5</option>
  <option value="entertainment/rating.php?a=1&rating=6">6</option>
  <option value="entertainment/rating.php?a=1&rating=7">7</option>
  <option value="entertainment/rating.php?a=1&rating=8">8</option>
  <option value="entertainment/rating.php?a=1&rating=9">9</option>
  <option value="entertainment/rating.php?a=1&rating=10">10</option>
</select>

	</form>
	</td>
  </tr>
</table>

<?php }
	elseif ($a = 1) { //if the action is set to process
		//open the txt file and get the data
		$handle = fopen("http://www.foymedia.com/absolute/website/student/website/entertainment/results.txt","r");
		$numVotes = trim(fgets($handle,80));
		$voteTot = trim(fgets($handle,80));
		fclose($handle);
		
		//process the data
		$numVotes = $numVotes + 1;
		$voteTot = $voteTot + $rating;
		
		//save the file
		$handle = fopen("http://www.foymedia.com/absolute/website/student/website/entertainment/results.txt","w");
		fwrite($handle,$numvotes);
		fwrite($handle,$totvotes);
		fclose($handle);
		
		//reload the index page with the command to display results
		header("Location: [url]http://[/url]".$_SERVER['HTTP_HOST'] . "/absolute/website/student/website/index.php?a=2");
		}
		
	else { //if the action is set to display results
		//open the txt file and get the data
		$handle = fopen("http://www.foymedia.com/absolute/website/student/website/entertainment/results.txt","r");
		$numVotes = trim(fgets($handle,80));
		$voteTot = trim(fgets($handle,80));
		$result = $votTot / $numVotes;
		fclose($handle);
		//display the data ?>
		
<script language="JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</script>		

<table align="center" width="150" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>
<font size="+1">Troy</font>
	</td>
  </tr>
  <tr>
    <td align="center">
<b>Avg. Rating: <font size="+1"><?php echo $result ?></font></b>
	</td>
  </tr>
</table>
<?php } ?>

Feel free to point out any mistakes you may see
 
0
•••
PHP:
// opens file $file
$file = "./users.txt";
$fp = fopen("$file", "a+");
// reads contents of $file
while (!feof($fp)) $content .= fgetc($fp);
$content = $content;
// explodes contents to remove all spaces and put each word/number in an array
$stats = explode(" ", $content);
while (list($index, $stat) = each ($stats));
// changes your votes $stats[0] = the first number/word +1 up each word
$numVotes = $stats[0] + 1;
$voteTot = $stats[1]  + $rating;
$cont = "$numVotes $voteTot";
// writes the new votes and total in $file
fwrite($fp, "$cont");
fclose($fp); 
// refresh page here

That should work for you.

You have to phase out you url.

header("Location: <a href="http://" target="_blank">http://</a>".$_SERVER['HTTP_HOST'] . "/absolute/website/student/website/index.php?a=2");

try adding the URL in a variable eg.
PHP:
$server = $_SERVER['HTTP_HOST'];
$link = "http://" . "$server" . "/absolute/website/student/website/index.php?a=2";
header("Location: <a href=\"$link\" target=\"_blank\">$link</a>");
 
Last edited:
0
•••
Olitt — high-converting AI websites, only from $1/moOlitt — high-converting AI websites, only from $1/mo

We're social

Escrow.com
Spaceship
Escrowly
CryptoExchange.com
Domain Recover
URLs.com
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back