...

View Full Version : Help with negative problems



Denver
03-31-2007, 12:23 AM
I have quite afew prolems with people putting negative numbers in the fields and it messes with my game, for example when they buy negative mercinates it floods the market with mercinarys and it they get gold they do not deserve...

here is the code please tell me to make it so they cannot put a negative number


<?
include "vsys.php";
if ($cgi['buy_merc']){
$merc=getCommonInfo();

if ($cgi['mercs_attack']){
if ($cgi['mercs_attack']>$merc->attackSpecCount)$cgi['mercs_attack']=$merc->attackSpecCount;
$gold=$cgi['mercs_attack']*$conf["mercenaries"]["atack"]["prise"];
$user=getUserDetails($_SESSION['isLogined']," gold ");
if ($user->gold>$gold){
updateMercenary(" attackSpecCount=attackSpecCount-{$cgi['mercs_attack']} ");
updateUser($_SESSION['isLogined'], " trainedAttackMerc=trainedAttackMerc+{$cgi['mercs_attack']} , gold=gold-$gold ");
}else{
$strErr="You do not have enough money";
}
}
if ($cgi['mercs_defend']){
if ($cgi['mercs_defend']>$merc->defSpecCount)$cgi['mercs_defend']=$merc->defSpecCount;
$gold=$cgi['mercs_defend']*$conf["mercenaries"]["defence"]["prise"];
$user=getUserDetails($_SESSION['isLogined']," gold ");
if ($user->gold>$gold){
updateMercenary(" defSpecCount =defSpecCount -{$cgi['mercs_defend']} ");
updateUser($_SESSION['isLogined'], " trainedDefMerc=trainedDefMerc+{$cgi['mercs_defend']} , gold=gold-$gold ");
}else{
$strErr="You do not have enough money";
}
}
if ($cgi['mercs_general']){
if ($cgi['mercs_general']>$merc->untrainedCount)$cgi['mercs_general']=$merc->untrainedCount;
$gold=$cgi['mercs_general']*$conf["mercenaries"]["untrained"]["prise"];
$user=getUserDetails($_SESSION['isLogined']," gold ");
if ($user->gold>$gold){
updateMercenary(" untrainedCount =untrainedCount -{$cgi['mercs_general']} ");
updateUser($_SESSION['isLogined'], " untrainedMerc=untrainedMerc+{$cgi['mercs_general']} , gold=gold-$gold ");
}else{
$strErr="You do not have enough money";
}
}
header("Location: mercs.php?strErr=$strErr");
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0037)http://www.kingsofchaos.com/mercs.php -->
<HTML><HEAD><TITLE>Combat Warriors! :: Massively Multiplayer Online Role Playing Game</TITLE>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1"><!-- ZoneLabs Privacy Insertion -->
<SCRIPT language=javascript src="js/js"></SCRIPT>
<LINK href="css/common.css" type=text/css rel=stylesheet><LINK
href="css/main.css" type=text/css rel=stylesheet>
<META
content="kingsofchaos, chaos, elves, humans, dwarves, orcs, rpg, mmorpg, role playing, game, online game, text based game, armory, mercenaries, spy, attack, army, battle, recruit, spies, spy skill, weapons, messaging, sabotage, recon, intelligence, pnp, mud, games, stockade, free, browser game"
name=keywords>
<META
content="Combat Warriors is a Massively Multiplayer Online Role Playing Game. Players can choose one of four races: Orcs, Humans, Elves and Dwarves and build armies, recruit friends as officers, buy weapons, and spy and attack on each other."
name=description>
<META content="Kings of Chaos" name=author>
<SCRIPT language=javascript type=text/javascript>
<!--
function checkCR(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}
document.onkeypress = checkCR;
//-->
</SCRIPT>

<META content="MSHTML 5.50.4522.1800" name=GENERATOR></HEAD>
<BODY text=#ffffff bgColor=#000000 leftMargin=0 topMargin=0 marginheight="0"
marginwidth="0">
<?
include "top.php";
?>

<TABLE cellSpacing=0 cellPadding=5 width="100%" border=0>
<TBODY>
<TR>
<TD class=menu_cell style="PADDING-LEFT: 15px" vAlign=top width=140>
<?
include ("left.php");
?>

