Jump to content

Problem calculating values and storing them into user profiles...


glmedia

Recommended Posts

I just can't seem to figure out how to do it, maybe it has something to do with lack of sleep. But what I am trying to do is calculate a user's cash balance (variable is $totalcash) based on another calculated variable ($cpp). First I have the code to calculate the first variable (which is a global value for all users in the database), but I can't get the second variable calculated for each individual member or get what I am to even store in the database. Here is my code:

 

<?

if(isset($_POST['txtadrev'])){

$adrev = $_POST['txtadrev'];

$result = mysql_query( "UPDATE revenue SET totaladrev=$adrev");

$result = mysql_query( "UPDATE revenue SET totaladrev=$adrev");

echo '<div align="center" style="color:#006600; padding-top:5px">Successfully calculated and updated.</div>';

}

 

$sql = "select * from users";

$result = mysql_query($sql);

if(mysql_num_rows($result)>0){

$totalpoints = 0;

while (($row = mysql_fetch_array($result)) != false){

$subtotalpoints = 0;

$res = mysql_query("select sum(hits) from urls where userid='".$row['id']."'");

if(mysql_num_rows($result)>0){

$r = mysql_fetch_row($res);

$subtotalpoints = $r[0];

}

if (($subtotalpoints == "") || ($subtotalpoints == null)) $subtotalpoints = "0";

$totalpoints += $subtotalpoints;

}

}

 

$result = mysql_query("select totaladrev from revenue");

$row = mysql_fetch_row($result);

$totaladrev = $row[0];

//$totaladrev = number_format($totaladrev, 2, '.', ''");

 

$cpp = "0.00";

if($totaladrev > 0) {

$cpp = $totalpoints/$totaladrev;

$cpp = number_format($cpp, 2, '.', '');

}

 

$sql = "select * from users where account!='1'";

$result = mysql_query($sql);

if(mysql_num_rows($result)>0){

while (($row = mysql_fetch_array($result)) != false){

$cash = 0;

$res = mysql_query("select sum(hits) from urls where userid='".$row['id']."'");

if(mysql_num_rows($result)>0){

$r = mysql_fetch_row($res);

$cash += $r[0] * $cpp;

}

if (($cash == "") || ($cash == null)) $cash = "0.00";

}

$cash = round($cash);

$cash = number_format($cash, 2, '.', '');

$cash = "$".$cash;

}

$content .= '<form action="memberstat.php" method="post" name="f">';

$content .= '<center>';

$content .= '<table border="0" class="grid" style="width:800px" cellpadding=0 cellspacing=0>';

$content .= '<tr class="grid-column-title">

<td class="grid-column-title-cell" align="left" valign="center" >Total Points: </td>

<td class="grid-column-title-cell" align="left" valign="center"><b>'.$totalpoints.'</b></td>

<td class="grid-column-title-cell" align="left" valign="center" >Total Ad Revenue: </td>

<td class="grid-column-title-cell" align="left" valign="center"><input width="50px" type="text"

 

name="txtadrev" id="txtadrev" value='.$totaladrev.' /></td>

<td class="grid-column-title-cell" align="left" valign="center" >Cash Per Point: </td>

<td class="grid-column-title-cell" align="left" valign="center" ><b>'.$cpp.'</b></td>

<td class="grid-column-title-cell" align="left" valign="center" >Cash: </td>

<td class="grid-column-title-cell" align="left" valign="center" ><b>'.$cash.'</b></td>

<td class="grid-column-title-cell" align="left" valign="center"><input type="submit" id="btncalculate"

 

name="btncalculate" value="Cash Calculate"></td>';

$content .= '</table></center></form>';

echo $content.'</br>';

 

if(isset($_GET['p']) && isset($_GET['r']) && isset($_GET['uid'])) {

 

if($_GET['r'] > 0) {

$cpp = $_GET['p'] / $_GET['r'];

$cash = $_GET['p'] * $cpp;

update_rev_n_cash($_GET['r'], $cash, $_GET['uid']);

}else if($_GET['r'] == 0) {

$cpp = 0;

$cash = $_GET['p'] * $cpp;

update_rev_n_cash($_GET['r'], $cash, $_GET['uid']);

}

}

 

function update_rev_n_cash($rev, $cash, $userid) {

 

$rev = number_format($rev, 2, '.', '');

$cash = number_format($cash, 2, '.', '');

 

$result = mysql_query("UPDATE users SET totaladrev='".$rev."', cash='".$cash."' WHERE id='".$userid."'");

if(mysql_affected_rows() > 0)

echo '<div align="center" style="color:#006600">Successfully calculated and updated.</div>';

}

$content = '<center>';

$content .= '<table class="grid" style="width:50%" cellpadding=0 cellspacing=0>';

$content .= '<tr class="grid-column-title">

<td class="grid-column-title-cell" align="left" valign="center">Username</td>

<td class="grid-column-title-cell" align="left" valign="center">Email</td>

<td class="grid-column-title-cell" align="left" valign="center">Total Cash</td>

<td class="grid-column-title-cell" align="left" valign="center">Total Points</td></tr>';

$sql = "select * from users";

$result = mysql_query($sql);

if(mysql_num_rows($result)>0){

$rownum = 1;

while (($row = mysql_fetch_array($result)) != false){

 

$totalpoints = 0;

$res = mysql_query("select sum(hits) from urls where userid='".$row['id']."'");

if(mysql_num_rows($result)>0){

$r = mysql_fetch_row($res);

$totalpoints = $r[0];

}

if (($totalpoints == "") || ($totalpoints == null)) $totalpoints = "0";

 

if($rownum%2!=0) { // Odd row

$content .= '<tr class="grid-row-odd">

<td class="grid-row-odd-cell" align="left" valign="center">'.$row["username"].'</td>

<td class="grid-row-odd-cell" align="left" valign="center">'.$row["email"].'</td>

<td class="grid-column-title-cell" align="left" valign="center" width="50px">'.$totalcash.'</td>

<td class="grid-row-odd-cell" align="left" valign="center">'.$totalpoints.'</td></tr>';

}

else { // Even row

$content .= '<tr class="grid-row-even">

<td class="grid-row-even-cell" align="left" valign="center">'.$row["username"].'</td>

<td class="grid-row-even-cell" align="left" valign="center">'.$row["email"].'</td>

<td class="grid-column-title-cell" align="left" valign="center" width="50px"><b>'.$row

 

["totalcash"].'</b></td>

<td class="grid-row-even-cell" align="left" valign="center">'.$totalpoints.'</td></tr>';

}

$rownum++;

}

$content .= '</table></center>';

echo $content;

}

?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.