Jump to content

[SOLVED] Function using variable


Zeyber

Recommended Posts

Ok my function doesn't seem to be working when I try to get a variable as the parameter. I know it could work if I repeated the code alot but that would just complicate things. Anyway heres the part I'm having trouble with:

 

//Get Weapon Function
function getitem($itemslot,$slot)
{
switch ($itemslot)
{
/*001 Stake*/
case 1:
{$csma = $cs['MeleeAttack'] + 2;
mysql_query("UPDATE CE SET Weapon = '1' WHERE Name = '$username'");
mysql_query("UPDATE Inventory SET $slot = '0' WHERE Name = '$username'");
mysql_query("UPDATE CS SET MeleeAttack = '$csma' WHERE Name = '$username'");
break;}
/*002 Throwing Twigs*/
case 2:
{$csma = $cs['RangedAttack'] + 1;
mysql_query("UPDATE CE SET Weapon = '2' WHERE Name = '$username'");
mysql_query("UPDATE Inventory SET $slot = '0' WHERE Name = '$username'");
mysql_query("UPDATE CS SET RangedAttack = '$csma' WHERE Name = '$username'");
break;}
/*003 Stick*/
case 3:
{$csma = $cs['MeleeAttack'] + 1;
$csmb = $cs['MagicAttack'] + 1;
mysql_query("UPDATE CE SET Weapon = '3' WHERE Name = '$username'");
mysql_query("UPDATE Inventory SET $slot = '0' WHERE Name = '$username'");
mysql_query("UPDATE CS SET MeleeAttack = '$csma' WHERE Name = '$username'");
mysql_query("UPDATE CS SET MagicAttack = '$csmb' WHERE Name = '$username'");
break;}
}
}

//Check if item is Weapon
If ($row['Weapon'] == 0)
{
/*Slot1*/
If ($itemusea >= 1)
{
  getitem(" . $itemusea . ","slot1");
}
/*Slot2*/
If ($itemuseb >= 1)
{
  getitem("$itemuseb","slot2");
}
/*Slot3*/
If ($itemusec >= 1)
{
  getitem("$itemusec","slot3");
}
}

 

The code works but the function doesn't actually seem to do anything. I need help. Please?

 

EDIT: I think it's to do with the $itemusea variables as the parameters for the function.

Link to comment
Share on other sites

one thing I see in your code is

 

getitem(" . $itemusea . ","slot1");

 

should be

 

getitem("$itemusea","slot1");

 

Yeah I was just testing that.

 

try putting the if condition validation in the top of the page; before your function and see if that works.

 

Not sure if that'll change anything. I tried having the code of the function at each place where the function was called for slot 1 having $slot=slot1 and $itemslot=$itemusea and it worked.

 

also check for your $row['Weapon'] variable if its not 0 then it will never come in the piece of code where the functions are called

 

It's definately 0. I have another page that checks.

 

 

I'll get the code above it later because my host just when temporarilly offline.

 

EDIT: Great I got it! Heres the code above the function:

//Get used item
$itemusea = $HTTP_GET_VARS['slota'];
$itemuseb = $HTTP_GET_VARS['slotb'];
$itemusec = $HTTP_GET_VARS['slotc'];
$result = mysql_query("SELECT * FROM CE WHERE Name='$username'");
$row = mysql_fetch_array($result);
$inv = mysql_query("SELECT * FROM Inventory WHERE Name='$username'");
$inventory = mysql_fetch_array($inv);
$cst = mysql_query("SELECT * FROM CS WHERE Name='$username'");
$cs = mysql_fetch_array($cst);

$itemusea=3, $itemuseb=0, $itemusec=0

Link to comment
Share on other sites

In your function, you use the variable "$username", but it's not set anywhere in the function. You really should be checking the results of your queries to make sure they work. You don't need the "{ }" around the "case" blocks.

 

Use $_GET instead of $HTTP_GET_VARS

 

Ken

 

 

Link to comment
Share on other sites

$username has been set here

 

$username = $_SESSION['username'];

 

I have tested the queries and they work fine.

 

I made those adjustments and here is the new code.

 

//Get used item
$itemusea = $_GET["slota"];
$itemuseb = $_GET["slotb"];
$itemusec = $_GET["slotc"];
If ($itemuseb > 0)
{$item=$itemuseb;}
If ($itemusea > 0)
{$item=$itemusea;}
If ($itemusec > 0)
{$item=$itemusec;}
$result = mysql_query("SELECT * FROM CE WHERE Name='$username'");
$row = mysql_fetch_array($result);
$inv = mysql_query("SELECT * FROM Inventory WHERE Name='$username'");
$inventory = mysql_fetch_array($inv);
$cst = mysql_query("SELECT * FROM CS WHERE Name='$username'");
$cs = mysql_fetch_array($cst);