</TD>
<TD style="PADDING-RIGHT: 15px; PADDING-LEFT: 15px; PADDING-TOP: 12px"
vAlign=top align=left> <? if ($cgi['strErr']){ echo "<center><font color=red>{$cgi['strErr']}</font></center>"; } ?><BR>
<H3>Mercenaries</H3>
<TABLE class=table_lines cellSpacing=0 cellPadding=6 width="380"
border=0>
<TBODY>
<TR>
<TH colSpan=2>Personnel</TH>
</TR>
<TR>
<TD><B>Trained Attack Soldiers</B></TD>
<TD align=right>
<? numecho ($user->trainedAttackSold) ?>
</TD>
</TR>
<TR>
<TD><B>Trained Attack Mercenaries</B></TD>
<TD align=right>
<? numecho ($user->trainedAttackMerc) ?>
</TD>
</TR>
<TR>
<TD><B>Trained Defense Soldiers</B></TD>
<TD align=right>
<? numecho ($user->trainedDefSold)?>
</TD>
</TR>
<TR>
<TD><B>Trained Defense Mercenaries</B></TD>
<TD align=right>
<? numecho ($user->trainedDefMerc) ?>
</TD>
</TR>
<TR>
<TD><B>Untrained Soldiers</B></TD>
<TD align=right>
<? numecho ($user->untrainedSold) ?>
</TD>
</TR>
<TR>
<TD><B>Untrained Mercenaries</B></TD>
<TD align=right>
<? numecho ($user->untrainedMerc) ?>
</TD>
</TR>
<TR>
<TD class=subh><B>Spies</B></TD>
<TD class=subh align=right>
<? numecho ($user->spies) ?>
</TD>
</TR>
<TR>
<TD><B>Total Fighting Force</B></TD>
<TD align=right>
<? numecho (getTotalFightingForce($user))?>
</TD>
</TR>
</TBODY>
</TABLE>
<P>
<?
$merc=getCommonInfo();
?>
<FORM action=mercs.php method=post>
<TABLE class=table_lines cellSpacing=0 cellPadding=6 width="100%"
border=0>
<TBODY>
<TR>
<TH align=middle colSpan=4>Buy Mercenaries</TH></TR>
<TR>
<TH class=subh align=left>Mercenary Type</TH>
<TH class=subh align=right>Cost Per Unit</TH>
<TH class=subh align=right>Quantity Available</TH>
<TH class=subh align=middle>Quantity to Buy</TH></TR>
<TR>
<TD>Attack Specialist</TD>
<TD align=right><? numecho ($conf["mercenaries"]["atack"]["prise"]) ?> Gold</TD>
<TD align=right><? numecho ($merc->attackSpecCount);?></TD>
<TD align=middle><INPUT size=3 value=0 name=mercs_attack></TD></TR>
<TR>
<TD>Defense Specialist</TD>
<TD align=right>
<? numecho ($conf["mercenaries"]["defence"]["prise"]) ?>
Gold</TD>
<TD align=right>
<? numecho ($merc->defSpecCount );?>
</TD>
<TD align=middle><INPUT size=3 value=0 name=mercs_defend></TD></TR>
<TR>
<TD>Untrained</TD>
<TD align=right>
<? numecho ($conf["mercenaries"]["untrained"]["prise"]) ?>
Gold</TD>
<TD align=right>
<? numecho ($merc->untrainedCount );?>
</TD>
<TD align=middle><INPUT size=3 value=0 name=mercs_general></TD></TR>
<TR>
<TD align=middle colSpan=4><INPUT type=submit value=Buy>
</TD></TR></TBODY></TABLE><INPUT type=hidden
value=93aa2b6a02603399396203313b45827e name=hash>
<INPUT type=hidden value=1 name=buy_merc></FORM>
<?
include ("bottom.php");
?>
</TD></TR></TBODY></TABLE>
</BODY></HTML>

Nightfire
03-31-2007, 01:34 AM
$str = str_replace("-","",$str);

Simplist way I can think of at this time.

mlseim
03-31-2007, 04:20 AM
I would use PHP absolute function:
http://php.about.com/od/phpfunctions/g/absolute_value.htm

The number will always be positive.

PolloX
05-29-2007, 07:41 PM
$str = str_replace("-","",$str);

Simplist way I can think of at this time.

hey, i have exactly the same problem, i have added the
$str = str_replace("-","",$str); code into my mercs.php source code but it still allows the negative users to enter a negative number.

here is MY source code, hopefully this helps:


