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
https://forums.phpfreaks.com/topic/78037-solved-function-using-variable/
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

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

 

 

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

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

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.