NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Quick PHP question - fopen() and related

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 06-01-2004, 01:01 PM THREAD STARTER               #1 (permalink)
RyanPrice.ca - Developer
Join Date: Dec 2003
Posts: 1,328
Jeanco is a jewel in the roughJeanco is a jewel in the roughJeanco is a jewel in the rough
 



Quick PHP question - fopen() and related


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 Code:
    <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>
????: NamePros.com http://www.namepros.com/programming/32545-quick-php-question-fopen-and-related.html
  <
option value="process.php?rating=3">3</option>
????: NamePros.com http://www.namepros.com/showthread.php?t=32545
  <
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
__________________
Ryan Price - Webmaster
www.HostDurham.com - For Hosting | www.jeanco.ca - For Webdesign
Jeanco is offline  
Old 06-01-2004, 04:14 PM   #2 (permalink)
Senior Member
Join Date: May 2003
Posts: 2,187
adam_uk is a jewel in the roughadam_uk is a jewel in the roughadam_uk is a jewel in the rough
 


Breast Cancer
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 by adam_uk; 06-01-2004 at 04:22 PM.
adam_uk is offline  
Old 06-01-2004, 04:25 PM   #3 (permalink)
RJ
NamePros Webmaster


 
RJ's Avatar
Join Date: Feb 2003
Posts: 12,930
RJ Has achieved greatnessRJ Has achieved greatnessRJ Has achieved greatnessRJ Has achieved greatnessRJ Has achieved greatnessRJ Has achieved greatnessRJ Has achieved greatnessRJ Has achieved greatnessRJ Has achieved greatnessRJ Has achieved greatnessRJ Has achieved greatness
 



Find Marrow Donors! Cystic Fibrosis Parkinson's Disease
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.
__________________
@DomainBuyer facebook
RJ is offline  
Old 06-01-2004, 04:29 PM THREAD STARTER               #4 (permalink)
RyanPrice.ca - Developer
Join Date: Dec 2003
Posts: 1,328
Jeanco is a jewel in the roughJeanco is a jewel in the roughJeanco is a jewel in the rough
 



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 Code:
<?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");
        }
        
????: NamePros.com http://www.namepros.com/showthread.php?t=32545
    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>        
????: NamePros.com http://www.namepros.com/showthread.php?t=32545

<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
__________________
Ryan Price - Webmaster
www.HostDurham.com - For Hosting | www.jeanco.ca - For Webdesign
Jeanco is offline  
Old 06-07-2004, 09:28 AM   #5 (permalink)
NamePros Member
Join Date: Nov 2003
Posts: 44
Pablo is an unknown quantity at this point
 



PHP Code:
// opens file $file
$file "./users.txt";
$fp fopen("$file""a+");
// reads contents of $file
????: NamePros.com http://www.namepros.com/showthread.php?t=32545
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));
????: NamePros.com http://www.namepros.com/showthread.php?t=32545
// 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.

Quote:
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 Code:
$server $_SERVER['HTTP_HOST'];
$link "http://" "$server"/absolute/website/student/website/index.php?a=2";
header("Location: <a href=\"$link\" target=\"_blank\">$link</a>"); 
__________________
Click Link for Java IRC

PHP-Live IRC: pgardner.net:6667, Channel: #php.
Last edited by Pablo; 06-07-2004 at 09:40 AM.
Pablo is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 07:28 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger