Jump to content

[SOLVED] Is there a solution for this?


Kryllster

Recommended Posts

Ok thanks for replying!

 

Here is the code;

<?php
// initialize outcome of fight and loot chances
$outcome = mt_rand(1,100);
$level_advance = 2;
$gold = mt_rand(1000,4000);
$diamonds = mt_rand(3,;
$rubies = mt_rand(3,7);

// My Fight and loot generator
$smarty->assign('gold',$gold);
$smarty->assign('rubies',$rubies);
$smarty->assign('diamonds',$diamonds);

// First the fight
if($outcome < 70){

// Connect to database
include('includes/config.php');
include('includes/advance_level.php');

// Update database
$sql="UPDATE $tbl_name SET 
onhand = onhand + $gold,
diamond = diamond + $diamonds,
rubie = rubie + $rubies,
advance = advance + $level_advance
WHERE uname='$uname'";
mysql_query($sql) or die (mysql_error()."<p>$sql</p>");
$smarty->display('fight_success.tpl');
}
else{
$smarty->display('fight_failure.tpl');
}
?>

 

When the link that leads to this it goes thru an index contoller and executes the depending on the out come of course diplays the values like I wanted to, but all the person has to do is hit their back button on over and over wash rinse and repeat. I am learninf alot but this has me stymied. I was trying to do it where it runs the script then exits and directs it to the right page the first version worked better however I could not get the the variable output to the template??

 

 

best solution is to have the form and the submit code on the same page i.e.

 

<?php
// codez
if(isset($_POST["submit"])){
// if form is submitted
} else {
?>
<form action="" method="post">
FORMZ
<input type="submit" value="submit" name="submit">
</form>
<?php
}
?>

Another solution would be to set a session var on the first run of the script which indicates that it has run already, then when running the script check for that session var and if it exists give the user a no-no message instead of running the script again.

 

Something to that effect anyway.

Could you explain how I could do that. Because the solution offered earlier would prolly work but I am using the Smarty template engine which I'm starting to see is a little advanced for me atm. I am thinking of converting to php / html strictly for now till I know more. Of course I am always full of questions so how do I make php templates and use a controller file??

 

Thanks,

I don't know smarty at all. I have stayed away from it as I don't want to learn a "new language" to use PHP which I already know. But to do what I suggested you would do something like this.

 

<?php
session_start(); // start our session
if(!isset($_SESSION['loot_chance_run']))
{
    // if $_SESSION['loot_chance_run] not set, proceed and run it

    $_SESSION['loot_chance_run'] = true; // set the session var now


  // initialize outcome of fight and loot chances
  $outcome = mt_rand(1,100);
  $level_advance = 2;
  $gold = mt_rand(1000,4000);
  $diamonds = mt_rand(3,;
  $rubies = mt_rand(3,7);

   // My Fight and loot generator
  $smarty->assign('gold',$gold);
  $smarty->assign('rubies',$rubies);
  $smarty->assign('diamonds',$diamonds);

  // First the fight
  if($outcome < 70){

  // Connect to database
  include('includes/config.php');
  include('includes/advance_level.php');

  // Update database
  $sql="UPDATE $tbl_name SET 
onhand = onhand + $gold,
diamond = diamond + $diamonds,
rubie = rubie + $rubies,
advance = advance + $level_advance
WHERE uname='$uname'";
  mysql_query($sql) or die (mysql_error()."<p>$sql</p>");
  $smarty->display('fight_success.tpl');
}
else{
$smarty->display('fight_failure.tpl');
}

}
else
{
      // give an error message and call them a cheater 
     echo 'Sorry, this operation cannot be completed again cheater';
}
?>

 

 

That is the basic premise of the code. There may be more to it depending on how many times this needs to run. At some point you may need to unset the session var to allow it to run depending on how the game is set up as this var will be set on all pages until you unset it. To unset it you use unset().

 

This also does not take into account any other operations you need to do such as calling the correct tpl file.

 

<?php
unset($_SESSION['loot_chance_run']);
?>

Thanks I will copy and past this to see how it goes and will get back. The other script above you wrote actually works for a different application as well lol? I changed it a bit but its great for looking up stuff on the database. Anyway I will get back here later!

 

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.