<?
include "vsys.php";
if ($cgi['buy_merc']){
$merc=getCommonInfo();

if ($cgi['mercs_attack']){
if ($cgi['mercs_attack']>$merc->attackSpecCount)$cgi['mercs_attack']=$merc->attackSpecCount;
$gold=$cgi['mercs_attack']*$conf["mercenaries"]["atack"]["prise"];
$user=getUserDetails($_SESSION['isLogined']," gold ");
if ($user->gold>$gold){
updateMercenary(" attackSpecCount=attackSpecCount-{$cgi['mercs_attack']} ");
updateUser($_SESSION['isLogined'], " trainedAttackMerc=trainedAttackMerc+{$cgi['mercs_attack']} , gold=gold-$gold ");
}else{
$str = str_replace("-","",$str);
$strErr="You do not have enough money";
}
}
if ($cgi['mercs_defend']){
if ($cgi['mercs_defend']>$merc->defSpecCount)$cgi['mercs_defend']=$merc->defSpecCount;
$gold=$cgi['mercs_defend']*$conf["mercenaries"]["defence"]["prise"];
$user=getUserDetails($_SESSION['isLogined']," gold ");
if ($user->gold>$gold){
updateMercenary(" defSpecCount =defSpecCount -{$cgi['mercs_defend']} ");
updateUser($_SESSION['isLogined'], " trainedDefMerc=trainedDefMerc+{$cgi['mercs_defend']} , gold=gold-$gold ");
}else{
$str = str_replace("-","",$str);
$strErr="You do not have enough money";

}
}
if ($cgi['mercs_general']){
if ($cgi['mercs_general']>$merc->untrainedCount)$cgi['mercs_general']=$merc->untrainedCount;
$gold=$cgi['mercs_general']*$conf["mercenaries"]["untrained"]["prise"];
$user=getUserDetails($_SESSION['isLogined']," gold ");
if ($user->gold>$gold){
updateMercenary(" untrainedCount =untrainedCount -{$cgi['mercs_general']} ");
updateUser($_SESSION['isLogined'], " untrainedMerc=untrainedMerc+{$cgi['mercs_general']} , gold=gold-$gold ");
}else{
$str = str_replace("-","",$str);
$strErr="You do not have enough money";
}
}
header("Location: mercs.php?strErr=$strErr");
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0037)http://www.kingsofchaos.com/mercs.php -->
<HTML><HEAD><TITLE>Crusaders of War :: Massively Multiplayer Online Role Playing Game</TITLE>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1"><!-- ZoneLabs Privacy Insertion -->
<SCRIPT language=javascript src="js/js"></SCRIPT>
<LINK href="css/common.css" type=text/css rel=stylesheet><LINK
href="css/Elves.css" type=text/css rel=stylesheet>
<META
content="crusadersofwar, war, elves, humans, dwarves, orcs, rpg, mmorpg, role playing, game, online game, text based game, armory, mercenaries, spy, attack, army, battle, recruit, spies, spy skill, weapons, messaging, sabotage, recon, intelligence, pnp, mud, games, stockade, free, browser game"
name=keywords>
<META
content="Crusaders of War is a Massively Multiplayer Online Role Playing Game. Players can choose one of four races: Orcs, Humans, Elves or Dwarves. Command and army, compete against friends, recruit officers, buy weapons, and spy and attack on each other."
name=description>
<META content="Crusaders of War" name=author>
<SCRIPT language=javascript type=text/javascript>
<!--
function checkCR(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}
document.onkeypress = checkCR;
//-->
</SCRIPT>

<META content="MSHTML 5.50.4522.1800" name=GENERATOR></HEAD>
<BODY text=#ffffff bgColor=#000000 leftMargin=0 topMargin=0 marginheight="0"
marginwidth="0">
<?
include "top.php";
?>

<TABLE cellSpacing=0 cellPadding=5 width="100%" border=0>
<TBODY>
<TR>
<TD class=menu_cell style="PADDING-LEFT: 15px" vAlign=top width=140>
<?
include ("left.php");
?>

