Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Math in php or MySql

Contributor ,
Jan 07, 2012 Jan 07, 2012

Copy link to clipboard

Copied

I was on a site recently and wondered how they gave points to users.  So I tried to do it on my own and can't seem to do it.

What I've done is I created a basic form.

There is a hiddenfield in the form that holds the users "points" from the database. We'll call this 'current_points'

There is another hiddenfield in the form that holds a pre-determined number (the points for posting the form).   We'll call this "new_points".

And lastly there is yet another hiddenfield in the form that is suppose to hold the total of the two fields above. 

So in other words - if they had 15 points and this post is worth 5 - then the third hiddenfield should end up with 20 in it and that will be updated in the user's points on the database.

      <input name="current_points" type="hidden" id="current_points" value="<?php echo $row_Recordset1['points']; ?>" />

<input name="new_points" type="hidden" id="new_points" value="5" />

      <input type="hidden" name="totalpts" id="totalpts" value="echo "Perform addition: $_POST['pts']; + $_POST['current_points']; = ".$addition."";"/>

This isn't working though.  I'm getting an error saying I have double the same attribute.  So I assume it is only looking at $_POST and not the field I want it to see.

Any thoughts?

TOPICS
Server side applications

Views

430
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 08, 2012 Jan 08, 2012

Copy link to clipboard

Copied

LATEST

I figured out what I was doing wrong.  It was easier than I was making it.  I'll put the solution here in case anyone is curious:

1. I created a basic form with the following elements and their corresponding name:

  • text area - 'comments'
  • hiddenfield1 - 'existing_points'
  • hiddenfield2 - 'total_points'
  • hiddenfield3 - 'user_id'
  • Submit Button - 'Submit'

At the top of my page, before the <head> tag I put:

<?php

$pointsnow=$row_Recordset1['points'] + "2";

?>

So, I have a recordset on the page that is getting their user_id and the amount of points they have.

The code above is taking the points from this recordset and adding an additional '2' to the number.

It is then saying that the answer is = $pointsnow

Then on my hiddenfield2 called "total_points", I changed the code to read as such:

<input name="total_points" type="hidden" id="total_points" value="<?php echo $pointsnow; ?>" />

The value here is echoing $pointsnow (the answer to the code I put above the <head> tag) and updating the database with this number and removing the old number.

I'm pretty excited I figured this out on my own!  : )

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines