Zeyber Posted November 20, 2007 Share Posted November 20, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/78037-solved-function-using-variable/ Share on other sites More sharing options...
rajivgonsalves Posted November 20, 2007 Share Posted November 20, 2007 one thing I see in your code is getitem(" . $itemusea . ","slot1"); should be getitem("$itemusea","slot1"); Quote Link to comment https://forums.phpfreaks.com/topic/78037-solved-function-using-variable/#findComment-394975 Share on other sites More sharing options...
phpQuestioner Posted November 20, 2007 Share Posted November 20, 2007 try putting the if condition validation in the top of the page; before your function and see if that works. Quote Link to comment https://forums.phpfreaks.com/topic/78037-solved-function-using-variable/#findComment-394976 Share on other sites More sharing options...
rajivgonsalves Posted November 20, 2007 Share Posted November 20, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/78037-solved-function-using-variable/#findComment-394977 Share on other sites More sharing options...
Zeyber Posted November 20, 2007 Author Share Posted November 20, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/78037-solved-function-using-variable/#findComment-394995 Share on other sites More sharing options...
Zeyber Posted November 20, 2007 Author Share Posted November 20, 2007 Oh just in case I mislead. I haven't solved this problem and I still need help. I just found that other bit of code. I still need help! Quote Link to comment https://forums.phpfreaks.com/topic/78037-solved-function-using-variable/#findComment-395018 Share on other sites More sharing options...
Zeyber Posted November 21, 2007 Author Share Posted November 21, 2007 Can anyone please help? Quote Link to comment https://forums.phpfreaks.com/topic/78037-solved-function-using-variable/#findComment-395691 Share on other sites More sharing options...
kenrbnsn Posted November 21, 2007 Share Posted November 21, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/78037-solved-function-using-variable/#findComment-395807 Share on other sites More sharing options...
Zeyber Posted November 22, 2007 Author Share Posted November 22, 2007 $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? Quote Link to comment https://forums.phpfreaks.com/topic/78037-solved-function-using-variable/#findComment-396494 Share on other sites More sharing options...
Northern Flame Posted November 22, 2007 Share Posted November 22, 2007 you need yo use global for the variables inside the function, example: <?php $variable = "I want to use this variable inside a function"; function myFunction(){ global $variable; echo $variable; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/78037-solved-function-using-variable/#findComment-396497 Share on other sites More sharing options...
Zeyber Posted November 22, 2007 Author Share Posted November 22, 2007 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/78037-solved-function-using-variable/#findComment-396503 Share on other sites More sharing options...
darkfreaks Posted November 22, 2007 Share Posted November 22, 2007 <?php If ($itemusea => 1) { ?> Quote Link to comment https://forums.phpfreaks.com/topic/78037-solved-function-using-variable/#findComment-396505 Share on other sites More sharing options...
Northern Flame Posted November 22, 2007 Share Posted November 22, 2007 you also have a few variables in the function that you did not use global for, i think $cs was one, you have to use global for every variable that was defined before your function was created...if you plan to use it in the function.... Quote Link to comment https://forums.phpfreaks.com/topic/78037-solved-function-using-variable/#findComment-396507 Share on other sites More sharing options...
Zeyber Posted November 22, 2007 Author Share Posted November 22, 2007 Omg thank you so much Northern Flame! You are my hero! This has been giving me grief for weeks! Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/78037-solved-function-using-variable/#findComment-396510 Share on other sites More sharing options...
dbo Posted November 22, 2007 Share Posted November 22, 2007 If you have to use global variables all over the place you're doing something wrong. If used improperly they can be very dangerous boogers. Quote Link to comment https://forums.phpfreaks.com/topic/78037-solved-function-using-variable/#findComment-396511 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.