//Get Weapon Function
function getitem($slot)
{
switch ($item)
{
/*001 Stake*/
case 1:
$csma = $cs['MeleeAttack'] + 2;
mysql_query("UPDATE CE SET Weapon = '1' WHERE Name = '$username'");
mysql_query("UPDATE Inventory SET $slot = '0' WHERE Name = '$username'");
mysql_query("UPDATE CS SET MeleeAttack = '$csma' WHERE Name = '$username'");
break;
/*002 Throwing Twigs*/
case 2:
$csma = $cs['RangedAttack'] + 1;
mysql_query("UPDATE CE SET Weapon = '2' WHERE Name = '$username'");
mysql_query("UPDATE Inventory SET $slot = '0' WHERE Name = '$username'");
mysql_query("UPDATE CS SET RangedAttack = '$csma' WHERE Name = '$username'");
break;
/*003 Stick*/
case 3:
$csma = $cs['MeleeAttack'] + 1;
$csmb = $cs['MagicAttack'] + 1;
mysql_query("UPDATE CE SET Weapon = '3' WHERE Name = '$username'");
mysql_query("UPDATE Inventory SET $slot = '0' WHERE Name = '$username'");
mysql_query("UPDATE CS SET MeleeAttack = '$csma' WHERE Name = '$username'");
mysql_query("UPDATE CS SET MagicAttack = '$csmb' WHERE Name = '$username'");
break;
}
}

//Check if item is Weapon
If ($row['Weapon'] == 0)
{
/*Slot1*/
If ($itemusea >= 1)
{
  getitem(slot1);
}
/*Slot2*/
If ($itemuseb >= 1)
{
  getitem(slot2);
}
/*Slot3*/
If ($itemusec >= 1)
{
  getitem(slot3);
}
}

 

Do the variables have to be set inside the function? Can't you use outside variables?

Link to comment
Share on other sites

That was genious man thanks! Too bad it didn't quite solve it completely. Maybe take a look at the code and see what might be wrong if say $itemusea was equal to 1.

 

//Get used item
$itemusea = $HTTP_GET_VARS['slota'];
$itemuseb = $HTTP_GET_VARS['slotb'];
$itemusec = $HTTP_GET_VARS['slotc'];
If ($itemusea > 0)
{$item=$itemusea;}
elseif ($itemuseb > 0)
{$item=$itemuseb;}
elseif ($itemusec > 0)
{$item=$itemusec;}
$result = mysql_query("SELECT * FROM CE WHERE Name='$username'");
$row = mysql_fetch_array($result);
$inv = mysql_query("SELECT * FROM Inventory WHERE Name='$username'");
$inventory = mysql_fetch_array($inv);
$cst = mysql_query("SELECT * FROM CS WHERE Name='$username'");
$cs = mysql_fetch_array($cst);

//Get Weapon Function
function getitem($slot)
{
global $item;
switch ($item)
{
/*001 Stake*/
case 1:
$csma = $cs['MeleeAttack'] + 2;
mysql_query("UPDATE CE SET Weapon = '1' WHERE Name = '$username'");
mysql_query("UPDATE Inventory SET $slot = '0' WHERE Name = '$username'");
mysql_query("UPDATE CS SET MeleeAttack = '$csma' WHERE Name = '$username'");
break;
/*002 Throwing Twigs*/
case 2:
$csma = $cs['RangedAttack'] + 1;
mysql_query("UPDATE CE SET Weapon = '2' WHERE Name = '$username'");
mysql_query("UPDATE Inventory SET $slot = '0' WHERE Name = '$username'");
mysql_query("UPDATE CS SET RangedAttack = '$csma' WHERE Name = '$username'");
break;
/*003 Stick*/
case 3:
$csma = $cs['MeleeAttack'] + 1;
$csmb = $cs['MagicAttack'] + 1;
mysql_query("UPDATE CE SET Weapon = '3' WHERE Name = '$username'");
mysql_query("UPDATE Inventory SET $slot = '0' WHERE Name = '$username'");
mysql_query("UPDATE CS SET MeleeAttack = '$csma' WHERE Name = '$username'");
mysql_query("UPDATE CS SET MagicAttack = '$csmb' WHERE Name = '$username'");
break;
}
}

//Check if item is Weapon
If ($row['Weapon'] == 0)
{
/*Slot1*/
If ($itemusea >= 1)
{
  getitem(slot1);
}
/*Slot2*/
If ($itemuseb >= 1)
{
  getitem(slot2);
}
/*Slot3*/
If ($itemusec >= 1)
{
  getitem(slot3);
}
}

//Return to inventory
echo '<script> location.href = "http://warriorwars.freehostia.com/game/inventory.php"; </script>';

Link to comment
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.