Jump to content

Recommended Posts

Hey guys, Been playing about here and I'm completely stuck! I've created a button with a form field that the user can put a number into and submit it. This works out a few things and removes "resources" from the users account in the database depending on the unit and the cost of that unit. (I've done this the only way I could work out how).

 

But the next step I need to do an If I think, to find out of the user has enough resources on the account to build X amount, at current it just makes the resources go into the - numbers.

 

One of the units in unit.php:

<?php	


        $result = mysql_query("SELECT * FROM userdb WHERE username='$_SESSION[user]'") 
                or die(mysql_error());  


		echo "<table border='1' cellpadding='10'>";
			echo "<th colspan='6'>Offence</th>";
               echo "<tr> 
			<th>Unit Name</th> 
			<th>Number</th> 
			<th>Wood Cost</th>
			<th>Iron Cost</th>
			<th>TP Cost</th>
			<th>Create</th>
			</tr>";

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


                echo "<tr>";

                echo '<td>Unit 1</td>';
                echo '<td>' . $row['ounit1'] . '</td>';
			echo '<td>'. $ounit1w . '</td>';
			echo '<td>'. $ounit1i . '</td>';
			echo '<td>'. $ounit1tp . '</td>';
			echo '<td> <form action="create.php" method="post"> 
						<input type="text" name="number" value="" size="5">
						<input type="hidden" name="memberid" value="' . $row['memberid'] . '">
						<input type="hidden" name="unit" value="ounit1">
						<input type="hidden" name="res1cost" value="'. $ounit1w . '">
						<input type="hidden" name="res2cost" value="'. $ounit1i . '">	
						<input type="hidden" name="tpcost" value="'. $ounit1tp . '">							
						<input type="submit" name="submit" value="Create">
						</form>
				  </td>';
                echo "</tr>"; 

 

continues x12 for offence and defence so wont copy it all here.

 

create.php:

 

<?php include ('mysql.php'); ?>

<?php

//unit costs

$ounit1w = 1000;
$ounit1i = 1000;
$ounit1tp = 100;


?>



<?php


$memberid = mysql_real_escape_string($_POST['memberid']); 
$number = $_POST['number'];
$unit = $_POST['unit'];
$unitres1 = $_POST['res1cost'];
$unitres2 = $_POST['res2cost'];
$unittp = $_POST['tpcost'];


    $result = mysql_query("SELECT * FROM userdb WHERE memberid='$memberid'")                 
        or die(mysql_error());  				    	
        while($row = mysql_fetch_array( $result )) {   						
            $unitstart = $row[$unit];	
		$res1 = $row['res1'];
		$res2 = $row['res2'];
		$tp = $row['trainpoints'];
        }



$unitnumber = $unitstart + $number;

$resneeded1 = $number * $unitres1;
$resneeded2 = $number * $unitres2;
$tpneeded = $number * $unittp;

$newres1 = $res1 - $resneeded1;
$newres2 = $res2 - $resneeded2;
$newtp = $tp - $tpneeded;


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



$query=("UPDATE userdb SET $unit = '$unitnumber' WHERE memberid='$memberid'"); 
$query2= ("UPDATE userdb SET res1 = '$newres1' WHERE memberid='$memberid'"); 
$query3= ("UPDATE userdb SET res2 = '$newres2' WHERE memberid='$memberid'"); 
$query4= ("UPDATE userdb SET trainpoints = '$newtp' WHERE memberid='$memberid'"); 

mysql_query($query); 
mysql_query($query2); 
mysql_query($query3); 
mysql_query($query4); 



header('Location: ' . $_SERVER['HTTP_REFERER']);
  }
?>

 

This works, but I've got no idea how to check if $row['res1'] has $resneeded1 and to stop if it doesn't. Could anyone point me in the right direction or show me an example?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/250575-if-conditions/
Share on other sites

Ok, please correct me if I'm wrong

 

What you want is

 

1. a check if the user has a certain amount of offence or defence to go to the next level (example: level one 100 offence 100 defence, level 2 is 200 offence and 200 defence)

2. if he has enough resources it should say, you can't add more resources?

 

please help me get the picture with an example

Link to comment
https://forums.phpfreaks.com/topic/250575-if-conditions/#findComment-1285691
Share on other sites

Sorry guess I wasn't clear.

 

User types in form 10 units.

User Submits form button.

Script works out how much $res1, $res2, $tp are needed per unit and multiplies it by the input (10).

MISSING BIT

Script add's 10 units to selected column.

Script removes the resources needed from res1, res2 and tp columns.

 

What I need is before it add's the units and removes the resources. Is to check if the user has enough units and enough resources to build the number of units input. I'm just not sure how to check it.

Link to comment
https://forums.phpfreaks.com/topic/250575-if-conditions/#findComment-1285693
Share on other sites

I think so.

 

So:

if( ($resneeded1 == $res1 || $resneeded2 == $res2 || $tpneeded == $tp) {
	 echo 'working';
} else {
	echo 'notworking';
	}

 

How would I add that to the create.php file. As I already have a if statement to execute the queries, but I only want to execute them if this returns ok.

Link to comment
https://forums.phpfreaks.com/topic/250575-if-conditions/#findComment-1285702
Share on other sites

Brilliant thanks!

 

For anyone else who is stuck here is the solution:

 

<?php include ('mysql.php'); ?>

<?php

//unit costs

$ounit1w = 1000;
$ounit1i = 1000;
$ounit1tp = 100;

$ounit2w = 800;
$ounit2i = 2500;
$ounit2tp = 200;

$ounit3w = 1200;
$ounit3i = 1500;
$ounit3tp = 300;

$ounit4w = 2000;
$ounit4i = 2000;
$ounit4tp = 450;

$ounit5w = 100;
$ounit5i = 5000;
$ounit5tp = 600;

$ounit6w = 1900;
$ounit6i = 2000;
$ounit6tp = 700;

$ounit7w = 5000;
$ounit7i = 800;
$ounit7tp = 800;

$ounit8w = 2700;
$ounit8i = 2800;
$ounit8tp = 930;

$ounit9w = 7000;
$ounit9i = 1000;
$ounit9tp = 1100;

$ounit10w = 50;
$ounit10i = 10000;
$ounit10tp = 1500;

$ounit11w = 750;
$ounit11i = 15000;
$ounit11tp = 1600;

$ounit12w = 15000;
$ounit12i = 15000;
$ounit12tp = 2000;

///////////////////////////////

$dunit1w = 1000;
$dunit1i = 1000;
$dunit1tp = 100;

$dunit2w = 800;
$dunit2i = 2500;
$dunit2tp = 200;

$dunit3w = 1200;
$dunit3i = 1500;
$dunit3tp = 300;

$dunit4w = 2000;
$dunit4i = 2000;
$dunit4tp = 450;

$dunit5w = 100;
$dunit5i = 5000;
$dunit5tp = 600;

$dunit6w = 1900;
$dunit6i = 2000;
$dunit6tp = 700;

$dunit7w = 5000;
$dunit7i = 800;
$dunit7tp = 800;

$dunit8w = 2700;
$dunit8i = 2800;
$dunit8tp = 930;

$dunit9w = 7000;
$dunit9i = 1000;
$dunit9tp = 1100;

$dunit10w = 50;
$dunit10i = 10000;
$dunit10tp = 1500;

$dunit11w = 750;
$dunit11i = 15000;
$dunit11tp = 1600;

$dunit12w = 15000;
$dunit12i = 15000;
$dunit12tp = 2000;
?>



<?php


$memberid = mysql_real_escape_string($_POST['memberid']); 
$number = $_POST['number'];
$unit = $_POST['unit'];
$unitres1 = $_POST['res1cost'];
$unitres2 = $_POST['res2cost'];
$unittp = $_POST['tpcost'];


    $result = mysql_query("SELECT * FROM userdb WHERE memberid='$memberid'")                 
        or die(mysql_error());  				    	
        while($row = mysql_fetch_array( $result )) {   						
            $unitstart = $row[$unit];	
		$res1 = $row['res1'];
		$res2 = $row['res2'];
		$tp = $row['trainpoints'];
        }



$unitnumber = $unitstart + $number;

$resneeded1 = $number * $unitres1;
$resneeded2 = $number * $unitres2;
$tpneeded = $number * $unittp;

$newres1 = $res1 - $resneeded1;
$newres2 = $res2 - $resneeded2;
$newtp = $tp - $tpneeded;

if ($resneeded1 < $res1 || $resneeded2 < $res2 || $tpneeded < $tp) {

	$query=("UPDATE userdb SET $unit = '$unitnumber' WHERE memberid='$memberid'"); 
	$query2= ("UPDATE userdb SET res1 = '$newres1' WHERE memberid='$memberid'"); 
	$query3= ("UPDATE userdb SET res2 = '$newres2' WHERE memberid='$memberid'"); 
	$query4= ("UPDATE userdb SET trainpoints = '$newtp' WHERE memberid='$memberid'"); 

	mysql_query($query); 
	mysql_query($query2); 
	mysql_query($query3); 
	mysql_query($query4); 



header('Location: ' . $_SERVER['HTTP_REFERER']);
} else {
	echo "Not enough Resources";
	}

?>

 

Only thing, how can I get the error to echo on the page without removing everything? at current it goes to a blank page with 'Not enough Resources'

Link to comment
https://forums.phpfreaks.com/topic/250575-if-conditions/#findComment-1285705
Share on other sites

Well, not sure about that one, but when I wanted to do something like that before

I used a header instead of an echo, so it redirects the user to a different page, just pure html that shows a message with the error and a link to get back to the previous page

 

not sure if that would help you tho

Link to comment
https://forums.phpfreaks.com/topic/250575-if-conditions/#findComment-1285708
Share on other sites

I ended up using this, might help you:

 

 

echo "<div class='error'>Not enough Resources</div>";

include "units.php";

exit;

 

 

Include the original page.

 

.error {
position:absolute;
margin-top:250px;
margin-left:425px;
color:#ff0000;
font-weight:bold;
}

 

Then positioned it with css. - Not sure if its the correct way but it works lol.

 

 

Link to comment
https://forums.phpfreaks.com/topic/250575-if-conditions/#findComment-1285709
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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