Jump to content


Photo

Multiple things on 1 script


  • Please log in to reply
4 replies to this topic

#1 supanoob

supanoob
  • Members
  • PipPipPip
  • Advanced Member
  • 342 posts

Posted 12 May 2006 - 09:40 PM

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:

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))

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.

#2 Carth

Carth
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts
  • LocationUK

Posted 12 May 2006 - 10:37 PM

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

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

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

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.

#3 supanoob

supanoob
  • Members
  • PipPipPip
  • Advanced Member
  • 342 posts

Posted 13 May 2006 - 09:34 AM

i dont understand what you mean :S what do i need to change on the script, where it says speed do i need to put option or summin like that?

#4 ignace

ignace
  • Moderators
  • Now mod flavored
  • 6,431 posts
  • LocationBelgium

Posted 13 May 2006 - 06:07 PM

DROPDOWN FORM

$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>';

BACK TO YOUR PHP

$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))


#5 supanoob

supanoob
  • Members
  • PipPipPip
  • Advanced Member
  • 342 posts

Posted 13 May 2006 - 08:37 PM

ummm, ok so most of that makes sense except the part at the end where you have left it to update in the Speed column still :s shouldnt that be where "Option" (take away the "") =$user?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users