Jump to content

Recommended Posts

well, i want to be able to run 3 different things using the same script. the way i want it to work is using a drag down box where you select the thing you want to do. for example:

i am making a gym, and i have Power, Speed and Dexterity now they all increase by the same random ammount. using the code below:

[code]if ($fatigue < 2)
{
echo "you do not have enough fatigue to do this";

die();
}

$rand=rand(1,7);
$rand=$rand/10;
$update=$speed+$rand;
$update2=$fatigue-2;
$sql2="UPDATE players SET speed='$update', fatigue='$update2' WHERE user='$user'";
if(mysql_query($sql2))[/code]

right, thats the code i am using at the moment for Speed ONLY.

now what i would like to do to save server space and loading time, is allow people to select what to train using a drop down menu, and it run that script for whatever they select. so if they select power it would run the script above but train power instead of speed. if someone could help me i would be grateful.
Link to comment
https://forums.phpfreaks.com/topic/9563-multiple-things-on-1-script/
Share on other sites

Pass it as a GET or POST var in the form, and then detect which one they are changing.

For example on your HTML:
[code]<select name="option" size="1">
  <option value="0">Speed</option>
  <option value="1">Power</option>
  <option value="2">Dexterity</option>
</select>[/code]

Then in PHP
[code]switch($option) {
case 0:
    // speed
    break;
case 1:
    // power
    break;
case 2:
    // dexterity
    break;
default:
    echo "error"
}[/code]

If the code to calculate the new values is the same for them all, you can do that before the switch statement, and let the switch statement decide which part of the database to update. You could use the direct result of the drop down box (e.g. make the form return "speed" in the $option var) in the SQL statement, but you could be at risk of code injection if someone puts something malicious there.
DROPDOWN FORM

[code]
$form = '<select name="option" onChange="window.location.href=\'file.php?option=\'+this.options[this.selectedIndex].value;">
<option value="0">Speed</option>
<option value="1">Power</option>
<option value="2">Dexterity</option>
</select>';
[/code]

BACK TO YOUR PHP

[code]
$option = trim( strip_tags( $_REQUEST['option'] ));

switch( $option ){
  case 0:
    ....
    if( $fatique < 2 ){
      print( 'you do not have enough fatique to do this.' );//leave the die(); command out or use die( 'you do not..' );
    }
    ....
  break;
  case 1:
    ...
    power it up
    ...
  break;
  case 2:
    ...
    dextergy WRAAAAWWWW
    ...
  break;
  default:
    print( $form );
  break;
}

$rand=rand(1,7);
$rand=$rand/10;
$update=$speed+$rand;
$update2=$fatigue-2;
$sql2="UPDATE players SET speed='$update', fatigue='$update2' WHERE user='$user'";
if(mysql_query($sql2))
[/code]
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.