Jump to content

Can someone please explain this code to me?


Sprout

Recommended Posts

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.  :-\

Link to comment
Share on other sites

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 />';

?>

Link to comment
Share on other sites

:shrug: 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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 />';

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.