storing info in a .txt file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

storing info in a .txt file

Post by bawla »

This is the code that I have already done.

Code: Select all

<?php
$value1=$_GET&#1111;'value1'];
$value2=$_GET&#1111;'value2'];
$value3=$_GET&#1111;'value3'];
$value4=$_GET&#1111;'value4'];
$username=$_GET&#1111;'username'];
$total1=(($value1 + $value2 + $value3) / $value4);
$total2=$total1 * 10;
echo "<font face=verdana size=2 color=red>&#123;$_GET&#1111;'username']&#125;</font><br>";
echo "<font face=verdana size=2>Games Won </font><br>";
echo "<font face=verdana size=2 color=red>&#123;$_GET&#1111;'value1']&#125;</font><br>";
echo "<font face=verdana size=2>Games lost by someone reaching 100 mojo</font> <br>";
echo "<font face=verdana size=2 color=red>&#123;$_GET&#1111;'value2']&#125;</font><br>";
echo "<font face=verdana size=2>Games lost by zero </font> <br>";
echo "<font face=verdana size=2 color=red>&#123;$_GET&#1111;'value3']&#125;</font><br>";
echo "<font face=verdana size=2>Games Played</font> <br>";
echo "<font face=verdana size=2 color=red>&#123;$_GET&#1111;'value4']&#125;</font><br>";
echo "<font face=verdana size=2>Percent of the time you/someone quit </font><br>";
echo "<font face=verdana size=2 color=red>&#123;$total2&#125;%</font>";
?>
What I want to do with it is to take the

Code: Select all

$total2 and $username
and store it on a .txt file so it has a record of what the % is for people.
I tried using the fopen,fwrite,fclose functions but I just can't seem to get them to work. Also how would I be able to have a top 10 list of the highest %'s. Hopefully this makes sense.
-Peyce
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

well you have the right idea with fopen n fwrite functions.

try this

Code: Select all

&lt;?php
$log = "whatever.txt"; 
if(file_exists("$log")) { 
$write = fopen($log, "r+"); 
fwrite($write, $username); 
fclose($write); 
} else { 
echo "$log could not be found"; 
} 
?&gt;
this should write the username...

do you have access to a MySQL database? what you want would be easier if you went thru that
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

where would i put that code? in the same file as the other php code?

and yea i have MySQL but i have no idea how to work it and php seems easier
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

i'd suggest you try to learn that... seems your new with php too?

Heres a good tutorial to get you started with both.

A MySQL DB is alot better than a text file when storing info. plus you can arrange, edit, add, delete, etc with stuff in a sql table alot easier then a text file.

http://hotwired.lycos.com/webmonkey/99/ ... rogramming
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

u can place the code anywhere after you defined the variables
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

thanks alot man, im gonna get one that tutorial right away
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

anytime, and always come back here for help with things you dont understand. Alot of very nice and intelligent people here willing to help.

and your choosing the right path by changing your method to MySQL. you'll thank me in the end.

:D

good luck and godspeed
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

hmm, i stumbled into a little problem, in the tutorial is says to put stuff into the comand line, but i dont know where that is, for MySQL i have phpmyadmin but it doesnt say anthing about that in the tutorial
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

by "stuff" i take it u mean the create table?

In phpmyadmin, after u named the table, click the table and you'll see a "sql" link

paste your create table code there

:D
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

wut about the comand line?

it says to put

mysql -u root mydb < mydb.dump

in there
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

Oh.. i'm sorry for not explaining...

in the first lessons of that tutorial, it tells u how to set-up php and mySQL on your own server, its thinking your an actual web hoster with the server.

ignore that part, just mimic the table in that tutorial
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

its ok, you've explained way more then you needed too and i thank you for that, you reply to my posts before i even submit them lol
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

i get this error when i try to view the php file that connects to the mysql

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/krunk/public_html/php/mysqlexample.php on line 25
First Name:

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/krunk/public_html/php/mysqlexample.php on line 29
Last Name:

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/krunk/public_html/php/mysqlexample.php on line 33
Address:

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/krunk/public_html/php/mysqlexample.php on line 37
Position:
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

read your private message.

and people usually post code so people can see the errors, but it seems like you dont ahve your table set-up correctly.

you connect to your DB before? lets see the code
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

Code: Select all

<?php



$db = mysql_connect("localhost", "root");



mysql_select_db("mydb",$db);



$result = mysql_query("SELECT * FROM employees",$db);



printf("First Name: %s<br>\n", mysql_result($result,0,"first"));



printf("Last Name: %s<br>\n", mysql_result($result,0,"last"));



printf("Address: %s<br>\n", mysql_result($result,0,"address"));



printf("Position: %s<br>\n", mysql_result($result,0,"position"));



?>
thats the code i got form the tutorial,

check your private msg
Post Reply