[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

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


Closed Thread
 
LinkBack Thread Tools
Old 06-01-2004, 12:01 PM   #1 (permalink)
RyanPrice.ca - Developer
 
Join Date: Dec 2003
Posts: 1,331
34.00 NP$ (Donate)

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>
  <
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
__________________
Ryan Price - Webmaster
www.HostDurham.com - For Hosting | www.jeanco.ca - For Webdesign
Jeanco is offline  
Old 06-01-2004, 03:14 PM   #2 (permalink)
Senior Member
 
Join Date: May 2003
Posts: 2,211
6,170.25 NP$ (Donate)

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 03:22 PM.
adam_uk is offline  
Old 06-01-2004, 03:25 PM   #3 (permalink)
RJ
NamePros Founder

Administrator

 
Join Date: Feb 2003
Location: Bay Area, CA
Posts: 13,173
102,201.68 NP$ (Donate)

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
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.
RJ is offline  
Old 06-01-2004, 03:29 PM   #4 (permalink)
RyanPrice.ca - Developer
 
Join Date: Dec 2003
Posts: 1,331
34.00 NP$ (Donate)

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");
        }
        
    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
__________________
Ryan Price - Webmaster
www.HostDurham.com - For Hosting | www.jeanco.ca - For Webdesign
Jeanco is offline  
Old 06-07-2004, 08:28 AM   #5 (permalink)
NamePros Member
 
Join Date: Nov 2003
Posts: 44
80.00 NP$ (Donate)

Pablo is an unknown quantity at this point


PHP Code:
// 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.

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 08:40 AM.
Pablo is offline  
Closed Thread


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 03:19 PM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85