Sprout Posted June 26, 2010 Share Posted June 26, 2010 Well, I asked how to create a user point system in PHP on Yahoo answers and someone gave me this response, unfortunately I don't quite understand how it works. PHP and MySQL will get the job done for you. MySQL: CREATE TABLE counter (user_id INT primary key,count INT); form_submit.php: <?php require_once('connect.php'); $result = mysql_query("SELECT count FROM counter WHERE user_id=".$_REQUEST['user_id']); if($result) { $row = mysql_fetch_array($result); $count = $row['count']; if($_REQUEST['mode'] == 'increase') $count += 1; else $count -= 1; mysql_query("UPDATE counter SET count = ".$count." WHERE user_id = ".$_REQUEST['user_id']); } header('Location: some_page.php'); ?> I already have a user database and table for my login system in my MySQL could somebody give me a more detailed as to how to implement this code? I'm very new and am still having trouble grasping some of the most basic things. Any clarification at all would be very much appreciated, although the more detailed the better. Thanks gang. Quote Link to comment https://forums.phpfreaks.com/topic/205948-can-someone-please-explain-this-code-to-me/ Share on other sites More sharing options...
jcbones Posted June 26, 2010 Share Posted June 26, 2010 Explained. <?php require_once('connect.php'); //require a database connection, to include the selection of the database. $result = mysql_query("SELECT count FROM counter WHERE user_id=".$_REQUEST['user_id']); //get the count column from the table for the requested user_id. if($result)//if there was a result returned. { $row = mysql_fetch_array($result); //get that result. $count = $row['count'];//from the count column. if($_REQUEST['mode'] == 'increase') $count += 1;//if a parameter of 'mode' is set to 'increase'. (POSSIBLY a $_GET paramenter (url), but will accept a POST as well.) increment the count. else $count -= 1; //if this mode is not set, or isn't set to increase, subtract from the count. mysql_query("UPDATE counter SET count = ".$count." WHERE user_id = ".$_REQUEST['user_id']);//save the data back to the database. } header('Location: some_page.php');//redirect to some page, you need to decide on. ?> Although, it can be much better. I'm also going to say, this isn't what you are looking for. What it does. 1. you pass it a user id. 2. it looks up that id, and gets the count. 3. it either increments the count, or de-increments the count, one of the two. 4. it saves that data to the database. 5. it re-directs to another page. Quote Link to comment https://forums.phpfreaks.com/topic/205948-can-someone-please-explain-this-code-to-me/#findComment-1077692 Share on other sites More sharing options...
Sprout Posted June 26, 2010 Author Share Posted June 26, 2010 Well shoot, that's disappointing. You wouldn't happen to know how it could be modified to display/create a point system for users do you? I want something that I can add to user pages that gives them their unique number of points associated with their username. Then have forms that can submit or subtract points based on user names. I can't find information anywhere on that, I've asked in both here and yahoo but this was the only real answer I got. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/205948-can-someone-please-explain-this-code-to-me/#findComment-1077696 Share on other sites More sharing options...
jcbones Posted June 26, 2010 Share Posted June 26, 2010 Try something like this: NOTICE, you will have to provide a way for the script to know who the user is. <?php require_once('connect.php'); //require a database connection, to include the selection of the database. $user = 1; //There must be a way to determine who's profile you are looking at. This will be the user. if(isset($user)) { //if we have a user. $user = mysql_real_escape_string($user); //escape them for mysql interaction. if(isset($_GET['points']) && ($_GET['points'] == 'increase' || $_GET['points'] == 'decrease')) { //if the url has 'points' set, and the = one of two things. if($_GET['points'] == 'increase') { //increase the count. $update = 'count + 1'; } elseif($_GET['points'] == 'decrease') { //decrease the count. $update = 'count - 1'; } $sql = "UPDATE counter SET count = $update WHERE user_id = '$user'"; //finalize query. mysql_query($sql); //execute query. } $increasePoints = '<a href="?points=increase">Increase</a>'; //increase points link set to variable. $decreasePoints = '<a href="?points=decrease">Decrease</a>'; //decrease points link set to variable. //Call the data from the db. $sql = "SELECT count FROM counter WHERE user_id = '$user'"; $re = mysql_query($sql); $r = mysql_fetch_row($re); $count = $r[0]; //echo the count and the variables. You can do this on the current page, or any page this code is included in. echo 'User has ' . $count . ' points!<br />Do you want to ' . $increasePoints . ' or ' . $decreasePoints . '?<br />'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/205948-can-someone-please-explain-this-code-to-me/#findComment-1077709 Share on other sites More sharing options...
Sprout Posted June 27, 2010 Author Share Posted June 27, 2010 Well, apparently I need to go back through PHP 101 because I can't get this working at all. I don't understand what part goes where and when I tried to upload it as is so I could tinker with it and get a feel it's giving me a parse error: syntax error, unexpected $end on line 66 Sorry to be such a bother but if anybody can explain this to me in the simplest way possible then I'd really appreciate it. Thanks for your help jcbones, at least I'm making some form of progress. Quote Link to comment https://forums.phpfreaks.com/topic/205948-can-someone-please-explain-this-code-to-me/#findComment-1077761 Share on other sites More sharing options...
jcbones Posted June 27, 2010 Share Posted June 27, 2010 A few of things. 1. Make sure you have a connection script in the same directory, named connect.php It should look something like: <?php // ******** EDIT CONNECTION INFORMATION BELOW ************ $hostname = "localhost"; $database = "test"; $username = "root"; $password = ""; // *********** END EDIT CONNECTION INFORMATION ************ $con = mysql_connect($hostname, $username, $password); mysql_select_db($database, $con); ?> 2. You can then include the current script in any other page by using. include('count.php'); //Or, whatever you named the script. 3. Of course, you can move the echo statement anywhere inside main script, as the variables will be available. Quote Link to comment https://forums.phpfreaks.com/topic/205948-can-someone-please-explain-this-code-to-me/#findComment-1077769 Share on other sites More sharing options...
Sprout Posted June 27, 2010 Author Share Posted June 27, 2010 I'm still getting this error Parse error: syntax error, unexpected $end in /home/public_html/test/logger/count.php on line 66 Also, am I supposed to create something for 'points' as of now I just copied and pasted the script exactly as is. Aside from the connect.php file to connect to my user database I copied and pasted everything as is, which based on the outcome I guess I'm not supposed to be doing. Quote Link to comment https://forums.phpfreaks.com/topic/205948-can-someone-please-explain-this-code-to-me/#findComment-1077789 Share on other sites More sharing options...
Sprout Posted June 28, 2010 Author Share Posted June 28, 2010 Still can't figure it out, anybody? Quote Link to comment https://forums.phpfreaks.com/topic/205948-can-someone-please-explain-this-code-to-me/#findComment-1078000 Share on other sites More sharing options...
kenrbnsn Posted June 28, 2010 Share Posted June 28, 2010 That error indicates that you have either unbalanced "{ }" or unbalanced quotes in your script. Ken Quote Link to comment https://forums.phpfreaks.com/topic/205948-can-someone-please-explain-this-code-to-me/#findComment-1078002 Share on other sites More sharing options...
Sprout Posted June 28, 2010 Author Share Posted June 28, 2010 That error indicates that you have either unbalanced "{ }" or unbalanced quotes in your script. Ken I know I just can't figure out where in the script. <?php require_once('connect.php'); //require a database connection, to include the selection of the database. $user = 1; //There must be a way to determine who's profile you are looking at. This will be the user. if(isset($user)) { //if we have a user. $user = mysql_real_escape_string($user); //escape them for mysql interaction. if(isset($_GET['points']) && ($_GET['points'] == 'increase' || $_GET['points'] == 'decrease')) { //if the url has 'points' set, and the = one of two things. if($_GET['points'] == 'increase') { //increase the count. $update = 'count + 1'; } elseif($_GET['points'] == 'decrease') { //decrease the count. $update = 'count - 1'; } $sql = "UPDATE counter SET count = $update WHERE user_id = '$user'"; //finalize query. mysql_query($sql); //execute query. } $increasePoints = '<a href="?points=increase">Increase</a>'; //increase points link set to variable. $decreasePoints = '<a href="?points=decrease">Decrease</a>'; //decrease points link set to variable. //Call the data from the db. $sql = "SELECT count FROM counter WHERE user_id = '$user'"; $re = mysql_query($sql); $r = mysql_fetch_row($re); $count = $r[0]; //echo the count and the variables. You can do this on the current page, or any page this code is included in. echo 'User has ' . $count . ' points!<br />Do you want to ' . $increasePoints . ' or ' . $decreasePoints . '?<br />'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/205948-can-someone-please-explain-this-code-to-me/#findComment-1078013 Share on other sites More sharing options...
kenrbnsn Posted June 28, 2010 Share Posted June 28, 2010 Here's your code with the blank lines removed: <?php require_once('connect.php'); //require a database connection, to include the selection of the database. $user = 1; //There must be a way to determine who's profile you are looking at. This will be the user. if(isset($user)) { //if we have a user. $user = mysql_real_escape_string($user); //escape them for mysql interaction. if(isset($_GET['points']) && ($_GET['points'] == 'increase' || $_GET['points'] == 'decrease')) { //if the url has 'points' set, and the = one of two things. if($_GET['points'] == 'increase') { //increase the count. $update = 'count + 1'; } elseif($_GET['points'] == 'decrease') { //decrease the count. $update = 'count - 1'; } $sql = "UPDATE counter SET count = $update WHERE user_id = '$user'"; //finalize query. mysql_query($sql); //execute query. } $increasePoints = '<a href="?points=increase">Increase</a>'; //increase points link set to variable. $decreasePoints = '<a href="?points=decrease">Decrease</a>'; //decrease points link set to variable. //Call the data from the db. $sql = "SELECT count FROM counter WHERE user_id = '$user'"; $re = mysql_query($sql); $r = mysql_fetch_row($re); $count = $r[0]; //echo the count and the variables. You can do this on the current page, or any page this code is included in. echo 'User has ' . $count . ' points!<br />Do you want to ' . $increasePoints . ' or ' . $decreasePoints . '?<br />'; ?> The problem is that there is an opening "{" on this line: <?php if(isset($user)) { //if we have a user. ?> and no closing "}" Ken Quote Link to comment https://forums.phpfreaks.com/topic/205948-can-someone-please-explain-this-code-to-me/#findComment-1078015 Share on other sites More sharing options...
Sprout Posted June 28, 2010 Author Share Posted June 28, 2010 Well, I'm going to assume we're a lot closer but now it's telling me that there's the same problem except this time on line 15. Quote Link to comment https://forums.phpfreaks.com/topic/205948-can-someone-please-explain-this-code-to-me/#findComment-1078380 Share on other sites More sharing options...
kenrbnsn Posted June 28, 2010 Share Posted June 28, 2010 Did you put in the missing "}". I didn't since I didn't know where to put it. Ken Quote Link to comment https://forums.phpfreaks.com/topic/205948-can-someone-please-explain-this-code-to-me/#findComment-1078407 Share on other sites More sharing options...
closerwalk Posted June 28, 2010 Share Posted June 28, 2010 I believe it is corret. Sometimes the logic is misplaced and the closing bracket ends in a place you would have not previously assumed. But play with it try and figure it out my frined. These guys gave you good code. Quote Link to comment https://forums.phpfreaks.com/topic/205948-can-someone-please-explain-this-code-to-me/#findComment-1078474 Share on other sites More sharing options...
Sprout Posted June 29, 2010 Author Share Posted June 29, 2010 Alright, well I got the error messages to stop popping up by all I have is a blank page... am I supposed to set something else up I'm unaware of? The count.php code is this... <?php //a require_once('connect.php'); //require a database connection, to include the selection of the database. $user = 1; //There must be a way to determine who's profile you are looking at. This will be the user. if(isset($user)) { //if we have a user. $user = mysql_real_escape_string($user); //escape them for mysql interaction. if(isset($_GET['points']) && ($_GET['points'] == 'increase' || $_GET['points'] == 'decrease')) { //if the url has 'points' set, and the = one of two things. if($_GET['points'] == 'increase') { //increase the count. $update = 'count + 1'; } elseif($_GET['points'] == 'decrease') { //decrease the count. $update = 'count - 1'; } $sql = "UPDATE counter SET count = $update WHERE user_id = '$user'"; //finalize query. mysql_query($sql); //execute query. } $increasePoints = '<a href="?points=increase">Increase</a>'; //increase points link set to variable. $decreasePoints = '<a href="?points=decrease">Decrease</a>'; //decrease points link set to variable. //Call the data from the db. $sql = "SELECT count FROM counter WHERE user_id = '$user'"; $re = mysql_query($sql); $r = mysql_fetch_row($re); $count = $r[0]; //echo the count and the variables. You can do this on the current page, or any page this code is included in. echo 'User has ' . $count . ' points!<br />Do you want to ' . $increasePoints . ' or ' . $decreasePoints . '?<br />'; ?> Is there anything else I need to do to get the point system working? Quote Link to comment https://forums.phpfreaks.com/topic/205948-can-someone-please-explain-this-code-to-me/#findComment-1078548 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.