</TD>
<TD style="PADDING-RIGHT: 15px; PADDING-LEFT: 15px; PADDING-TOP: 12px"
vAlign=top align=left> <? if ($cgi['strErr']){ echo "<center><font color=red>{$cgi['strErr']}</font></center>"; } ?><BR>
<H3>Mercenaries</H3>
<TABLE class=table_lines cellSpacing=0 cellPadding=6 width="380"
border=0>
<TBODY>
<TR>
<TH colSpan=2>Personnel</TH>
</TR>
<TR>
<TD><B>Trained Attack Soldiers</B></TD>
<TD align=right>
<? numecho ($user->trainedAttackSold) ?>
</TD>
</TR>
<TR>
<TD><B>Trained Attack Mercenaries</B></TD>
<TD align=right>
<? numecho ($user->trainedAttackMerc) ?>
</TD>
</TR>
<TR>
<TD><B>Trained Defense Soldiers</B></TD>
<TD align=right>
<? numecho ($user->trainedDefSold)?>
</TD>
</TR>
<TR>
<TD><B>Trained Defense Mercenaries</B></TD>
<TD align=right>
<? numecho ($user->trainedDefMerc) ?>
</TD>
</TR>
<TR>
<TD><B>Untrained Soldiers</B></TD>
<TD align=right>
<? numecho ($user->untrainedSold) ?>
</TD>
</TR>
<TR>
<TD><B>Untrained Mercenaries</B></TD>
<TD align=right>
<? numecho ($user->untrainedMerc) ?>
</TD>
</TR>
<TR>
<TD class=subh><B>Spies</B></TD>
<TD class=subh align=right>
<? numecho ($user->spies) ?>
</TD>
</TR>
<TR>
<TD><B>Total Fighting Force</B></TD>
<TD align=right>
<? numecho (getTotalFightingForce($user))?>
</TD>
</TR>
</TBODY>
</TABLE>
<P>
<?
$merc=getCommonInfo();
?>
<FORM action=mercs.php method=post>
<TABLE class=table_lines cellSpacing=0 cellPadding=6 width="100%"
border=0>
<TBODY>
<TR>
<TH align=middle colSpan=4>Buy Mercenaries</TH></TR>
<TR>
<TH class=subh align=left>Mercenary Type</TH>
<TH class=subh align=right>Cost Per Unit</TH>
<TH class=subh align=right>Quantity Available</TH>
<TH class=subh align=middle>Quantity to Buy</TH></TR>
<TR>
<TD>Attack Specialist</TD>
<TD align=right><? numecho ($conf["mercenaries"]["atack"]["prise"]) ?> Gold</TD>
<TD align=right><? numecho ($merc->attackSpecCount);?></TD>
<TD align=middle><INPUT size=3 value=0 name=mercs_attack></TD></TR>
<TR>
<TD>Defense Specialist</TD>
<TD align=right>
<? numecho ($conf["mercenaries"]["defence"]["prise"]) ?>
Gold</TD>
<TD align=right>
<? numecho ($merc->defSpecCount );?>
</TD>
<TD align=middle><INPUT size=3 value=0 name=mercs_defend></TD></TR>
<TR>
<TD>Untrained</TD>
<TD align=right>
<? numecho ($conf["mercenaries"]["untrained"]["prise"]) ?>
Gold</TD>
<TD align=right>
<? numecho ($merc->untrainedCount );?>
</TD>
<TD align=middle><INPUT size=3 value=0 name=mercs_general></TD></TR>
<TR>
<TD align=middle colSpan=4><INPUT type=submit value=Buy>
</TD></TR></TBODY></TABLE><INPUT type=hidden
value=93aa2b6a02603399396203313b45827e name=hash>
<INPUT type=hidden value=1 name=buy_merc></FORM>

<?
include ("bottom.php");
?>
</TD></TR></TBODY></TABLE>
</BODY></HTML>


Thanks :thumbsup: :thumbsup: :thumbsup:

rafiki
05-29-2007, 07:52 PM
I would use PHP absolute function:
http://php.about.com/od/phpfunctions/g/absolute_value.htm

The number will always be positive.

take note to the last line :P
The number will always be positive.

PolloX
05-29-2007, 08:12 PM
take note to the last line :P
The number will always be positive.

okay, so where do i put that...

<?php
abs (-6) ;
//returns a value of 6

abs (12.4) ;
//returns a value of 12.4

abs (-456.2) ;
// returns a value of 456.2
?>

whizard
05-29-2007, 08:27 PM
One reason the str_replace thing didn't work for you is that it was an example - you need to actually customize it to your site, by replacing $str with whatever variable it is that you want to be positive.

for the absolute value, if $var is the name of the variable you want to be positive, do this after the user puts his value in for it:

$var = abs($var);

(replacing $var with your variable name of course)

HTH
Dan

PolloX
05-29-2007, 08:33 PM
omg this is soo confusing, im a noob at php... please be patient with me... thnks

marek_mar
05-29-2007, 11:57 PM
take note to the last line :P
The number will always be positive.
That's actually wrong. It should say that the number will be always non-negative.

mlseim
05-30-2007, 01:32 AM
are you positive?

marek_mar
05-30-2007, 02:42 PM
Quite. :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum