Thauwa Posted December 31, 2008 Share Posted December 31, 2008 I am making an online game. In it you earn gold coins. What is the code to add one gold coin on clicking a button? Any help will be appreciated. Link to comment https://forums.phpfreaks.com/topic/138958-adding-gold-from-stock-on-click/ Share on other sites More sharing options...
Thauwa Posted December 31, 2008 Author Share Posted December 31, 2008 I simply want to know how to add a numeric value to a field Link to comment https://forums.phpfreaks.com/topic/138958-adding-gold-from-stock-on-click/#findComment-726777 Share on other sites More sharing options...
Absorbator Posted December 31, 2008 Share Posted December 31, 2008 Do you store the state of a user somewhere, like MySQL table.... Please give more details about what do you actually want to do and if it's possible - a peace of code of your script. Link to comment https://forums.phpfreaks.com/topic/138958-adding-gold-from-stock-on-click/#findComment-726865 Share on other sites More sharing options...
Absorbator Posted December 31, 2008 Share Posted December 31, 2008 I read your second post again and I think you mean how to increase a numeric value in a MySQL table. Use the field name itself in the mysql query like "fieldName=fieldName+1" Link to comment https://forums.phpfreaks.com/topic/138958-adding-gold-from-stock-on-click/#findComment-726869 Share on other sites More sharing options...
Thauwa Posted January 1, 2009 Author Share Posted January 1, 2009 any further code would be appreciated. how do I make the code edit the mysql data base that way? Link to comment https://forums.phpfreaks.com/topic/138958-adding-gold-from-stock-on-click/#findComment-727396 Share on other sites More sharing options...
Absorbator Posted January 1, 2009 Share Posted January 1, 2009 the MySQL query should be like this <?php $query=mysql_query("UPDATE tableName SET fieldName=fieldName+1 WHERE username=$UserName"); //You can can update multiple fields by separating them by comma, like $query=mysql_query("UPDATE tableName SET Coins=Coins+1, timeField=$Time WHERE username=$UserName"); ?> Link to comment https://forums.phpfreaks.com/topic/138958-adding-gold-from-stock-on-click/#findComment-727419 Share on other sites More sharing options...
Thauwa Posted January 1, 2009 Author Share Posted January 1, 2009 thanks, ill try that. for my knowledge, can you explain to me this part? WHERE username=$UserName"); is 'username' (in the code) a field name or what? Link to comment https://forums.phpfreaks.com/topic/138958-adding-gold-from-stock-on-click/#findComment-727424 Share on other sites More sharing options...
lokie538 Posted January 1, 2009 Share Posted January 1, 2009 Yes it is a field, Absorbator assumes that every player will have their own user account Link to comment https://forums.phpfreaks.com/topic/138958-adding-gold-from-stock-on-click/#findComment-727435 Share on other sites More sharing options...
Absorbator Posted January 1, 2009 Share Posted January 1, 2009 Actually yes, it is. I used it because I didn't know what field names do you have, so feel free to replace it with your index field name. The WHERE clause, tells MySQL which rows to update and if you ignore it, MySQL will update all rows in your table, which is not what you want, do you? Link to comment https://forums.phpfreaks.com/topic/138958-adding-gold-from-stock-on-click/#findComment-727437 Share on other sites More sharing options...
Thauwa Posted January 1, 2009 Author Share Posted January 1, 2009 then I could use the same basis to substract gold coins? I read in another forum topic (posted by Kryllster) a code like this.. <?php function takeDiamond() { $diamond = (Run a query to get amount of diamonds in inventory); if ($diamond > 0) { $diamond--; (Run new query to update the database with new diamond amount); return true; } else { return false; } ?> or can I simply change the signs in absorbater's code? + to -. like tableName SET Coins=Coins-1 my next problem is the 'on click' part, as in the forum topic. can anyone help me with that? Link to comment https://forums.phpfreaks.com/topic/138958-adding-gold-from-stock-on-click/#findComment-727443 Share on other sites More sharing options...
Absorbator Posted January 1, 2009 Share Posted January 1, 2009 About the substract - yes, this tableName SET Coins=Coins-1 is valid. About the "on click" part - The takeDiamond function you provided is not bad. Try this: <?php function takeDiamond() { $query=mysql_query("SELECT * FROM tableName WHERE userName=$User"); $fetch_query=mysql_fetch_array($query); $diamond = fetch_query["coins"]; if ($diamond > 0) { $diamond--; //Or $diamond++; if you want to add coins if(mysql_query("UPDATE tableName SET coins='$diamond' WHERE userName=$User")) //Remember, I use userName field, but you can use any other field names return true; else return false; } else { return false; } ?> Then call the function on the beginning of the files you need. But remember this - You have to specify the User Name as it is required by the function. You can pass it as an argument to the function or refer to the Variable that holds user name or id as a GLOBAL variable like: <?php function takeDiamond() { $User = $GLOBALS["User"]; //This is the new thing.The "User" between "[" and "]" is name of a global variable (means that is outside the function). Replace it with the name of the actual variable that holds user name or user id $query=mysql_query("SELECT * FROM tableName WHERE userName=$User"); $fetch_query=mysql_fetch_array($query); $diamond = fetch_query["coins"]; if ($diamond > 0) { $diamond--; //Or $diamond++; if you want to add coins if(mysql_query("UPDATE tableName SET coins='$diamond' WHERE userName=$User")) //Remember, I use userName field, but you can use any other field names return true; else return false; } else { return false; } ?> One more thing - you have to call mysql_query function as well as mysql_select_db in order to use takeDiamond without errors Tell us if you get problems... Link to comment https://forums.phpfreaks.com/topic/138958-adding-gold-from-stock-on-click/#findComment-727490 Share on other sites More sharing options...
Thauwa Posted January 3, 2009 Author Share Posted January 3, 2009 sorry for being soo dumb, but can I make an error page for the return flase ? how can I? Link to comment https://forums.phpfreaks.com/topic/138958-adding-gold-from-stock-on-click/#findComment-728512 Share on other sites More sharing options...
Nitroware Posted January 3, 2009 Share Posted January 3, 2009 ya, you mean like this: <?php if (takeDiamond(vars...)){ do whatever is right... }else{ do whatever when it returns false } ?> or <?php $var=takeDiamond(vars...) if ($var){ do whatever is right... }else{ do whatever when it returns false } ?> Link to comment https://forums.phpfreaks.com/topic/138958-adding-gold-from-stock-on-click/#findComment-728521 Share on other sites More sharing options...
Thauwa Posted January 3, 2009 Author Share Posted January 3, 2009 that means i could use the header() function after 'do'? Link to comment https://forums.phpfreaks.com/topic/138958-adding-gold-from-stock-on-click/#findComment-728630 Share on other sites More sharing options...
DarkSuperHero Posted January 3, 2009 Share Posted January 3, 2009 header() function can only beused before anything is output to the browser...if this code is at the top of your script and nothign has been sent to the browser then yes....otherwise...no....its all about placement Link to comment https://forums.phpfreaks.com/topic/138958-adding-gold-from-stock-on-click/#findComment-728649 Share on other sites More sharing options...
DarkSuperHero Posted January 3, 2009 Share Posted January 3, 2009 try reading this...is my last post didnt make much sence... http://www.phpfreaks.com/forums/index.php/topic,37442.0.html Link to comment https://forums.phpfreaks.com/topic/138958-adding-gold-from-stock-on-click/#findComment-728651 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.