I have a php code that updates mysql tables with results of a fight.
When you press the button fight you excetude the below:
if(isset($_POST['attack_creature'])){
if($monnewhealth > "0"){ //if Monster exists
if($charhealth > "0"){ //if Character is alive go to fight
$fightcreature = "UPDATE user_character SET current_action_points = current_action_points-$fight_action_points WHERE ID = $currentUser AND current_action_points>$fight_action_points";
$stmt = $con->prepare($fightcreature);
$stmt->execute();
if($totalinflicteddamagetocreature > "0") {
$monnewhealth = $monnewhealth - $totalinflicteddamagetocreature;
if($monnewhealth < "0") {
$monnewhealth = 0;
}
$updatenmonewhealth = "UPDATE user_character SET fight_creature_new_health = $monnewhealth WHERE ID = $currentUser";
$stmt = $con->prepare($updatenmonewhealth);
$stmt->execute();
}
if($monnewhealth <= "0"){
$lastFight = $now_time;
$updatecharlastfightkills = "UPDATE user_character SET character_last_fight = $now_time, character_kills = $charkills+1, character_gold = $chargold+$mongoldreward, character_current_xp = $charexp+$monxpreward, current_xp_reward = $monxpreward, current_gold_reward = $mongoldreward WHERE ID = $currentUser";
$stmt = $con->prepare($updatecharlastfightkills);
$stmt->execute();
$insertbattlelog1 = "INSERT INTO battle_log (ID, battle_log_date, battle_log_result, battle_log_enemy_name, battle_log_enemy_lvl, battle_log_gold, battle_log_xp, battle_log_event) VALUES ('$currentUser', '$now_time', '1', '$monname', '$monlvl', '$charlastgoldreward', '$charlastxpreward', 'You have Destroyed Level $monlvl $monname and earned $monxpreward XP and $mongoldreward')";
mysqli_query($con, $insertbattlelog1);
}
if($monnewhealth > "0"){ //if Monster still alive
if($totalinflicteddamagetocharacter > "0") {
$charhealth = $charhealth - $totalinflicteddamagetocharacter;
if($charhealth < "0") {
$charhealth = 0;
}
$updatecharnewhealth = "UPDATE user_character SET current_health = $charhealth WHERE ID = $currentUser";
$stmt = $con->prepare($updatecharnewhealth);
$stmt->execute();
}
if($charhealth <= "0"){
$updatecharlastfightdeaths = "UPDATE user_character SET character_last_fight = $now_time, character_deaths = $chardeaths+1 WHERE ID = $currentUser";
$stmt = $con->prepare($updatecharlastfightdeaths);
$stmt->execute();
$insertbattlelog2 = "INSERT INTO battle_log (ID, battle_log_date, battle_log_result, battle_log_enemy_name, battle_log_enemy_lvl, battle_log_event) VALUES ('$currentUser', '$now_time', '2', '$monname', '$monlvl', '$charlastgoldreward', '$charlastxpreward', 'You have been killed by Level $monlvl $monname')";
mysqli_query($con, $insertbattlelog2);
}
}
}
}
header('Location: hunt.php');
}
I don't know how to repeat this process until monhealth or charhealth reach 0 Zero.
Also I want to log how many rounds took to reach 0 Zero and log every round totaldamages.
Many thank you in advance,
Chris
You can write a function and call it recursively with a condition.
function doIt() {
if($condition > 0) {
doIt();
}
}
doIt();
instead of using if just use while so if the $monnewhealth is still greater than 0 i will rerun the code block. and also add new variable $rounds thats increase on each round. something like the code below.
$rounds = 0;
while ($monnewhealth > "0") { // if($monnewhealth > "0"){
$rounds++; // this will cound your rounds
while ($charhealth > "0") { // if($charhealth > "0"){
// your huge code block here
}
}
A while loop would work in this case:
$roundCount = 0;
while($monnewhealth > 0 && $charhealth > 0) {
++$roundCount;
// Your current code
}
Related
So I thought this would be a simple query to just delete rows that didn't have any data stored under certain columns, but for some reason my query is returning that zero rows have been deleted, I checked the table and they are still there.
What I want to do is delete from my gps_routes table where the route_lat and route_long do not contain a location (empty).
I have checked my to make sure I have delete permissions enabled as well.
$sql = "SELECT * FROM gps_routes";
$result = $link->query($sql);
$rowCount = $result->num_rows; $rows_deleted = 0; $delete_row = false;
if ($rowCount > 0)
{
while($row = $result->fetch_assoc())
{
$user = $row['user_email'];
$id = $row['route_id'];
$lat = $row['route_lat'];
$lng = $row['route_long'];
if (empty($lat) || empty($lng)){
$delete_row = true;
}
if (ctype_space($lat) || strlen(trim($lat)) == 0){
$delete_row = true;
}
if ($lat == '' || $lat == ""){
$delete_row = true;
}
if ($delete_row){
$rows_deleted++;
mysqli_query($link, "DELETE FROM gps_routes WHERE user_email = '$user' AND route_id = '$id'");
}
}
echo "Routes deleted: $rows_deleted";
}
From your code is suggest that you just want to go through your DB and check to see if the lat and long are empty. If they are then delete them.
Sounds like you can just use this query to get the job done.
mysqli_query($link, "DELETE FROM gps_routes WHERE (route_lat = '' OR route_lat IS NULL) OR (route_long = '' OR route_long IS NULL)");
This is how I would do it based off the code you have provided:
$query = "DELETE FROM gps_routes WHERE (route_lat = '' OR route_lat IS NULL) OR (route_long = '' OR route_long IS NULL)";
$result = $link->query($query);
echo 'Routes deleted: ' . $result->num_rows;
i have a code here that
if the $cost <= $row2['cost_disbursed'], it will execute the if statement here
but, it still deducts the values even if it is less than the value
while ( ($row = mysqli_fetch_array($query_result)) && ($row2 =
mysqli_fetch_array($query_result2)) ) {
if ( $cost <= $row2['cost_disbursed'] ) {
if ($cost <= $row['remaining']){
$sql= "UPDATE project set cost_disbursed = cost_disbursed - '$cost'
WHERE id = '$id'";
$result = mysqli_query($con,$sql);
$sql2= "UPDATE budget set remaining = remaining - '$cost' where id='1'";
$result2 = mysqli_query($con,$sql2);
echo "<script>window.alert('Balance successfully deducted!')
location.href = 'disbursed-Page.php'</script>";
}
}
else {
echo "<script>window.alert('Balance is not enough')
location.href = 'disbursed-Page.php'</script>";
}
}
i have a problem figuring out, how to roll the dice/s, so that the result/s will either do nothing or only UPDATE the selected users inventory.
<?php
if(isset($_SESSION['loggedin']))
{
include 'system/config.php';
//SESSION
$username = $_SESSION['loggedin'];
//selecting id from table users
$sql = "SELECT id FROM users WHERE username ='$username'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
//the user id from users
$user_id = $row['id'];
$sql = "SELECT user_id, size_kg, fish1, fish2, fish3, fish4, fish5, seaweed FROM inventory WHERE user_id='$user_id'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$userId= $row['user_id'];
$fish1 = $row['fish1'];
$fish2 = $row['fish2'];
$fish3 = $row['fish3'];
$fish4 = $row['fish4'];
$fish5 = $row['fish5'];
$seaweed = $row['seaweed'];
//for debug
echo "$userId. id " . "$fish1 . fish1 <br>";
//$CatchProbability: dice roll for Catch Probability (ex: CatchProbability >= 30; echo You cought a $FishType(fish1, fish2, fish3, fish4, fish5, seaweed))
function rollcatch() {
return mt_rand(1,100);
}
echo rollcatch()." catch <br>";//for debug
//$FishType: dice roll for type of Fish (ex: $FishType(fish1) = 1-10 , $FishType(fish2) = 11-20, $FishType(fish4) = 31-40 $FishType(fish5) = 41-50, $FishType(seaweed) = 51-100)
function rolltype() {
return mt_rand(1,100);
}
echo rolltype()." type <br>";//for debug
function catchFish(){
if(rollcatch() < 30){
$rolltype = rolltype();
$result = "";
if($rolltype > 0 && $rolltype<10){
$result = "fish1";
}
else if($rolltype > 10 && $rolltype<=20){
$result = "fish2";
}
else if($rolltype > 20 && $rolltype<=30){
$result = "fish3";
}
else if($rolltype > 30 && $rolltype<=40){
$result = "fish4";
}
else if($rolltype > 40 && $rolltype<=50){
$result = "fish5";
}
else
{
$result="seaweed";
}
$sql = "UPDATE inventory SET $result = $result + 1 WHERE user_id='$userId'";
if(mysqli_query($conn, $sql)){
echo("You caught a $result");
}
}
else
{
echo("You caught nothing...");
}
}
catchFish(); //for debug
}
?>
Please, help me to debug, I get this error on successful catch:
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in ... on line 72
Line 72
<?
if(mysqli_query($conn, $sql)){
echo("You caught a $result");
}
?>
If I understood your question correctly:
if($CatchProbability <= 30) {
$FishType = rolltype();
if ($FishType <= 10) {
$sql = "UPDATE inventory SET fish1 = fish1 +1 WHERE user_id = '$userId'";
if(mysqli_query($conn, $sql))
{
echo " </br> You caught one Fish1.</br>";
}
}
echo 'You caught a '.$FishType.';
Not entirely sure I understood perfectly, but this code:
Checks if you caught a fish (30% chance)
Determines which fish you caught (10% chance for fish 1-5, 50% chance seaweed)
Adds 1 to the caught fish in the database
Outputs a message of which fish you caught
Hopefully this works for you
function catchFish(){
if(rollcatch() < 30){
$rolltype = rolltype();
$result = "";
if($rolltype > 0 && $rolltype<=10){
$result = "fish1";
}
else if($rolltype > 10 && $rolltype<=20){
$result = "fish2";
}
else if($rolltype > 20 && $rolltype<=30){
$result = "fish3";
}
else if($rolltype > 30 && $rolltype<=40){
$result="fish4";
}
else if($rolltype > 40 && $rolltype<=50){
$result="fish5";
}
else
{
$result="seaweed";
}
$sql = "UPDATE inventory SET $result = $result + 1 WHERE user_id='$user_id'";
if(mysqli_query($conn,$sql)){
echo("You caught a $result.");
}
}
else
{
echo("You caught nothing...");
}
}
Please I've been trying to create a function that would query a DB, select from the table, and if the row count is not equal to 6, then a row from the table and repeat (or maybe duplicate) until the row count is equal to 6. I've search for this here in StackOverflow, but didn't get anything close. Please if you have a similar link to this, please post it here, and I'll give it a go.
Here's my code:
//List All active adverts
function showActiveAdverts()
{
$status = 1;
//Build final queries.
$query = mysql_query("SELECT * FROM table WHERE
status = '".mysql_real_escape_string($status)."' ORDER BY rand() LIMIT 6") or die(mysql_error());
$count = mysql_num_rows($query);
$row = mysql_fetch_assoc($query);
# My question here, check if the $count >= 1 && $count != 6, then do getDefaultBannner() and repeat it until it runs for 6 times.
if($count >= 1){
do{
$list[] = $row['id'];
}while($row = mysql_fetch_assoc($query));
return $list;
}
else{ return FALSE; }
}
Here's the code for getDefaultBannner()
function getDefaultBannner()
{
$status = 6;
$query = mysql_query("SELECT id FROM table WHERE status = '".mysql_real_escape_string($status)."' ")
or die(mysql_error());
$count = mysql_num_rows($query);
$row = mysql_fetch_assoc($query);
if($count >= 1){
do{
$list[] = $row['id'];
}while($row = mysql_fetch_assoc($query));
return $list;
}
else{ return FALSE; }
}
Thanks in advance!
You can rewrite your code as this
//List All active adverts
$query = mysql_query("SELECT * FROM table WHERE
status = '".mysql_real_escape_string($status)."' ORDER BY rand() LIMIT 6") or die(mysql_error());
$count = mysql_num_rows($query);
if($count >= 1 && $count != 6)
$list = getDefaultBannner();
else
$list = showActiveAdverts($query);
function showActiveAdverts($query)
{
$status = 1;
//Build final queries.
$row = mysql_fetch_assoc($query);
if($count >= 1){
do{
$list[] = $row['id'];
}while($row = mysql_fetch_assoc($query));
return $list;
}
else{ return FALSE; }
}
I have a script that checks the submitgame table and if both approve1 and approve 2 are not blank it inserts data into clanstats. There is no mysql_error it simply redirects to the header without inserting anything into the clanstats table, so I have no idea what is going on. Below is the code.
<?php
include("user.php");
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM submitgame WHERE id='$id'") or die(mysql_error());
$playerclan = $row['playerclan'];
$opponentclan = $row['opponentclan'];
$win = $row['win'];
$approve1 = $row['approve1'];
$approve2 = $row['approve2'];
if($win == "won") {
$win = 1;
$points = 2;
$win2 = 0;
$points2 = 1;
}
else {
$win = 0;
$points = 1;
$win2 = 1;
$points2 = 2;
}
if($approve1 != "" && $approve2 != "") {
$query=mysql_query("INSERT INTO clanstats (clan, points, wins) VALUES ('$playerclan', '$points', '$win')");
$query=mysql_query("INSERT INTO clanstats (clan, points, wins) VALUES ('$opponentclan', '$points2', '$win2')");
echo mysql_error($query);
}
else {
header("location:../approvegames.php");
}
mysql_close($con);
header("location:../approvegames.php");
?>
<?php
//first off are you connecting, ill presume so
include("user.php");
//sql injection!!!
$id = mysql_real_escape_string($_GET['id']);
$result = mysql_query("SELECT * FROM submitgame WHERE id='$id' limit 1") or die(mysql_error());
//you were missing this
$row=mysql_fetch_array($result);
$playerclan = $row['playerclan'];
$opponentclan = $row['opponentclan'];
$win = $row['win'];
$approve1 = $row['approve1'];
$approve2 = $row['approve2'];
if($win == "won") {
$win = 1;
$points = 2;
$win2 = 0;
$points2 = 1;
}else{
$win = 0;
$points = 1;
$win2 = 1;
$points2 = 2;
}
if($approve1 != "" && $approve2 != "") {
//you can have multiple inserts
$query=mysql_query("INSERT INTO clanstats (clan, points, wins) VALUES
('$playerclan', '$points', '$win'),
('$opponentclan', '$points2', '$win2')");
header("location:../approvegames.php");
//adding die after the header will make sure nothing else gets executed
die();
}else{
header("location:../approvegames.php");
die();
}
//no need to kill the connection as it will close when the script exits
?>
I think you are missing a line. Perhaps something like:
$row = mysql_fetch_row($result)